Selaa lähdekoodia

fix: migration

Tienson Qin 4 vuotta sitten
vanhempi
sitoutus
d281d40276

+ 7 - 5
src/main/frontend/components/block.cljs

@@ -354,7 +354,7 @@
                            (state/get-left-sidebar-open?))
                   (ui-handler/close-left-sidebar!)))}
 
-   (if (seq children)
+   (if (and (coll? children) (seq children))
      (for [child children]
        (if (= (first child) "Label")
          (last child)
@@ -1053,8 +1053,10 @@
   [config children collapsed? *ref-collapsed?]
   (let [ref? (:ref? config)
         collapsed? (if ref? (rum/react *ref-collapsed?) collapsed?)
-        children (filter map? children)]
-    (when (and (seq children) (not collapsed?))
+        children (and (coll? children) (filter map? children))]
+    (when (and (coll? children)
+               (seq children)
+               (not collapsed?))
       (let [doc-mode? (:document/mode? config)]
        [:div.block-children {:style {:margin-left (if doc-mode? 12 21)
                                      :display (if collapsed? "none" "")}}
@@ -1074,7 +1076,7 @@
   [state config block uuid block-id body children dummy? collapsed? *ref-collapsed? *control-show?]
   (let [has-child? (and
                     (not (:pre-block? block))
-                    (or (seq children)
+                    (or (and (coll? children) (seq children))
                         (seq body)))
         control-show? (util/react *control-show?)
         ref-collapsed? (util/react *ref-collapsed?)
@@ -1765,7 +1767,7 @@
         has-child? (boolean
                     (and
                      (not pre-block?)
-                     (or (seq children)
+                     (or (and (coll? children) (seq children))
                          (seq body))))
         attrs (on-drag-and-mouse-attrs block uuid top? block-id *move-to-top? has-child? *control-show? doc-mode?)
         data-refs (build-refs-data-value block (remove (set refs) path-refs))

+ 2 - 2
src/main/frontend/db.cljs

@@ -137,7 +137,7 @@
   [{:keys [repos] :as me} old-db-schema restore-config-handler]
   (let [logged? (:name me)
         ;; TODO: switch to use the db version
-        old-db? (and old-db-schema (:block/name old-db-schema))]
+        old-db? (and old-db-schema (not (:block/name old-db-schema)))]
     (doall
      (for [{:keys [url]} repos]
        (let [repo url]
@@ -148,7 +148,7 @@
                  stored (idb/get-item db-name)
                  _ (if stored
                      (let [stored-db (string->db stored)
-                           stored-db (if old-db? (migrate/migrate stored-db) stored-db)
+                           stored-db (if old-db? (migrate/migrate url stored-db) stored-db)
                            attached-db (d/db-with stored-db (concat
                                                              [(me-tx stored-db me)]
                                                              default-db/built-in-pages))]

+ 28 - 13
src/main/frontend/db/migrate.cljs

@@ -1,18 +1,33 @@
 (ns frontend.db.migrate
-  (:require [clojure.walk :as walk]))
+  (:require [clojure.walk :as walk]
+            [datascript.core :as d]
+            [frontend.db-schema :as db-schema]
+            [frontend.state :as state]))
 
 (defonce debug-db (atom nil))
+
+(defn- migrate-attribute
+  [f]
+  (if (and (keyword? f) (= "page" (namespace f)))
+    (let [k (keyword "block" (name f))]
+      (case k
+        :block/ref-pages
+        :block/refs
+        k))
+    f))
+
+(defn with-schema [db new-schema]
+  (let [datoms (->> (d/datoms db :eavt)
+                    (map (fn [d]
+                           (let [a (migrate-attribute (:a d))]
+                             (d/datom (:e d) a (:v d) (:tx d) (:added d))))))]
+    (-> (d/empty-db new-schema)
+       (with-meta (meta db))
+       (d/db-with datoms))))
+
 (defn migrate
-  [db]
-  (prn "migrate")
+  [repo db]
+  (prn "Migrate DB")
   (reset! debug-db db)
-  (walk/postwalk
-   (fn [f]
-     (if (and (keyword? f) (= :page (namespace f)))
-       (let [k (keyword "block" (name f))]
-         (case k
-           :block/ref-pages
-           :block/refs
-           k))
-       f))
-   db))
+  (state/pub-event! [:graph/added repo])
+  (with-schema db db-schema/schema))

+ 4 - 7
src/main/frontend/handler.cljs

@@ -74,8 +74,8 @@
                      (js/clearInterval @interval)
                      (reset! interval nil)
                      (-> (p/all (db/restore!
-                                 old-db-schema
                                  (assoc me :repos repos)
+                                 old-db-schema
                                  (fn [repo]
                                    (file-handler/restore-config! repo false)
                                    (ui-handler/add-style-if-exists!))))
@@ -90,16 +90,13 @@
 
                               :else
                               (state/set-db-restoring! false))
-                            (if false   ; FIXME: incompatible changes
-                              (notification/show!
-                               [:p "Database schema changed, please backup your notes first, and re-index/re-link your graphs."]
-                               :warning
-                               false)
-                              (store-schema!))
+
+                            (store-schema!)
 
                             (state/pub-event! [:modal/nfs-ask-permission])
 
                             (page-handler/init-commands!)
+
                             (when (seq (:repos me))
                               ;; FIXME: handle error
                               (common-handler/request-app-tokens!

+ 2 - 0
src/main/frontend/handler/migrate.cljs

@@ -34,6 +34,8 @@
   [repo]
   (notification/show!
    [:div
+    [:h1 (str "Graph: " repo)]
+
     [:p "Previously Logseq uses `#` Markdown heading as outliner bullets, since beta, we've changed to use more standard `-` unordered list as outliner bullets."]
 
     [:p.mt-2 "If you've converted this graph before, click on this button so that you won't see this notification again."]