mirror of
https://github.com/logseq/logseq.git
synced 2026-05-05 11:26:26 +00:00
add frontend.db.restore
This commit is contained in:
@@ -73,24 +73,6 @@
|
||||
|
||||
[logseq.db.default built-in-pages-names built-in-pages])
|
||||
|
||||
(defn- old-schema?
|
||||
"Requires migration if the schema version is older than db-schema/version"
|
||||
[db]
|
||||
(let [v (db-migrate/get-schema-version db)
|
||||
;; backward compatibility
|
||||
v (if (integer? v) v 0)]
|
||||
(cond
|
||||
(= db-schema/version v)
|
||||
false
|
||||
|
||||
(< db-schema/version v)
|
||||
(do
|
||||
(js/console.error "DB schema version is newer than the app, please update the app. " ":db-version" v)
|
||||
false)
|
||||
|
||||
:else
|
||||
true)))
|
||||
|
||||
;; persisting DBs between page reloads
|
||||
(defn persist! [repo]
|
||||
(let [key (datascript-db repo)
|
||||
@@ -159,149 +141,6 @@
|
||||
(assoc option
|
||||
:listen-handler listen-and-persist!))))
|
||||
|
||||
(defn restore-graph-from-text!
|
||||
"Swap db string into the current db status
|
||||
stored: the text to restore from"
|
||||
[repo stored]
|
||||
(p/let [db-name (datascript-db repo)
|
||||
db-conn (d/create-conn db-schema/schema)
|
||||
_ (swap! conns assoc db-name db-conn)
|
||||
_ (when stored
|
||||
(let [stored-db (try (string->db stored)
|
||||
(catch :default _e
|
||||
(js/console.warn "Invalid graph cache")
|
||||
(d/empty-db db-schema/schema)))
|
||||
attached-db (d/db-with stored-db
|
||||
default-db/built-in-pages) ;; TODO bug overriding uuids?
|
||||
db (if (old-schema? attached-db)
|
||||
(db-migrate/migrate attached-db)
|
||||
attached-db)]
|
||||
(conn/reset-conn! db-conn db)))]
|
||||
(d/transact! db-conn [{:schema/version db-schema/version}])))
|
||||
|
||||
(defn- uuid-str->uuid-in-av-vec
|
||||
[[a v]]
|
||||
(cond
|
||||
(and (= :block/uuid a) (string? v))
|
||||
[a (uuid v)]
|
||||
|
||||
(and (coll? v) (= 2 (count v))
|
||||
(= :block/uuid (first v))
|
||||
(string? (second v)))
|
||||
[a [:block/uuid (uuid (second v))]]
|
||||
|
||||
:else
|
||||
[a v]))
|
||||
|
||||
(defn- add-tempid-to-av-colls
|
||||
[start-tempid av-colls]
|
||||
(map-indexed (fn [idx av-coll]
|
||||
(map (partial cons (dec (- start-tempid idx))) av-coll))
|
||||
av-colls))
|
||||
|
||||
(defn restore-other-data-from-sqlite!
|
||||
[repo data]
|
||||
(let [per-length 2000]
|
||||
(p/loop [data data]
|
||||
(cond
|
||||
(not= repo (state/get-current-repo)) ; switched to another graph
|
||||
nil
|
||||
|
||||
(empty? data)
|
||||
nil
|
||||
|
||||
(not (state/input-idle? repo)) ; wait until input is idle
|
||||
(js/setTimeout #(restore-other-data-from-sqlite! repo data) 5000)
|
||||
|
||||
:else
|
||||
(let [part (->> (take per-length data)
|
||||
(map (fn [block]
|
||||
(map uuid-str->uuid-in-av-vec
|
||||
(edn/read-string (gobj/get block "datoms")))))
|
||||
(map-indexed (fn [idx av-coll]
|
||||
(->> av-coll
|
||||
(map (partial cons (dec (- idx))))
|
||||
(sort-by #(if (= :block/uuid (second %)) 0 1)))))
|
||||
(apply concat)
|
||||
(map (fn [eav] (cons :db/add eav))))]
|
||||
(transact! repo part {:skip-persist? true})
|
||||
(p/let [_ (p/delay 200)]
|
||||
(p/recur (drop per-length data)))
|
||||
)))))
|
||||
|
||||
(defn restore-graph-from-sqlite!
|
||||
"Load initial data from SQLite"
|
||||
[repo]
|
||||
(p/let [db-name (datascript-db repo)
|
||||
db-conn (d/create-conn db-schema/schema)
|
||||
_ (swap! conns assoc db-name db-conn)
|
||||
data (ipc/ipc :get-initial-data repo)
|
||||
{:keys [all-pages all-blocks journal-blocks]} (bean/->clj data)
|
||||
pages (map (fn [page]
|
||||
(->> page
|
||||
:datoms
|
||||
edn/read-string
|
||||
(map uuid-str->uuid-in-av-vec)))
|
||||
all-pages)
|
||||
all-blocks' (map (fn [b]
|
||||
[[:block/uuid (uuid (:uuid b))]
|
||||
[:block/page [:block/uuid (uuid (:page_uuid b))]]])
|
||||
all-blocks)
|
||||
journal-blocks' (map (fn [b]
|
||||
(->> b
|
||||
:datoms
|
||||
edn/read-string
|
||||
(map uuid-str->uuid-in-av-vec)))
|
||||
journal-blocks)
|
||||
pages-eav-colls (add-tempid-to-av-colls 0 pages)
|
||||
pages-eav-coll (->> pages-eav-colls
|
||||
(apply concat)
|
||||
(sort-by (fn [eav] (if (= :block/uuid (second eav)) 0 1))))
|
||||
blocks-eav-colls (->> (concat all-blocks' journal-blocks')
|
||||
(add-tempid-to-av-colls (- (count pages-eav-colls)))
|
||||
(apply concat))
|
||||
tx-data (map (partial cons :db/add) (concat pages-eav-coll blocks-eav-colls))]
|
||||
(def xx [all-pages all-blocks journal-blocks tx-data])
|
||||
(d/transact! db-conn tx-data)
|
||||
|
||||
;; TODO: Store schema in sqlite
|
||||
;; (db-migrate/migrate attached-db)
|
||||
|
||||
(d/transact! db-conn [(react/kv :db/type "db")
|
||||
{:schema/version db-schema/version}]
|
||||
{:skip-persist? true})
|
||||
(println :restore-graph-from-sqlite! :done)
|
||||
|
||||
(js/setTimeout
|
||||
(fn []
|
||||
(p/let [other-data (ipc/ipc :get-other-data repo (map :uuid journal-blocks))]
|
||||
(restore-other-data-from-sqlite! repo other-data)))
|
||||
1000)
|
||||
))
|
||||
|
||||
(defn restore-graph!
|
||||
"Restore db from serialized db cache"
|
||||
[repo]
|
||||
(if (string/starts-with? repo config/db-version-prefix)
|
||||
(restore-graph-from-sqlite! repo)
|
||||
(p/let [db-name (datascript-db repo)
|
||||
stored (db-persist/get-serialized-graph db-name)]
|
||||
(restore-graph-from-text! repo stored))))
|
||||
|
||||
(defn restore!
|
||||
[repo]
|
||||
(p/let [_ (restore-graph! repo)]
|
||||
(listen-and-persist! repo)))
|
||||
|
||||
(defn run-batch-txs!
|
||||
[]
|
||||
(let [chan (state/get-db-batch-txs-chan)]
|
||||
(async/go-loop []
|
||||
(let [f (async/<! chan)]
|
||||
(f))
|
||||
(recur))
|
||||
chan))
|
||||
|
||||
(defn new-block-id
|
||||
[]
|
||||
(d/squuid))
|
||||
|
||||
Reference in New Issue
Block a user