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)
(.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]]
(let [dir (utils/get-graph-dir graph)]
(win/graph-has-other-windows? win dir)))

View File

@ -77,11 +77,6 @@
comp [:div (str payload)]]
(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"
(fn [_data]
(prn "Rebuild search indices")

View File

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

View File

@ -127,7 +127,7 @@
blocks** (outliner-pipeline/build-upsert-blocks blocks* nil db)]
(<? (p->c (persist-db/<new repo)))
;; 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)))))

View File

@ -116,21 +116,7 @@
(when-not (= repo r)
(close-db! r db))))
(defn- create-or-open-db!
[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]
(defn- iter->vec [iter]
(when iter
(p/loop [acc []]
(p/let [elem (.next iter)]
@ -157,6 +143,20 @@
(rest 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
(defn <remove-all-files!
"!! Dangerous: use it only for development."
@ -211,8 +211,8 @@
(createOrOpenDB
[_this repo]
(close-other-dbs! repo)
(create-or-open-db! repo))
(p/let [_ (close-other-dbs! repo)]
(create-or-open-db! repo)))
(transact
[_this repo tx-data tx-meta]

View File

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

View File

@ -16,6 +16,7 @@
[frontend.handler.common.file :as file-common-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.notification :as notification]
[frontend.handler.global-config :as global-config-handler]
[frontend.idb :as idb]
[frontend.search :as search]
@ -344,16 +345,14 @@
[{:keys [url] :as repo}]
(let [db-based? (config/db-based-graph? url)
delete-db-f (fn []
(let [graph-exists? (db/get-db url)
current-repo (state/get-current-repo)]
(let [current-repo (state/get-current-repo)]
(db/remove-conn! url)
(db-persist/delete-graph! url)
(search/remove-db! url)
(state/delete-repo! repo)
(when graph-exists? (ipc/ipc "graphUnlinked" repo))
(when (= current-repo url)
(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)
db-based?
(config/demo-graph? url))
@ -532,21 +531,25 @@
(ipc/ipc "graphReady" graph)))
(defn- create-db [full-graph-name]
(p/let [_ (persist-db/<new full-graph-name)
_ (op-mem-layer/<init-load-from-indexeddb! full-graph-name)
_ (start-repo-db-if-not-exists! full-graph-name)
_ (state/add-repo! {:url full-graph-name})
_ (route-handler/redirect-to-home!)
_ (db/transact! full-graph-name [(react/kv :db/type "db")
(react/kv :schema/version db-schema/version {:id -2})])
initial-data (sqlite-create-graph/build-db-initial-data config/config-default-content)
_ (db/transact! full-graph-name initial-data)
_ (repo-config-handler/set-repo-config-state! full-graph-name config/config-default-content)
(->
(p/let [_ (persist-db/<new full-graph-name)
_ (op-mem-layer/<init-load-from-indexeddb! full-graph-name)
_ (start-repo-db-if-not-exists! full-graph-name)
_ (state/add-repo! {:url full-graph-name})
_ (route-handler/redirect-to-home!)
_ (db/transact! full-graph-name [(react/kv :db/type "db")
(react/kv :schema/version db-schema/version {:id -2})])
initial-data (sqlite-create-graph/build-db-initial-data config/config-default-content)
_ (db/transact! full-graph-name initial-data)
_ (repo-config-handler/set-repo-config-state! full-graph-name config/config-default-content)
;; TODO: handle global graph
_ (state/pub-event! [:init/commands])
_ (state/pub-event! [:page/create (date/today) {:redirect? false}])]
(js/setTimeout ui-handler/re-render-root! 100)
(prn "New db created: " full-graph-name)))
_ (state/pub-event! [:init/commands])
_ (state/pub-event! [:page/create (date/today) {:redirect? false}])]
(js/setTimeout ui-handler/re-render-root! 100)
(prn "New db created: " full-graph-name))
(p/catch (fn [error]
(notification/show! "Create graph failed." :error)
(js/console.error error)))))
(defn new-db!
"Handler for creating a new database graph"

View File

@ -119,8 +119,7 @@
(db/transact! repo update-tx-ids {:replace? true
:update-tx-ids? true}))
(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?
(react/refresh! repo tx-report'))

View File

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

View File

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