Skip to content

Commit

Permalink
Export notes as zip files when refreshing and realized that database … (
Browse files Browse the repository at this point in the history
logseq#782)

* Export notes as zip files when refreshing and realized that database schema changed

Resolved logseq#781
  • Loading branch information
tiensonqin authored Nov 29, 2020
1 parent afdfeae commit ed92e69
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 59 deletions.
47 changes: 22 additions & 25 deletions src/main/frontend/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1887,38 +1887,35 @@
(d/transact! db-conn [(me-tx (d/db db-conn) me)]))))

(defn restore!
[{:keys [repos] :as me} restore-config-handler db-schema-changed-handler]
[{:keys [repos] :as me} restore-config-handler]
(let [logged? (:name me)]
(doall
(for [{:keys [url]} repos]
(let [repo url
db-name (datascript-files-db repo)
db-conn (d/create-conn db-schema/files-db-schema)]
(swap! conns assoc db-name db-conn)
(->
(p/let [stored (-> (.getItem localforage-instance db-name)
(p/then (fn [result]
result))
(p/catch (fn [error]
nil)))
_ (when stored
(let [stored-db (string->db stored)
attached-db (d/db-with stored-db [(me-tx stored-db me)])]
(reset-conn! db-conn attached-db)))
db-name (datascript-db repo)
db-conn (d/create-conn db-schema/schema)
_ (d/transact! db-conn [{:schema/version db-schema/version}])
_ (swap! conns assoc db-name db-conn)
stored (.getItem localforage-instance db-name)
_ (if stored
(let [stored-db (string->db stored)
attached-db (d/db-with stored-db [(me-tx stored-db me)])]
(reset-conn! db-conn attached-db)
(when (not= (:schema stored-db) db-schema/schema) ;; check for db schema changes
(db-schema-changed-handler {:url repo})))
(when logged?
(d/transact! db-conn [(me-tx (d/db db-conn) me)])))
_ (restore-config-handler repo)])))))))
(p/let [stored (-> (.getItem localforage-instance db-name)
(p/then (fn [result]
result))
(p/catch (fn [error]
nil)))
_ (when stored
(let [stored-db (string->db stored)
attached-db (d/db-with stored-db [(me-tx stored-db me)])]
(reset-conn! db-conn attached-db)))
db-name (datascript-db repo)
db-conn (d/create-conn db-schema/schema)
_ (d/transact! db-conn [{:schema/version db-schema/version}])
_ (swap! conns assoc db-name db-conn)
stored (.getItem localforage-instance db-name)
_ (if stored
(let [stored-db (string->db stored)
attached-db (d/db-with stored-db [(me-tx stored-db me)])]
(reset-conn! db-conn attached-db))
(when logged?
(d/transact! db-conn [(me-tx (d/db db-conn) me)])))]
(restore-config-handler repo)))))))

(defn- build-edges
[edges]
Expand Down
87 changes: 53 additions & 34 deletions src/main/frontend/handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
[frontend.handler.export :as export-handler]
[frontend.ui :as ui]
[goog.object :as gobj]
[frontend.helper :as helper]))
[frontend.helper :as helper]
[lambdaisland.glogi :as log]))

(defn- watch-for-date!
[]
Expand All @@ -30,30 +31,73 @@
(repo-handler/create-today-journal-if-not-exists repo))))))
1000))

(defn store-schema!
[]
(storage/set :db-schema db-schema/schema))

(defn schema-changed?
[]
(when-let [schema (storage/get :db-schema)]
(not= schema db-schema/schema)))

(defn- get-me-and-repos
[]
(let [me (and js/window.user (bean/->clj js/window.user))
logged? (:name me)
repos (if logged?
(:repos me)
[{:url config/local-repo}])]
{:me me
:logged? logged?
:repos repos}))

(declare restore-and-setup!)

(defn clear-stores-and-refresh!
[]
(p/let [_ (db/clear-local-storage-and-idb!)]
(let [{:keys [me logged? repos]} (get-me-and-repos)]
(js/window.location.reload))))

(defn restore-and-setup!
[me repos logged?]
;; wait until pfs is loaded
(let [pfs-loaded? (atom js/window.pfs)
interval (atom nil)
db-schema-changed-handler (if (state/logged?)
(fn [repo]
(repo-handler/rebuild-index! repo))
(fn [_] nil))
inner-fn (fn []
(when (and @interval js/window.pfs)
(js/clearInterval @interval)
(reset! interval nil)
(-> (p/all (db/restore! (assoc me :repos repos)
(fn [repo]
(file-handler/restore-config! repo false)
(ui-handler/add-style-if-exists!))
db-schema-changed-handler))
(ui-handler/add-style-if-exists!))))
(p/then
(fn []
(if (and (not logged?)
(not (seq (db/get-files config/local-repo))))
(repo-handler/setup-local-repo-if-not-exists!)
(state/set-db-restoring! false))

(if (schema-changed?)
(do
(notification/show!
[:p "Database schema changed, your notes will be exported as zip files, your repos will be re-indexed then."]
:warning
false)
(let [export-repos (for [repo repos]
(when-let [url (:url repo)]
(println "Export repo: " url)
(export-handler/export-repo-as-zip! url)))]
(-> (p/all export-repos)
(p/then (fn []
(store-schema!)
(js/setTimeout clear-stores-and-refresh! 5000)))
(p/catch (fn [error]
(log/error :export/zip {:error error
:repos repos}))))))
(store-schema!))

(page-handler/init-commands!)
(if (seq (:repos me))
;; FIXME: handle error
Expand Down Expand Up @@ -119,33 +163,9 @@
(js/window.addEventListener "online" handle-connection-change)
(js/window.addEventListener "offline" handle-connection-change))

(defn store-schema!
[]
(storage/set :db-schema db-schema/schema))

(defn clear-stores-if-schema-changed!
[handler]
(if (not= (storage/get :db-schema) db-schema/schema)
;; TODO: export repo zip
(p/let [_ (db/clear-local-storage-and-idb!)]
(handler)
(store-schema!))
(do
(handler)
(store-schema!))))

(defn clear-stores-and-refresh!
[]
(p/let [_ (db/clear-local-storage-and-idb!)]
(js/window.location.reload)))

(defn start!
[render]
(let [me (and js/window.user (bean/->clj js/window.user))
logged? (:name me)
repos (if logged?
(:repos me)
[{:url config/local-repo}])]
(let [{:keys [me logged? repos]} (get-me-and-repos)]
(when me (state/set-state! :me me))
(state/set-db-restoring! true)
(render)
Expand All @@ -157,8 +177,7 @@
(notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
(state/set-indexedb-support! false)))

(clear-stores-if-schema-changed!
#(restore-and-setup! me repos logged?))
(restore-and-setup! me repos logged?)

(periodically-persist-repo-to-indexeddb!)

Expand Down

0 comments on commit ed92e69

Please sign in to comment.