Copy database and delete old one

This commit is contained in:
Julia Friesel
2018-09-07 20:01:53 +02:00
parent 701f1bc5ce
commit b7470151b4
8 changed files with 102 additions and 127 deletions
+19 -2
View File
@@ -1,5 +1,7 @@
import Realm from 'realm'
import { LocalDate, ChronoUnit } from 'js-joda'
import fs from 'react-native-fs'
import restart from 'react-native-restart'
import {
cycleWithFhmMucus,
longAndComplicatedCycleWithMucus,
@@ -154,7 +156,7 @@ const realmConfig = {
deleteRealmIfMigrationNeeded: true
}
const db = new Realm(realmConfig)
let db = new Realm(realmConfig)
const bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
const temperatureDaysSortedByDate = db.objects('CycleDay').filtered('temperature != null').sorted('date', true)
@@ -289,6 +291,20 @@ function tryToImportWithoutDelete(cycleDays) {
})
}
async function encrypt() {
const oldPath = db.path
const dir = db.path.split('/')
dir.pop()
dir.push('copied.realm')
const copyPath = dir.join('/')
db.writeCopyTo(copyPath)
db.close()
await fs.unlink(oldPath)
await fs.moveFile(copyPath, oldPath)
db = new Realm(realmConfig)
restart.Restart()
}
export {
saveSymptom,
getOrCreateCycleDay,
@@ -303,5 +319,6 @@ export {
getAmountOfCycleDays,
schema,
tryToImportWithDelete,
tryToImportWithoutDelete
tryToImportWithoutDelete,
encrypt
}