1
0
Эх сурвалжийг харах

Support db migration for :block/type update

Tienson Qin 1 жил өмнө
parent
commit
5d2cd70142

+ 1 - 1
deps/db/src/logseq/db/frontend/schema.cljs

@@ -2,7 +2,7 @@
   "Main datascript schemas for the Logseq app"
   (:require [clojure.set :as set]))
 
-(def version 11)
+(def version 12)
 ;; A page is a special block, a page can corresponds to multiple files with the same ":block/name".
 (def ^:large-vars/data-var schema
   {:db/ident        {:db/unique :db.unique/identity}

+ 1 - 1
src/main/frontend/components/block.cljs

@@ -782,7 +782,7 @@
   "Component for a page. `page` argument contains :block/name which can be (un)sanitized page name.
    Keys for `config`:
    - `:preview?`: Is this component under preview mode? (If true, `page-preview-trigger` won't be registered to this `page-cp`)"
-  [state {:keys [label children preview? disable-preview?] :as config} _page]
+  [state {:keys [label children preview? disable-preview?] :as config} page]
   (let [entity (::entity state)]
     (when-let [entity (when entity (db/sub-block (:db/id entity)))]
       (if (or (ldb/page? entity) (:block/tags entity))

+ 34 - 1
src/main/frontend/worker/db/migrate.cljs

@@ -92,6 +92,38 @@
                               [:db/add id new prop-value]]))))
             old-new-props)))
 
+(defn- update-block-type-many->one
+  [conn _search-db]
+  (let [db @conn
+        datoms (d/datoms db :avet :block/type)
+        new-type-tx (->> (set (map :e datoms))
+                         (mapcat
+                          (fn [id]
+                            (let [types (:block/type (d/entity db id))
+                                  type (if (set? types)
+                                         (cond
+                                           (contains? types "class")
+                                           "tag"
+                                           (contains? types "property")
+                                           "property"
+                                           (contains? types "whiteboard")
+                                           "whiteboard"
+                                           (contains? types "journal")
+                                           "journal"
+                                           (contains? types "hidden")
+                                           "hidden"
+                                           (contains? types "page")
+                                           "page"
+                                           :else
+                                           (first types))
+                                         types)]
+                              [[:db/retract id :block/type]
+                               [:db/add id :block/type type]]))))
+        schema (:schema db)]
+    (ldb/transact! conn new-type-tx {:db-migrate? true})
+    (d/reset-schema! conn (update schema :block/type #(assoc % :db/cardinality :db.cardinality/one)))
+    []))
+
 (def schema-version->updates
   [[3 {:properties [:logseq.property/table-sorting :logseq.property/table-filters
                     :logseq.property/table-hidden-columns :logseq.property/table-ordered-columns]
@@ -111,7 +143,8 @@
    [8 {:fix replace-object-and-page-type-with-node}]
    [9 {:fix update-task-ident}]
    [10 {:fix update-table-properties}]
-   [11 {:fix property-checkbox-type-non-ref}]])
+   [11 {:fix property-checkbox-type-non-ref}]
+   [12 {:fix update-block-type-many->one}]])
 
 (let [max-schema-version (apply max (map first schema-version->updates))]
   (assert (<= db-schema/version max-schema-version))