From 8e31215860749816ece2a9206f6b420bf9543ecf Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Thu, 27 Sep 2018 17:05:36 +0200 Subject: [PATCH] Run migrations if necessary --- db/index.js | 41 ++++++--- db/{schema.js => schemas/0.js} | 27 +++--- db/schemas/1.js | 156 +++++++++++++++++++++++++++++++++ db/schemas/index.js | 4 + 4 files changed, 202 insertions(+), 26 deletions(-) rename db/{schema.js => schemas/0.js} (90%) create mode 100644 db/schemas/1.js create mode 100644 db/schemas/index.js diff --git a/db/index.js b/db/index.js index 0b0c15d..34afd23 100644 --- a/db/index.js +++ b/db/index.js @@ -11,13 +11,38 @@ import { longAndComplicatedCycleWithCervix, cycleWithTempAndNoCervixShift } from './fixtures' -import dbSchema from './schema' +import schemas from './schemas' let db -const realmConfig = { - schema: dbSchema + +export async function openDb ({ hash, persistConnection }) { + const realmConfig = {} + if (hash) { + realmConfig.encryptionKey = hashToInt8Array(hash) + } + + // perform migrations if necessary, see https://realm.io/docs/javascript/2.8.0/#migrations + let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath) + while (nextSchemaIndex < schemas.length - 1) { + const tempConfig = Object.assign( + realmConfig, + schemas[nextSchemaIndex++] + ) + const migratedRealm = new Realm(tempConfig) + migratedRealm.close() + } + + // open the Realm with the latest schema + realmConfig.schema = schemas[schemas.length - 1] + const connection = await Realm.open(Object.assign( + realmConfig, + schemas[schemas.length - 1] + )) + + if (persistConnection) db = connection } + export function getBleedingDaysSortedByDate() { return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) } @@ -160,16 +185,6 @@ export function requestHash(type, pw) { })) } -export async function openDb ({ hash, persistConnection }) { - if (hash) { - realmConfig.encryptionKey = hashToInt8Array(hash) - } - - const connection = await Realm.open(realmConfig) - - if (persistConnection) db = connection -} - export async function changeEncryptionAndRestartApp(hash) { let key if (hash) key = hashToInt8Array(hash) diff --git a/db/schema.js b/db/schemas/0.js similarity index 90% rename from db/schema.js rename to db/schemas/0.js index 96445cc..1387195 100644 --- a/db/schema.js +++ b/db/schemas/0.js @@ -67,8 +67,6 @@ const SexSchema = { patch: { type: 'bool', optional: true }, ring: { type: 'bool', optional: true }, implant: { type: 'bool', optional: true }, - diaphragm: { type: 'bool', optional: true }, - none: { type: 'bool', optional: true }, other: { type: 'bool', optional: true }, note: { type: 'string', optional: true } } @@ -129,14 +127,17 @@ const CycleDaySchema = { } } -export default [ - CycleDaySchema, - TemperatureSchema, - BleedingSchema, - MucusSchema, - CervixSchema, - NoteSchema, - DesireSchema, - SexSchema, - PainSchema -] +export default { + schema: [ + CycleDaySchema, + TemperatureSchema, + BleedingSchema, + MucusSchema, + CervixSchema, + NoteSchema, + DesireSchema, + SexSchema, + PainSchema + ], + schemaVersion: 0 +} diff --git a/db/schemas/1.js b/db/schemas/1.js new file mode 100644 index 0000000..572a221 --- /dev/null +++ b/db/schemas/1.js @@ -0,0 +1,156 @@ +const TemperatureSchema = { + name: 'Temperature', + properties: { + value: 'double', + exclude: 'bool', + time: { + type: 'string', + optional: true + }, + note: { + type: 'string', + optional: true + } + } +} + +const BleedingSchema = { + name: 'Bleeding', + properties: { + value: 'int', + exclude: 'bool' + } +} + +const MucusSchema = { + name: 'Mucus', + properties: { + feeling: 'int', + texture: 'int', + value: 'int', + exclude: 'bool' + } +} + +const CervixSchema = { + name: 'Cervix', + properties: { + opening: 'int', + firmness: 'int', + position: {type: 'int', optional: true }, + exclude: 'bool' + } +} + +const NoteSchema = { + name: 'Note', + properties: { + value: 'string' + } +} + +const DesireSchema = { + name: 'Desire', + properties: { + value: 'int' + } +} + +const SexSchema = { + name: 'Sex', + properties: { + solo: { type: 'bool', optional: true }, + partner: { type: 'bool', optional: true }, + condom: { type: 'bool', optional: true }, + pill: { type: 'bool', optional: true }, + iud: { type: 'bool', optional: true }, + patch: { type: 'bool', optional: true }, + ring: { type: 'bool', optional: true }, + implant: { type: 'bool', optional: true }, + diaphragm: { type: 'bool', optional: true }, + none: { type: 'bool', optional: true }, + other: { type: 'bool', optional: true }, + note: { type: 'string', optional: true } + } +} + +const PainSchema = { + name: 'Pain', + properties: { + cramps: { type: 'bool', optional: true }, + ovulationPain: { type: 'bool', optional: true }, + headache: { type: 'bool', optional: true }, + backache: { type: 'bool', optional: true }, + nausea: { type: 'bool', optional: true }, + tenderBreasts: { type: 'bool', optional: true }, + migraine: { type: 'bool', optional: true }, + other: { type: 'bool', optional: true }, + note: { type: 'string', optional: true } + } +} + +const CycleDaySchema = { + name: 'CycleDay', + primaryKey: 'date', + properties: { + date: 'string', + temperature: { + type: 'Temperature', + optional: true + }, + bleeding: { + type: 'Bleeding', + optional: true + }, + mucus: { + type: 'Mucus', + optional: true + }, + cervix: { + type: 'Cervix', + optional: true + }, + note: { + type: 'Note', + optional: true + }, + desire: { + type: 'Desire', + optional: true + }, + sex: { + type: 'Sex', + optional: true + }, + pain: { + type: 'Pain', + optional: true + } + } +} + +export default { + schema: [ + CycleDaySchema, + TemperatureSchema, + BleedingSchema, + MucusSchema, + CervixSchema, + NoteSchema, + DesireSchema, + SexSchema, + PainSchema + ], + schemaVersion: 1, + migration: (oldRealm, newRealm) => { + if (oldRealm.schemaVersion >= 1) return + const oldCycleDays = oldRealm.objects('CycleDay') + const newCycleDays = newRealm.objects('CycleDay') + + oldCycleDays.forEach((day, i) => { + if (!day.sex) return + newCycleDays[i].sex.diaphragm = null + newCycleDays[i].sex.none = null + }) + } +} diff --git a/db/schemas/index.js b/db/schemas/index.js new file mode 100644 index 0000000..eacf7dd --- /dev/null +++ b/db/schemas/index.js @@ -0,0 +1,4 @@ +import schema0 from './0.js' +import schema1 from './1.js' + +export default [schema0, schema1] \ No newline at end of file