diff --git a/src/main/kotlin/com/openlattice/mechanic/upgrades/V3StudyMigrationUpgrade.kt b/src/main/kotlin/com/openlattice/mechanic/upgrades/V3StudyMigrationUpgrade.kt index e8a014c0..7192cc0c 100644 --- a/src/main/kotlin/com/openlattice/mechanic/upgrades/V3StudyMigrationUpgrade.kt +++ b/src/main/kotlin/com/openlattice/mechanic/upgrades/V3StudyMigrationUpgrade.kt @@ -147,18 +147,19 @@ class V3StudyMigrationUpgrade( EnumSet.of(MetadataOption.LAST_WRITE), Optional.empty(), false - ).forEach { (v2Id, fqnToValue) -> - logger.info("Processing study with entity_key_id $v2Id") + ).forEach { (studyEkid, fqnToValue) -> + logger.info("Processing study with entity_key_id $studyEkid") // process study entities into a Study table logger.info("Inserting study $fqnToValue into studies") - if (insertIntoStudiesTable(connection, orgId, v2Id, fqnToValue)) { + if (insertIntoStudiesTable(connection, orgId, studyEkid, fqnToValue)) { // process participants of studies - logger.info("Processing all participants of $v2Id") + logger.info("Processing all participants of $studyEkid") try { - processParticipantsOfStudy(connection, v2Id, orgStudyEntitySetIds, orgMaybeParticipantEntitySetIds) + val studyStringId = fqnToValue[FullQualifiedName("general.stringid")]!!.first() as String + processParticipantsOfStudy(connection, studyEkid, studyStringId, orgStudyEntitySetIds, orgMaybeParticipantEntitySetIds) } catch (ex: Exception) { - logger.error("An error occurred processing participants of $v2Id", ex) + logger.error("An error occurred processing participants of $studyEkid", ex) } } } @@ -184,6 +185,7 @@ class V3StudyMigrationUpgrade( entitySets.keySet( Predicates.equal("name", "chronicle_participants_${legacyStudyFqnToValue.getOrDefault(FullQualifiedName("general.stringid"), null)}") ).forEach { participantESID -> + val legacyStudyStringId = legacyStudyFqnToValue[FullQualifiedName("general.stringid")]!!.first() as String dataQueryService.getEntitiesWithPropertyTypeFqns( mapOf(participantESID to Optional.of(setOf())), mapOf(participantESID to legacyParticipantPropertyTypes), @@ -193,7 +195,7 @@ class V3StudyMigrationUpgrade( false ).forEach { (_, legacyParticipantFqnToValue) -> logger.info("Inserting participant: $legacyParticipantFqnToValue into candidates") - insertIntoCandidatesTable(connection, legacyStudyEkid, legacyParticipantFqnToValue) + insertIntoCandidatesTable(connection, legacyStudyStringId, legacyParticipantFqnToValue) } } } catch (ex: Exception) { @@ -206,7 +208,7 @@ class V3StudyMigrationUpgrade( return true } - private fun insertIntoStudiesTable(conn: Connection, orgId: UUID, studyId: UUID, fqnToValue: Map>): Boolean { + private fun insertIntoStudiesTable(conn: Connection, orgId: UUID, studyEkid: UUID, fqnToValue: Map>): Boolean { val columns = fqnToStudiesColumnName.filter { fqnToValue.containsKey(it.key) } if (columns.isEmpty()) { logger.info("No useful property to record for study ${fqnToValue}, skipping") @@ -222,7 +224,7 @@ class V3StudyMigrationUpgrade( try { var index = 1 - ps.setObject(index++, studyId) + ps.setObject(index++, studyEkid) ps.setObject(index++, orgId) columns.keys.forEach { when (it.namespace) { @@ -246,7 +248,7 @@ class V3StudyMigrationUpgrade( return true } - private fun processParticipantsOfStudy(conn: Connection, studyEkid: UUID, orgStudyEntitySetIds: Set, orgMaybeParticipantEntitySetIds: Set) { + private fun processParticipantsOfStudy(conn: Connection, studyEkid: UUID, studyStringId: String, orgStudyEntitySetIds: Set, orgMaybeParticipantEntitySetIds: Set) { val filter = EntityNeighborsFilter(setOf(studyEkid), Optional.of(orgMaybeParticipantEntitySetIds), Optional.of(orgStudyEntitySetIds), Optional.empty()) @@ -273,12 +275,12 @@ class V3StudyMigrationUpgrade( val neighborFqnToValue = neighbor.neighborDetails.get() + neighbor.associationDetails.filterKeys { it == FullQualifiedName("ol.status") } logger.info("Inserting participant: $neighborFqnToValue into candidates") - insertIntoCandidatesTable(conn, studyEkid, neighborFqnToValue) + insertIntoCandidatesTable(conn, studyStringId, neighborFqnToValue) } } } - private fun insertIntoCandidatesTable(conn: Connection, studyId: UUID, fqnToValue: Map>) { + private fun insertIntoCandidatesTable(conn: Connection, studyStringId: String, fqnToValue: Map>) { val columns = fqnToCandidatesColumnName.filter { fqnToValue.containsKey(it.key) } if (columns.isEmpty()) { logger.info("No useful property to record for participant ${fqnToValue}, skipping") @@ -286,7 +288,7 @@ class V3StudyMigrationUpgrade( } val INSERT_INTO_CANDIDATE_SQL = """ - INSERT INTO candidates (v2_study_ekid, ${columns.values.joinToString()}) VALUES (?${",?".repeat(columns.size)}) + INSERT INTO candidates (v2_study_id, ${columns.values.joinToString()}) VALUES (?${",?".repeat(columns.size)}) """.trimIndent() logger.debug(INSERT_INTO_CANDIDATE_SQL) @@ -294,7 +296,7 @@ class V3StudyMigrationUpgrade( try { var index = 1 - ps.setObject(index++, studyId) + ps.setString(index++, studyStringId) columns.keys.forEach { when (it.name) { "PersonBirthDate" -> ps.setObject(index++, fqnToValue[it]!!.first() as LocalDate)