Skip to content

Commit

Permalink
avniproject/avni-product#1561 - reverted observation and point as emb…
Browse files Browse the repository at this point in the history
…edded. updated schema version for skipping 185.
  • Loading branch information
petmongrels committed Mar 21, 2024
1 parent 068dc82 commit b89ae54
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/AddressLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion src/ChecklistItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Encounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Family.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/Individual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
3 changes: 1 addition & 2 deletions src/Observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/ProgramEncounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/ProgramEnrolment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 2 additions & 78 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/SchemaNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SchemaNames {
static Comment = "Comment";
static Concept = "Concept";
static EntityApprovalStatus = "EntityApprovalStatus";
static EmbeddedPoint = "EmbeddedPoint";
static Point = "Point";
}

export default SchemaNames;
8 changes: 4 additions & 4 deletions src/draft/DraftEncounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/draft/DraftSubject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
},
Expand Down
3 changes: 1 addition & 2 deletions src/geo/Point.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/program/SubjectProgramEligibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion src/relationship/IndividualRelationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions src/task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion test/DefinedObjectSchemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
};

Expand Down
7 changes: 0 additions & 7 deletions test/SchemaTest.js

This file was deleted.

0 comments on commit b89ae54

Please sign in to comment.