From b89ae54a999a17f49aeb9591175d77d494defac7 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Thu, 21 Mar 2024 13:55:18 +0530 Subject: [PATCH] avniproject/avni-product#1561 - reverted observation and point as embedded. updated schema version for skipping 185. --- src/AddressLevel.js | 2 +- src/ChecklistItem.js | 2 +- src/Encounter.js | 8 +-- src/Family.js | 2 +- src/Individual.js | 4 +- src/Observation.js | 3 +- src/ProgramEncounter.js | 8 +-- src/ProgramEnrolment.js | 8 +-- src/Schema.js | 80 +--------------------- src/SchemaNames.js | 2 +- src/draft/DraftEncounter.js | 8 +-- src/draft/DraftSubject.js | 4 +- src/geo/Point.js | 3 +- src/program/SubjectProgramEligibility.js | 2 +- src/relationship/IndividualRelationship.js | 2 +- src/task/Task.ts | 4 +- test/DefinedObjectSchemaTest.js | 2 +- test/SchemaTest.js | 7 -- 18 files changed, 33 insertions(+), 118 deletions(-) delete mode 100644 test/SchemaTest.js diff --git a/src/AddressLevel.js b/src/AddressLevel.js index aa1589bb..c618d130 100644 --- a/src/AddressLevel.js +++ b/src/AddressLevel.js @@ -80,7 +80,7 @@ class AddressLevel extends BaseEntity { level: "double", type: {type: "string", optional: true}, locationMappings: {type: "list", objectType: "LocationMapping"}, - locationProperties: {type: "list", objectType: "EmbeddedObservation"}, + locationProperties: {type: "list", objectType: "Observation"}, titleLineage: {type: "string", optional: true}, voided: {type: "bool", default: false}, parentUuid: {type: "string", optional: true}, diff --git a/src/ChecklistItem.js b/src/ChecklistItem.js index 665d5cc8..38dc302d 100644 --- a/src/ChecklistItem.js +++ b/src/ChecklistItem.js @@ -24,7 +24,7 @@ class ChecklistItem extends BaseEntity { uuid: "string", detail: "ChecklistItemDetail", completionDate: {type: "date", optional: true}, - observations: {type: "list", objectType: "EmbeddedObservation"}, + observations: {type: "list", objectType: "Observation"}, checklist: "Checklist", approvalStatuses: {type: "list", objectType: "EntityApprovalStatus"}, latestEntityApprovalStatus: {type: "EntityApprovalStatus", optional: true}, //Reporting purposes diff --git a/src/Encounter.js b/src/Encounter.js index de8a3725..c99eb2c5 100644 --- a/src/Encounter.js +++ b/src/Encounter.js @@ -20,14 +20,14 @@ class Encounter extends AbstractEncounter { encounterType: "EncounterType", encounterDateTime: {type: "date", optional: true}, individual: SchemaNames.Individual, - observations: {type: "list", objectType: "EmbeddedObservation"}, - encounterLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + observations: {type: "list", objectType: "Observation"}, + encounterLocation: {type: SchemaNames.Point, optional: true}, name: {type: "string", optional: true}, earliestVisitDateTime: {type: "date", optional: true}, maxVisitDateTime: {type: "date", optional: true}, cancelDateTime: {type: "date", optional: true}, - cancelObservations: {type: "list", objectType: "EmbeddedObservation"}, - cancelLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + cancelObservations: {type: "list", objectType: "Observation"}, + cancelLocation: {type: SchemaNames.Point, optional: true}, voided: {type: "bool", default: false}, approvalStatuses: {type: "list", objectType: "EntityApprovalStatus"}, latestEntityApprovalStatus: {type: "EntityApprovalStatus", optional: true}, //Reporting purposes diff --git a/src/Family.js b/src/Family.js index 7de402e9..98e179e1 100644 --- a/src/Family.js +++ b/src/Family.js @@ -29,7 +29,7 @@ class Family extends BaseEntity { typeOfFamily: "string", householdNumber: "string", members: { type: "list", objectType: "Individual" }, - observations: { type: "list", objectType: "EmbeddedObservation" }, + observations: { type: "list", objectType: "Observation" }, }, }; diff --git a/src/Individual.js b/src/Individual.js index 42fd860b..1fcd3d48 100644 --- a/src/Individual.js +++ b/src/Individual.js @@ -52,10 +52,10 @@ class Individual extends BaseEntity { voided: {type: "bool", default: false}, enrolments: {type: "list", objectType: SchemaNames.ProgramEnrolment}, encounters: {type: "list", objectType: SchemaNames.Encounter}, - observations: {type: "list", objectType: "EmbeddedObservation"}, + observations: {type: "list", objectType: "Observation"}, relationships: {type: "list", objectType: "IndividualRelationship"}, groupSubjects: {type: "list", objectType: "GroupSubject"}, - registrationLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + registrationLocation: {type: SchemaNames.Point, optional: true}, comments: {type: "list", objectType: "Comment"}, groups: {type: "list", objectType: "GroupSubject"}, approvalStatuses: {type: "list", objectType: "EntityApprovalStatus"}, diff --git a/src/Observation.js b/src/Observation.js index 48208b0a..949f150d 100644 --- a/src/Observation.js +++ b/src/Observation.js @@ -14,8 +14,7 @@ const clone = function (concept, valueJSON) { class Observation extends PersistedObject { static schema = { - name: "EmbeddedObservation", - embedded: true, + name: "Observation", properties: { concept: "Concept", valueJSON: "string", diff --git a/src/ProgramEncounter.js b/src/ProgramEncounter.js index 8d60760c..31b2c4f0 100644 --- a/src/ProgramEncounter.js +++ b/src/ProgramEncounter.js @@ -32,11 +32,11 @@ class ProgramEncounter extends AbstractEncounter { maxVisitDateTime: {type: "date", optional: true}, encounterDateTime: {type: "date", optional: true}, programEnrolment: SchemaNames.ProgramEnrolment, - observations: {type: "list", objectType: "EmbeddedObservation"}, + observations: {type: "list", objectType: "Observation"}, cancelDateTime: {type: "date", optional: true}, - cancelObservations: {type: "list", objectType: "EmbeddedObservation"}, - encounterLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, - cancelLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + cancelObservations: {type: "list", objectType: "Observation"}, + encounterLocation: {type: SchemaNames.Point, optional: true}, + cancelLocation: {type: SchemaNames.Point, optional: true}, voided: {type: "bool", default: false}, approvalStatuses: {type: "list", objectType: "EntityApprovalStatus"}, latestEntityApprovalStatus: {type: "EntityApprovalStatus", optional: true}, //Reporting purposes diff --git a/src/ProgramEnrolment.js b/src/ProgramEnrolment.js index 400cf768..e17906cd 100644 --- a/src/ProgramEnrolment.js +++ b/src/ProgramEnrolment.js @@ -32,15 +32,15 @@ class ProgramEnrolment extends BaseEntity { uuid: "string", program: "Program", enrolmentDateTime: "date", - observations: {type: "list", objectType: "EmbeddedObservation"}, + observations: {type: "list", objectType: "Observation"}, programExitDateTime: {type: "date", optional: true}, - programExitObservations: {type: "list", objectType: "EmbeddedObservation"}, + programExitObservations: {type: "list", objectType: "Observation"}, programOutcome: {type: "ProgramOutcome", optional: true}, encounters: {type: "list", objectType: SchemaNames.ProgramEncounter}, checklists: {type: "list", objectType: "Checklist"}, individual: "Individual", - enrolmentLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, - exitLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + enrolmentLocation: {type: SchemaNames.Point, optional: true}, + exitLocation: {type: SchemaNames.Point, optional: true}, voided: {type: "bool", default: false}, approvalStatuses: {type: "list", objectType: "EntityApprovalStatus"}, latestEntityApprovalStatus: {type: "EntityApprovalStatus", optional: true}, //Reporting purposes diff --git a/src/Schema.js b/src/Schema.js index 2657f0b0..0152f9cc 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -249,82 +249,6 @@ function migrateEmbeddedObjects(oldDB, newDB,) { newDB.deleteModel("StringKeyNumericValue") } -function flush(db) { - db.commitTransaction(); - db.beginTransaction(); -} - -function shouldFlush(numberOfRecords) { - const batchSize = 100; - return (numberOfRecords % batchSize) === (batchSize - 1); -} - -export function createTransactionDataMapForEmbeddedFields() { - const map = new Map(); - MetaDataService.forEachPointField((fieldName, schemaName) => { - if (map.has(schemaName)) { - map.get(schemaName).push({fieldName, fieldType: "Point"}); - } else { - map.set(schemaName, [{fieldName, fieldType: "Point"}]); - } - }); - MetaDataService.forEachObservationField((fieldName, schemaName) => { - if (map.has(schemaName)) { - map.get(schemaName).push({fieldName, fieldType: "Obs"}); - } else { - map.set(schemaName, [{fieldName, fieldType: "Obs"}]); - } - }); - return map; -} - -function migrateAllEmbeddedForTxnData(oldDB, newDB) { - const startTime = new Date(); - flush(newDB); - const map = createTransactionDataMapForEmbeddedFields(); - - let recordCounter = 0; - const conceptMap = new Map(); - map.forEach((fields, schemaName) => { - console.log(`schema: ${schemaName}, fields: ${fields.length}`); - newDB.objects(schemaName).forEach((newDbParentEntity) => { - if (shouldFlush(recordCounter)) { - flush(newDB); - } - fields.forEach((field) => { - const oldEntity = oldDB.objects(schemaName).filtered(`uuid = "${newDbParentEntity.uuid}"`)[0]; - const oldValue = oldEntity[field.fieldName]; - if (!_.isNil(oldValue)) { - if (field.fieldType === "Point") - newDbParentEntity[field.fieldName] = {x: oldValue.x, y: oldValue.y}; - else { - const newObsList = []; - oldValue.forEach((oldItemValue) => { - let newConcept = conceptMap.get(oldItemValue.concept.uuid); - if (_.isNil(newConcept)) { - newConcept = newDB.objects("Concept").filtered(`uuid = "${oldItemValue.concept.uuid}"`)[0]; - conceptMap.set(oldItemValue.concept.uuid, newConcept); - } - newObsList.push({ - concept: newConcept, - valueJSON: oldItemValue.valueJSON - }); - }); - newDbParentEntity[field.fieldName] = newObsList; - } - } - }); - recordCounter++; - }); - }); - flush(newDB); - newDB.deleteModel("Point"); - newDB.deleteModel("Observation"); - const endTime = new Date(); - const diff = moment(endTime).diff(startTime, "seconds", true); - console.log("Total Time Taken", diff, "seconds"); -} - function createRealmConfig() { return { shouldCompact: function (totalBytes, usedBytes) { @@ -333,7 +257,7 @@ function createRealmConfig() { return doCompact; }, //order is important, should be arranged according to the dependency - schemaVersion: 185, + schemaVersion: 186, onMigration: function (oldDB, newDB) { console.log("[AvniModels.Schema]", `Running migration with old schema version: ${oldDB.schemaVersion} and new schema version: ${newDB.schemaVersion}`); if (oldDB.schemaVersion < 10) { @@ -973,7 +897,7 @@ function createRealmConfig() { migrateEmbeddedObjects(oldDB, newDB); } if (oldDB.schemaVersion < 185) { - migrateAllEmbeddedForTxnData(oldDB, newDB); + // removed migration code. keeping the version number in case this number is required for any checks later } }, }; diff --git a/src/SchemaNames.js b/src/SchemaNames.js index eafffc73..bbe8e4e2 100644 --- a/src/SchemaNames.js +++ b/src/SchemaNames.js @@ -40,7 +40,7 @@ class SchemaNames { static Comment = "Comment"; static Concept = "Concept"; static EntityApprovalStatus = "EntityApprovalStatus"; - static EmbeddedPoint = "EmbeddedPoint"; + static Point = "Point"; } export default SchemaNames; diff --git a/src/draft/DraftEncounter.js b/src/draft/DraftEncounter.js index 098377f3..b6b01da2 100644 --- a/src/draft/DraftEncounter.js +++ b/src/draft/DraftEncounter.js @@ -17,14 +17,14 @@ class DraftEncounter extends BaseEntity { encounterType: "EncounterType", encounterDateTime: {type: "date", optional: true}, individual: "Individual", - observations: {type: "list", objectType: "EmbeddedObservation"}, - encounterLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + observations: {type: "list", objectType: "Observation"}, + encounterLocation: {type: SchemaNames.Point, optional: true}, name: {type: "string", optional: true}, earliestVisitDateTime: {type: "date", optional: true}, maxVisitDateTime: {type: "date", optional: true}, cancelDateTime: {type: "date", optional: true}, - cancelObservations: {type: "list", objectType: "EmbeddedObservation"}, - cancelLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + cancelObservations: {type: "list", objectType: "Observation"}, + cancelLocation: {type: SchemaNames.Point, optional: true}, voided: {type: "bool", default: false} }, }; diff --git a/src/draft/DraftSubject.js b/src/draft/DraftSubject.js index 2bbcc066..a760fee5 100644 --- a/src/draft/DraftSubject.js +++ b/src/draft/DraftSubject.js @@ -23,8 +23,8 @@ class DraftSubject extends BaseEntity { gender: {type: "Gender", optional: true}, registrationDate: "date", lowestAddressLevel: "AddressLevel", - observations: {type: "list", objectType: "EmbeddedObservation"}, - registrationLocation: {type: SchemaNames.EmbeddedPoint, optional: true}, + observations: {type: "list", objectType: "Observation"}, + registrationLocation: {type: SchemaNames.Point, optional: true}, updatedOn: "date", totalMembers: {type: "string", optional: true} }, diff --git a/src/geo/Point.js b/src/geo/Point.js index b5d4b037..d8c856d3 100644 --- a/src/geo/Point.js +++ b/src/geo/Point.js @@ -2,8 +2,7 @@ import PersistedObject from "../PersistedObject"; class Point extends PersistedObject { static schema = { - name: "EmbeddedPoint", - embedded: true, + name: "Point", properties: { x: "double", y: "double", diff --git a/src/program/SubjectProgramEligibility.js b/src/program/SubjectProgramEligibility.js index 58f5fbea..55b1f457 100644 --- a/src/program/SubjectProgramEligibility.js +++ b/src/program/SubjectProgramEligibility.js @@ -19,7 +19,7 @@ class SubjectProgramEligibility extends BaseEntity { program: "Program", checkDate: "date", eligible: {type: "bool", default: false}, - observations: {type: "list", objectType: "EmbeddedObservation"}, + observations: {type: "list", objectType: "Observation"}, voided: {type: "bool", default: false}, ...AuditFields }, diff --git a/src/relationship/IndividualRelationship.js b/src/relationship/IndividualRelationship.js index 9bce8218..fa408e43 100644 --- a/src/relationship/IndividualRelationship.js +++ b/src/relationship/IndividualRelationship.js @@ -19,7 +19,7 @@ class IndividualRelationship extends BaseEntity { individualB: "Individual", enterDateTime: {type: "date", optional: true}, exitDateTime: {type: "date", optional: true}, - exitObservations: {type: "list", objectType: "EmbeddedObservation"}, + exitObservations: {type: "list", objectType: "Observation"}, voided: {type: "bool", default: false}, ...AuditFields }, diff --git a/src/task/Task.ts b/src/task/Task.ts index f03bef55..d3b68fdd 100644 --- a/src/task/Task.ts +++ b/src/task/Task.ts @@ -22,9 +22,9 @@ class Task extends BaseEntity { taskStatus: "TaskStatus", scheduledOn: {type: "date"}, completedOn: {type: "date", optional: true}, - metadata: {type: "list", objectType: "EmbeddedObservation"}, + metadata: {type: "list", objectType: "Observation"}, subject: {type: 'Individual', optional: true}, - observations: {type: "list", objectType: "EmbeddedObservation"}, + observations: {type: "list", objectType: "Observation"}, voided: {type: 'bool', default: false}, ...AuditFields }, diff --git a/test/DefinedObjectSchemaTest.js b/test/DefinedObjectSchemaTest.js index 51112667..9d4e563e 100644 --- a/test/DefinedObjectSchemaTest.js +++ b/test/DefinedObjectSchemaTest.js @@ -18,7 +18,7 @@ const schema = { registrationDate: "date", lowestAddressLevel: {type: "AddressLevel", optional: false}, enrolments: {type: "list", objectType: "Bar"}, - registrationLocation: {type: SchemaNames.EmbeddedPoint, optional: true} + registrationLocation: {type: SchemaNames.Point, optional: true} } }; diff --git a/test/SchemaTest.js b/test/SchemaTest.js deleted file mode 100644 index 09d40f7a..00000000 --- a/test/SchemaTest.js +++ /dev/null @@ -1,7 +0,0 @@ -import {assert} from "chai"; -import {createTransactionDataMapForEmbeddedFields} from "../src/Schema"; - -it('should createTransactionDataMapForEmbeddedFields', function () { - const map = createTransactionDataMapForEmbeddedFields(); - assert.equal(map.get("Encounter").length, 4); -});