Просмотр исходного кода

enhance: validate renamed db-idents not random and class entities when migrating

rcmerci 1 месяц назад
Родитель
Сommit
b12b3f7220

+ 7 - 0
deps/db/src/logseq/db/frontend/db_ident.cljc

@@ -89,3 +89,10 @@
                "-"
                (rand-nth non-int-char-range)
                (nano-id 7))))))
+
+(defn replace-db-ident-random-suffix
+  [db-ident-kw new-suffix]
+  (assert (keyword? db-ident-kw))
+  (assert (and (string? new-suffix) (= 8 (count new-suffix))))
+  (keyword (namespace db-ident-kw)
+           (string/replace-first (name db-ident-kw) #"-.{8}$" (str "-" new-suffix))))

+ 7 - 2
src/main/frontend/worker/db/migrate.cljs

@@ -11,6 +11,7 @@
             [logseq.db :as ldb]
             [logseq.db.common.order :as db-order]
             [logseq.db.frontend.class :as db-class]
+            [logseq.db.frontend.db-ident :as db-ident]
             [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.schema :as db-schema]
             [logseq.db.sqlite.create-graph :as sqlite-create-graph]
@@ -312,7 +313,9 @@
              block-uuid (:block/uuid ent)]
          (when block-uuid
            {:db-ident-or-block-uuid block-uuid
-            :new-db-ident (db-class/create-user-class-ident-from-name db title)})))
+            :new-db-ident (db-ident/replace-db-ident-random-suffix
+                           (db-class/create-user-class-ident-from-name db title)
+                           (subs (str block-uuid) 28))})))
      class-ids)))
 
 (defn fix-using-properties-as-tags
@@ -352,7 +355,9 @@
              block-uuid (:block/uuid ent)]
          (when block-uuid
            {:db-ident-or-block-uuid block-uuid
-            :new-db-ident (db-class/create-user-class-ident-from-name db title)})))
+            :new-db-ident (db-ident/replace-db-ident-random-suffix
+                           (db-class/create-user-class-ident-from-name db title)
+                           (subs (str block-uuid) 28))})))
      property-ids)))
 
 (defn remove-block-order-for-tags

+ 11 - 2
src/main/frontend/worker/db/rename_db_ident.cljs

@@ -1,6 +1,7 @@
 (ns frontend.worker.db.rename-db-ident
   "utils for rename-db-idents migration"
-  (:require [datascript.core :as d]))
+  (:require [datascript.core :as d]
+            [logseq.db :as ldb]))
 
 (defn rename-db-idents-migration-tx-data
   "Rename :db/ident and replace all usages as well.
@@ -9,6 +10,11 @@
   [db rename-db-idents]
   (assert (fn? rename-db-idents))
   (let [rename-db-idents-coll (rename-db-idents db)
+        rename-db-idents-coll2  (rename-db-idents db)
+        ;; ensure there's no random data in result
+        _ (when (not= rename-db-idents-coll rename-db-idents-coll2)
+            (throw (ex-info "db-idents cannot be randomly generated"
+                            {:rename-db-idents-colls [rename-db-idents-coll rename-db-idents-coll2]})))
         *rename-db-idents-coll (atom [])
         tx-data
         (->> (for [{:keys [db-ident-or-block-uuid new-db-ident] :as rename-db-ident} rename-db-idents-coll
@@ -16,7 +22,10 @@
                                             db-ident-or-block-uuid
                                             [:block/uuid db-ident-or-block-uuid]))
                          old-db-ident (:db/ident ent)]]
-               (do (when (some? ent) (swap! *rename-db-idents-coll conj rename-db-ident))
+               (do (when (some? ent)
+                     (when-not (ldb/class? ent)
+                       (throw (ex-info "Only entities of class type support :rename-db-ident" {:ent (into {} ent)})))
+                     (swap! *rename-db-idents-coll conj rename-db-ident))
                    (cons {:db/id (:db/id ent) :db/ident new-db-ident}
                          (some->> old-db-ident
                                   (d/q '[:find ?b ?v :in $ ?a :where [?b ?a ?v]] db)