fix: save db to disk when remove a graph

pull/10639/head
Tienson Qin 2023-12-13 01:34:45 +08:00
parent 9b388f52f0
commit 436ff54039
10 changed files with 65 additions and 83 deletions

View File

@ -579,10 +579,6 @@
(logger/info ::quick-and-install) (logger/info ::quick-and-install)
(.quitAndInstall autoUpdater)) (.quitAndInstall autoUpdater))
(defmethod handle :graphUnlinked [^js _win [_ repo]]
(doseq [window (win/get-all-windows)]
(utils/send-to-renderer window "graphUnlinked" (bean/->clj repo))))
(defmethod handle :graphHasOtherWindow [^js win [_ graph]] (defmethod handle :graphHasOtherWindow [^js win [_ graph]]
(let [dir (utils/get-graph-dir graph)] (let [dir (utils/get-graph-dir graph)]
(win/graph-has-other-windows? win dir))) (win/graph-has-other-windows? win dir)))

View File

@ -77,11 +77,6 @@
comp [:div (str payload)]] comp [:div (str payload)]]
(notification/show! comp type false)))) (notification/show! comp type false))))
(safe-api-call "graphUnlinked"
(fn [data]
(let [repo (bean/->clj data)]
(repo-handler/remove-repo! repo))))
(safe-api-call "rebuildSearchIndice" (safe-api-call "rebuildSearchIndice"
(fn [_data] (fn [_data]
(prn "Rebuild search indices") (prn "Rebuild search indices")

View File

@ -39,10 +39,11 @@
[graph] [graph]
(let [key (db-conn/datascript-db graph) (let [key (db-conn/datascript-db graph)
db-based? (config/db-based-graph? graph)] db-based? (config/db-based-graph? graph)]
(persist-db/<unsafe-delete graph) (p/let [_ (persist-db/<export-db graph {})
_ (persist-db/<unsafe-delete graph)]
(if (util/electron?) (if (util/electron?)
(ipc/ipc "deleteGraph" graph key db-based?) (ipc/ipc "deleteGraph" graph key db-based?)
(idb/remove-item! key)))) (idb/remove-item! key)))))
(defn rename-graph! (defn rename-graph!
[old-repo new-repo] [old-repo new-repo]

View File

@ -127,7 +127,7 @@
blocks** (outliner-pipeline/build-upsert-blocks blocks* nil db)] blocks** (outliner-pipeline/build-upsert-blocks blocks* nil db)]
(<? (p->c (persist-db/<new repo))) (<? (p->c (persist-db/<new repo)))
;; FIXME: pass tx-data tx-meta instead ;; FIXME: pass tx-data tx-meta instead
(<? (persist-db/<transact-data repo blocks** nil)) (<? (p->c (persist-db/<transact-data repo blocks** nil)))
(op-mem-layer/update-local-tx! repo t))))) (op-mem-layer/update-local-tx! repo t)))))

View File

@ -116,21 +116,7 @@
(when-not (= repo r) (when-not (= repo r)
(close-db! r db)))) (close-db! r db))))
(defn- create-or-open-db! (defn- iter->vec [iter]
[repo]
(when-not (get-sqlite-conn repo)
(p/let [pool (<get-opfs-pool repo)
db (new (.-OpfsSAHPoolDb pool) (get-repo-path repo))
storage (new-sqlite-storage repo {})]
(swap! *sqlite-conns assoc repo db)
(.exec db "PRAGMA locking_mode=exclusive")
(.exec db "create table if not exists kvs (addr INTEGER primary key, content TEXT)")
(let [conn (or (d/restore-conn storage)
(d/create-conn db-schema/schema-for-db-based-graph {:storage storage}))]
(swap! *datascript-conns assoc repo conn)
nil))))
(defn iter->vec [iter]
(when iter (when iter
(p/loop [acc []] (p/loop [acc []]
(p/let [elem (.next iter)] (p/let [elem (.next iter)]
@ -157,6 +143,20 @@
(rest dirs))] (rest dirs))]
(p/recur result dirs))))))) (p/recur result dirs)))))))
(defn- create-or-open-db!
[repo]
(when-not (get-sqlite-conn repo)
(p/let [^js pool (<get-opfs-pool repo)
db (new (.-OpfsSAHPoolDb pool) (get-repo-path repo))
storage (new-sqlite-storage repo {})]
(swap! *sqlite-conns assoc repo db)
(.exec db "PRAGMA locking_mode=exclusive")
(.exec db "create table if not exists kvs (addr INTEGER primary key, content TEXT)")
(let [conn (or (d/restore-conn storage)
(d/create-conn db-schema/schema-for-db-based-graph {:storage storage}))]
(swap! *datascript-conns assoc repo conn)
nil))))
(comment (comment
(defn <remove-all-files! (defn <remove-all-files!
"!! Dangerous: use it only for development." "!! Dangerous: use it only for development."
@ -211,8 +211,8 @@
(createOrOpenDB (createOrOpenDB
[_this repo] [_this repo]
(close-other-dbs! repo) (p/let [_ (close-other-dbs! repo)]
(create-or-open-db! repo)) (create-or-open-db! repo)))
(transact (transact
[_this repo tx-data tx-meta] [_this repo tx-data tx-meta]

View File

@ -181,7 +181,8 @@
"Logic for keeping db sync when switching graphs "Logic for keeping db sync when switching graphs
Only works for electron Only works for electron
graph: the target graph to switch to" graph: the target graph to switch to"
[graph {:keys [persist?]}] [graph {:keys [persist?]
:or {persist? true}}]
(let [current-repo (state/get-current-repo)] (let [current-repo (state/get-current-repo)]
(p/do! (p/do!
(when persist? (when persist?
@ -201,13 +202,12 @@
state/set-state! :sync-graph/init? false))) state/set-state! :sync-graph/init? false)))
(defmethod handle :graph/switch [[_ graph opts]] (defmethod handle :graph/switch [[_ graph opts]]
(let [opts (if (false? (:persist? opts)) opts (assoc opts :persist? true))]
(if (or (not (false? (get @outliner-file/*writes-finished? graph))) (if (or (not (false? (get @outliner-file/*writes-finished? graph)))
(:sync-graph/init? @state/state)) (:sync-graph/init? @state/state))
(graph-switch-on-persisted graph opts) (graph-switch-on-persisted graph opts)
(notification/show! (notification/show!
"Please wait seconds until all changes are saved for the current graph." "Please wait seconds until all changes are saved for the current graph."
:warning)))) :warning)))
(defmethod handle :graph/pull-down-remote-graph [[_ graph dir-name]] (defmethod handle :graph/pull-down-remote-graph [[_ graph dir-name]]
(if (mobile-util/native-ios?) (if (mobile-util/native-ios?)

View File

@ -16,6 +16,7 @@
[frontend.handler.common.file :as file-common-handler] [frontend.handler.common.file :as file-common-handler]
[frontend.handler.route :as route-handler] [frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler] [frontend.handler.ui :as ui-handler]
[frontend.handler.notification :as notification]
[frontend.handler.global-config :as global-config-handler] [frontend.handler.global-config :as global-config-handler]
[frontend.idb :as idb] [frontend.idb :as idb]
[frontend.search :as search] [frontend.search :as search]
@ -344,16 +345,14 @@
[{:keys [url] :as repo}] [{:keys [url] :as repo}]
(let [db-based? (config/db-based-graph? url) (let [db-based? (config/db-based-graph? url)
delete-db-f (fn [] delete-db-f (fn []
(let [graph-exists? (db/get-db url) (let [current-repo (state/get-current-repo)]
current-repo (state/get-current-repo)]
(db/remove-conn! url) (db/remove-conn! url)
(db-persist/delete-graph! url) (db-persist/delete-graph! url)
(search/remove-db! url) (search/remove-db! url)
(state/delete-repo! repo) (state/delete-repo! repo)
(when graph-exists? (ipc/ipc "graphUnlinked" repo))
(when (= current-repo url) (when (= current-repo url)
(when-let [graph (:url (first (state/get-repos)))] (when-let [graph (:url (first (state/get-repos)))]
(state/pub-event! [:graph/switch graph {}])))))] (state/pub-event! [:graph/switch graph {:persist? false}])))))]
(when (or (config/local-file-based-graph? url) (when (or (config/local-file-based-graph? url)
db-based? db-based?
(config/demo-graph? url)) (config/demo-graph? url))
@ -532,6 +531,7 @@
(ipc/ipc "graphReady" graph))) (ipc/ipc "graphReady" graph)))
(defn- create-db [full-graph-name] (defn- create-db [full-graph-name]
(->
(p/let [_ (persist-db/<new full-graph-name) (p/let [_ (persist-db/<new full-graph-name)
_ (op-mem-layer/<init-load-from-indexeddb! full-graph-name) _ (op-mem-layer/<init-load-from-indexeddb! full-graph-name)
_ (start-repo-db-if-not-exists! full-graph-name) _ (start-repo-db-if-not-exists! full-graph-name)
@ -546,7 +546,10 @@
_ (state/pub-event! [:init/commands]) _ (state/pub-event! [:init/commands])
_ (state/pub-event! [:page/create (date/today) {:redirect? false}])] _ (state/pub-event! [:page/create (date/today) {:redirect? false}])]
(js/setTimeout ui-handler/re-render-root! 100) (js/setTimeout ui-handler/re-render-root! 100)
(prn "New db created: " full-graph-name))) (prn "New db created: " full-graph-name))
(p/catch (fn [error]
(notification/show! "Create graph failed." :error)
(js/console.error error)))))
(defn new-db! (defn new-db!
"Handler for creating a new database graph" "Handler for creating a new database graph"

View File

@ -119,8 +119,7 @@
(db/transact! repo update-tx-ids {:replace? true (db/transact! repo update-tx-ids {:replace? true
:update-tx-ids? true})) :update-tx-ids? true}))
(when (and (config/db-based-graph? repo) (not config/publishing?)) (when (and (config/db-based-graph? repo) (not config/publishing?))
(go (persist-db/<transact-data repo (:tx-data tx-report) (:tx-meta tx-report))))
(<! (persist-db/<transact-data repo (:tx-data tx-report) (:tx-meta tx-report))))))
(when-not importing? (when-not importing?
(react/refresh! repo tx-report')) (react/refresh! repo tx-report'))

View File

@ -2,10 +2,7 @@
"Backend of DB based graph" "Backend of DB based graph"
(:require [frontend.persist-db.browser :as browser] (:require [frontend.persist-db.browser :as browser]
[frontend.persist-db.protocol :as protocol] [frontend.persist-db.protocol :as protocol]
[promesa.core :as p] [promesa.core :as p]))
[frontend.state :as state]
[frontend.util :as util]
[frontend.config :as config]))
(defonce opfs-db (browser/->InBrowser)) (defonce opfs-db (browser/->InBrowser))
@ -43,7 +40,5 @@
;; @shuyu Do we still need this? ;; @shuyu Do we still need this?
(defn <new [repo] (defn <new [repo]
{:pre [(<= (count repo) 56)]} {:pre [(<= (count repo) 56)]}
(p/do! (p/let [_ (protocol/<new (get-impl) repo)]
(let [current-repo (state/get-current-repo)] (<export-db repo {})))
(<export-db current-repo {})
(protocol/<new (get-impl) repo))))

View File

@ -3,7 +3,6 @@
This interface uses clj data format as input." This interface uses clj data format as input."
(:require ["comlink" :as Comlink] (:require ["comlink" :as Comlink]
[cljs.core.async.interop :refer [p->c]]
[frontend.persist-db.protocol :as protocol] [frontend.persist-db.protocol :as protocol]
[frontend.config :as config] [frontend.config :as config]
[promesa.core :as p] [promesa.core :as p]
@ -53,14 +52,8 @@
(defrecord InBrowser [] (defrecord InBrowser []
protocol/PersistentDB protocol/PersistentDB
(<new [_this repo] (<new [_this repo]
(let [^js sqlite @*sqlite] (when-let [^js sqlite @*sqlite]
(-> (.createOrOpenDB sqlite repo) (.createOrOpenDB sqlite repo)))
(p/then (fn [_result]
(prn "SQLite db created or opened successfully: " repo)))
(p/catch (fn [error]
(js/console.error error)
(notification/show! [:div (str "SQLiteDB creation error: " error)] :error)
nil)))))
(<list-db [_this] (<list-db [_this]
(when-let [^js sqlite @*sqlite] (when-let [^js sqlite @*sqlite]
@ -79,10 +72,9 @@
(.unsafeUnlinkDB sqlite repo))) (.unsafeUnlinkDB sqlite repo)))
(<transact-data [_this repo tx-data tx-meta] (<transact-data [_this repo tx-data tx-meta]
(when-let [^js sqlite @*sqlite] (let [^js sqlite @*sqlite]
(p->c (p/let [_ (when sqlite (.transact sqlite repo (pr-str tx-data) (pr-str tx-meta)))]
(p/let [_ (.transact sqlite repo (pr-str tx-data) (pr-str tx-meta))] nil)))
nil))))
(<fetch-initial-data [_this repo _opts] (<fetch-initial-data [_this repo _opts]
(when-let [^js sqlite @*sqlite] (when-let [^js sqlite @*sqlite]
@ -96,9 +88,10 @@
(<export-db [_this repo opts] (<export-db [_this repo opts]
(when-let [^js sqlite @*sqlite] (when-let [^js sqlite @*sqlite]
(-> (p/let [data (.exportDB sqlite repo)] (-> (p/let [data (.exportDB sqlite repo)]
(when data
(if (:return-data? opts) (if (:return-data? opts)
data data
(<export-db! repo data))) (<export-db! repo data))))
(p/catch (fn [error] (p/catch (fn [error]
(prn :debug :save-db-error repo) (prn :debug :save-db-error repo)
(js/console.error error) (js/console.error error)