Run migrations if necessary

This commit is contained in:
Julia Friesel
2018-09-27 17:05:36 +02:00
parent 9029be081f
commit 8e31215860
4 changed files with 202 additions and 26 deletions
+28 -13
View File
@@ -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)