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 { (def figwheel-urls {
:ios "ws://localhost:3449/figwheel-ws" :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 (defn realm-version
[file-name encryption-key] [file-name encryption-key]
(if encryption-key (if encryption-key
(.schemaVersion rn-dependencies/realm file-name (to-buffer encryption-key)) ;; we need to try this if previous version was using unencrypted database
(.schemaVersion rn-dependencies/realm file-name))) (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 (defn open-realm
[options file-name encryption-key] [options file-name encryption-key]
@ -57,6 +64,19 @@
(delete-realm file-name) (delete-realm file-name)
(open-realm (last schemas) file-name encryption-key)) (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] (defn- index-entity-schemas [all-schemas]
(into {} (map (juxt :name identity)) (-> all-schemas last :schema))) (into {} (map (juxt :name identity)) (-> all-schemas last :schema)))
@ -78,14 +98,14 @@
(log/debug "Opening base realm... (first run)") (log/debug "Opening base realm... (first run)")
(when @base-realm (when @base-realm
(close @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")) (log/debug "Created @base-realm"))
(defn reset-account-realm [encryption-key] (defn reset-account-realm [encryption-key]
(log/debug "Resetting account realm...") (log/debug "Resetting account realm...")
(when @account-realm (when @account-realm
(close @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)) (.write @account-realm #(.deleteAll @account-realm))
(log/debug "Created @account-realm")) (log/debug "Created @account-realm"))
@ -93,7 +113,7 @@
(log/debug "Moved file with error: " err address) (log/debug "Moved file with error: " err address)
(if err (if err
(log/error "Error moving account realm: " (.-message 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)) (handler err))
(defn change-account [address new-account? encryption-key handler] (defn change-account [address new-account? encryption-key handler]
@ -106,7 +126,7 @@
(log/debug "Moving file " path " to " new-path) (log/debug "Moving file " path " to " new-path)
(fs/move-file path new-path #(move-file-handler address encryption-key % handler))) (fs/move-file path new-path #(move-file-handler address encryption-key % handler)))
(do (do
(reset! account-realm (migrate-realm address account/schemas encryption-key)) (reset! account-realm (open-migrated-realm address account/schemas encryption-key))
(handler nil))))) (handler nil)))))
(declare realm-obj->clj) (declare realm-obj->clj)

View File

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

View File

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