Erase realm on new installation

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
janherich 2018-05-17 09:57:45 +02:00 committed by Andrey Shovkoplyas
parent 9b5058d632
commit 7574a1dc25
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
6 changed files with 52 additions and 70 deletions

View File

@ -2,5 +2,5 @@
(def figwheel-urls {
:ios "ws://localhost:3449/figwheel-ws"
:android "ws://10.0.2.2:3449/figwheel-ws"
})
:android "ws://10.0.3.2:3449/figwheel-ws"
})

View File

@ -23,8 +23,15 @@
(defn realm-version
[file-name encryption-key]
(if encryption-key
(.schemaVersion rn-dependencies/realm file-name (to-buffer encryption-key))
(.schemaVersion rn-dependencies/realm file-name)))
;; we need to try this if previous version was using unencrypted database
(try
(.schemaVersion rn-dependencies/realm file-name (to-buffer encryption-key))
(catch js/Object e
(log/info "Attempting to read encrypted file failes")))
(try
(.schemaVersion rn-dependencies/realm file-name)
(catch js/Object e
(log/info "Attempting to read unencrypted file failed")))))
(defn open-realm
[options file-name encryption-key]
@ -57,6 +64,19 @@
(delete-realm file-name)
(open-realm (last schemas) file-name encryption-key))
(defn open-migrated-realm
[file-name schemas encryption-key]
;; TODO: remove for release 0.9.20
;; delete the realm file if its schema version is higher
;; than existing schema version (this means the previous
;; install has incompatible database schemas)
(if-let [current-version (realm-version file-name encryption-key)]
(if (> current-version
(apply max (map :schemaVersion base/schemas)))
(reset-realm file-name schemas encryption-key)
(migrate-realm file-name schemas encryption-key))
(reset-realm file-name schemas encryption-key)))
(defn- index-entity-schemas [all-schemas]
(into {} (map (juxt :name identity)) (-> all-schemas last :schema)))
@ -78,14 +98,14 @@
(log/debug "Opening base realm... (first run)")
(when @base-realm
(close @base-realm))
(reset! base-realm (migrate-realm (.-defaultPath rn-dependencies/realm) base/schemas encryption-key))
(reset! base-realm (open-migrated-realm (.-defaultPath rn-dependencies/realm) base/schemas encryption-key))
(log/debug "Created @base-realm"))
(defn reset-account-realm [encryption-key]
(log/debug "Resetting account realm...")
(when @account-realm
(close @account-realm))
(reset! account-realm (migrate-realm new-account-filename account/schemas encryption-key))
(reset! account-realm (open-migrated-realm new-account-filename account/schemas encryption-key))
(.write @account-realm #(.deleteAll @account-realm))
(log/debug "Created @account-realm"))
@ -93,7 +113,7 @@
(log/debug "Moved file with error: " err address)
(if err
(log/error "Error moving account realm: " (.-message err))
(reset! account-realm (migrate-realm address account/schemas encryption-key)))
(reset! account-realm (open-migrated-realm address account/schemas encryption-key)))
(handler err))
(defn change-account [address new-account? encryption-key handler]
@ -106,7 +126,7 @@
(log/debug "Moving file " path " to " new-path)
(fs/move-file path new-path #(move-file-handler address encryption-key % handler)))
(do
(reset! account-realm (migrate-realm address account/schemas encryption-key))
(reset! account-realm (open-migrated-realm address account/schemas encryption-key))
(handler nil)))))
(declare realm-obj->clj)

View File

@ -1,11 +1,7 @@
(ns status-im.data-store.realm.schemas.base.core
(:require [status-im.data-store.realm.schemas.base.v1.core :as v1]
[status-im.data-store.realm.schemas.base.v2.core :as v2]))
(:require [status-im.data-store.realm.schemas.base.v1.core :as v1]))
;; put schemas ordered by version
(def schemas [{:schema v1/schema
:schemaVersion 1
:migration v1/migration}
{:schema v2/schema
:schemaVersion 2
:migration v2/migration}])
:migration v1/migration}])

View File

@ -2,23 +2,25 @@
(def schema {:name :account
:primaryKey :address
:properties {:address :string
:public-key :string
:name {:type :string :optional true}
:email {:type :string :optional true}
:status {:type :string :optional true}
:debug? {:type :bool :default false}
:photo-path :string
:signing-phrase {:type :string}
:mnemonic {:type :string}
:last-updated {:type :int :default 0}
:last-sign-in {:type :int :default 0}
:signed-up? {:type :bool
:default false}
:network :string
:networks {:type :list
:objectType :network}
:settings {:type :string}
:sharing-usage-data? {:type :bool :default false}
:dev-mode? {:type :bool :default false}
:seed-backed-up? {:type :bool :default false}}})
:properties {:address :string
:public-key :string
:name {:type :string :optional true}
:email {:type :string :optional true}
:status {:type :string :optional true}
:debug? {:type :bool :default false}
:photo-path :string
:signing-phrase {:type :string}
:mnemonic {:type :string}
:last-updated {:type :int :default 0}
:last-sign-in {:type :int :default 0}
:signed-up? {:type :bool
:default false}
:network :string
:networks {:type :list
:objectType :network}
:settings {:type :string}
:sharing-usage-data? {:type :bool :default false}
:dev-mode? {:type :bool :default false}
:seed-backed-up? {:type :bool :default false}
:wallet-set-up-passed? {:type :bool
:default false}}})

View File

@ -1,26 +0,0 @@
(ns status-im.data-store.realm.schemas.base.v2.account)
(def schema {:name :account
:primaryKey :address
:properties {:address :string
:public-key :string
:name {:type :string :optional true}
:email {:type :string :optional true}
:status {:type :string :optional true}
:debug? {:type :bool :default false}
:photo-path :string
:signing-phrase {:type :string}
:mnemonic {:type :string}
:last-updated {:type :int :default 0}
:last-sign-in {:type :int :default 0}
:signed-up? {:type :bool
:default false}
:network :string
:networks {:type :list
:objectType :network}
:settings {:type :string}
:sharing-usage-data? {:type :bool :default false}
:dev-mode? {:type :bool :default false}
:seed-backed-up? {:type :bool :default false}
:wallet-set-up-passed? {:type :bool
:default false}}})

View File

@ -1,10 +0,0 @@
(ns status-im.data-store.realm.schemas.base.v2.core
(:require [status-im.data-store.realm.schemas.base.v1.network :as network]
[status-im.data-store.realm.schemas.base.v2.account :as account]
[taoensso.timbre :as log]))
(def schema [network/schema
account/schema])
(defn migration [old-realm new-realm]
(log/debug "migrating base database v2: " old-realm new-realm))