浏览代码

fix: initial data overwritten when importing from sqlite db

Tienson Qin 1 年之前
父节点
当前提交
34c632a81c

+ 5 - 1
src/main/frontend/components/imports.cljs

@@ -60,6 +60,7 @@
             all-graphs (->> (state/get-repos)
                             (map #(text-util/get-graph-name-from-path (:url %)))
                             set)]
+        (state/set-state! :graph/importing :logseq)
         (cond
           (string/blank? graph-name)
           (notification/show! "Empty graph name." :error)
@@ -72,7 +73,10 @@
             (set! (.-onload reader)
                   (fn []
                     (let [buffer (.-result ^js reader)]
-                      (import-handler/import-from-sqlite-db! buffer graph-name finished-cb))))
+                      (import-handler/import-from-sqlite-db! buffer graph-name
+                                                             (fn []
+                                                               (state/set-state! :graph/importing nil)
+                                                               (finished-cb))))))
             (set! (.-onerror reader) (fn [e] (js/console.error e)))
             (set! (.-onabort reader) (fn [e]
                                        (prn :debug :aborted)

+ 2 - 3
src/main/frontend/db_worker.cljs

@@ -258,9 +258,8 @@
   (importDb
    [this repo data]
    (when-not (string/blank? repo)
-     (p/let [pool (<get-opfs-pool repo)
-             data (<import-db repo data)]
-       nil))))
+     (p/let [pool (<get-opfs-pool repo)]
+       (<import-db repo data)))))
 
 (defn init
   "web worker entry"

+ 3 - 1
src/main/frontend/handler/import.cljs

@@ -5,6 +5,7 @@
             [frontend.external :as external]
             [frontend.handler.file :as file-handler]
             [frontend.handler.repo :as repo-handler]
+            [frontend.handler.search :as search-handler]
             [frontend.state :as state]
             [frontend.date :as date]
             [frontend.config :as config]
@@ -225,7 +226,8 @@
   (let [graph (str config/db-version-prefix bare-graph-name)]
     (-> (do
           (persist-db/<import-db graph buffer)
-          (repo-handler/new-db! bare-graph-name))
+          (repo-handler/new-db! bare-graph-name {:restore-db? true})
+          (search-handler/rebuild-indices!))
         (p/then
          (fn [_result]
            (finished-ok-handler)))

+ 11 - 8
src/main/frontend/handler/repo.cljs

@@ -529,17 +529,20 @@
   (when (util/electron?)
     (ipc/ipc "graphReady" graph)))
 
-(defn- create-db [full-graph-name]
+(defn- create-db [full-graph-name {:keys [restore-db?]}]
   (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)
+          _ (when restore-db?
+              (restore-and-setup-repo! full-graph-name))
+          _ (when-not restore-db?
+              (db/transact! full-graph-name [(react/kv :db/type "db")
+                                            (react/kv :schema/version db-schema/version {:id -2})])
+              (let [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}])]
@@ -548,11 +551,11 @@
 
 (defn new-db!
   "Handler for creating a new database graph"
-  [graph]
+  [graph & opts]
   (let [full-graph-name (str config/db-version-prefix graph)
         graph-already-exists? (some #(= (:url %) full-graph-name) (state/get-repos))]
     (if graph-already-exists?
       (state/pub-event! [:notification/show
                          {:content (str "The graph '" graph "' already exists. Please try again with another name.")
                           :status :error}])
-      (create-db full-graph-name))))
+      (create-db full-graph-name opts))))