|
|
@@ -290,12 +290,29 @@
|
|
|
db)]
|
|
|
(mapcat
|
|
|
(fn [id]
|
|
|
- (let [title (:block/title (d/entity db id))]
|
|
|
- [[:db/add id :db/ident (db-class/create-user-class-ident-from-name db title)]
|
|
|
- [:db/add id :logseq.property.class/extends :logseq.class/Root]
|
|
|
- [:db/retract id :block/tags :logseq.class/Page]
|
|
|
- [:db/retract id :block/refs :logseq.class/Page]
|
|
|
- [:db/retract id :block/path-refs :logseq.class/Page]]))
|
|
|
+ [[:db/add id :logseq.property.class/extends :logseq.class/Root]
|
|
|
+ [:db/retract id :block/tags :logseq.class/Page]
|
|
|
+ [:db/retract id :block/refs :logseq.class/Page]
|
|
|
+ [:db/retract id :block/path-refs :logseq.class/Page]])
|
|
|
+ class-ids)))
|
|
|
+
|
|
|
+(defn add-missing-db-ident-for-tags2
|
|
|
+ [db]
|
|
|
+ (let [class-ids
|
|
|
+ (d/q
|
|
|
+ '[:find [?b ...]
|
|
|
+ :where
|
|
|
+ [?b :block/tags :logseq.class/Tag]
|
|
|
+ [(missing? $ ?b :db/ident)]]
|
|
|
+ db)]
|
|
|
+ (keep
|
|
|
+ (fn [id]
|
|
|
+ (let [ent (d/entity db id)
|
|
|
+ title (:block/title ent)
|
|
|
+ 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)})))
|
|
|
class-ids)))
|
|
|
|
|
|
(defn fix-using-properties-as-tags
|
|
|
@@ -312,11 +329,30 @@
|
|
|
(map first))]
|
|
|
(mapcat
|
|
|
(fn [id]
|
|
|
- (let [property (d/entity db id)
|
|
|
- title (:block/title property)]
|
|
|
- (into (retract-property-attributes id)
|
|
|
- [[:db/retract id :logseq.property/parent]
|
|
|
- [:db/add id :db/ident (db-class/create-user-class-ident-from-name db title)]])))
|
|
|
+ (into (retract-property-attributes id)
|
|
|
+ [[:db/retract id :logseq.property/parent]]))
|
|
|
+ property-ids)))
|
|
|
+
|
|
|
+(defn fix-using-properties-as-tags2
|
|
|
+ [db]
|
|
|
+ (let [property-ids
|
|
|
+ (->>
|
|
|
+ (d/q
|
|
|
+ '[:find ?b ?i
|
|
|
+ :where
|
|
|
+ [?b :block/tags :logseq.class/Tag]
|
|
|
+ [?b :db/ident ?i]]
|
|
|
+ db)
|
|
|
+ (filter (fn [[_ ident]] (= "user.property" (namespace ident))))
|
|
|
+ (map first))]
|
|
|
+ (keep
|
|
|
+ (fn [id]
|
|
|
+ (let [ent (d/entity db id)
|
|
|
+ title (:block/title ent)
|
|
|
+ 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)})))
|
|
|
property-ids)))
|
|
|
|
|
|
(defn remove-block-order-for-tags
|
|
|
@@ -358,20 +394,17 @@
|
|
|
(def schema-version->updates
|
|
|
"A vec of tuples defining datascript migrations. Each tuple consists of the
|
|
|
schema version integer and a migration map. A migration map can have keys of :properties, :classes
|
|
|
- and :fix."
|
|
|
+ :rename-db-idents and :fix."
|
|
|
[["65.0" {:fix separate-classes-and-properties}]
|
|
|
["65.1" {:fix fix-rename-parent-to-extends}]
|
|
|
["65.2" {:fix fix-tag-properties}]
|
|
|
- ["65.3" {:fix add-missing-db-ident-for-tags}]
|
|
|
- ["65.4" {:fix fix-using-properties-as-tags}]
|
|
|
+ ["65.3" {:rename-db-idents add-missing-db-ident-for-tags2 :fix add-missing-db-ident-for-tags}]
|
|
|
+ ["65.4" {:rename-db-idents fix-using-properties-as-tags2 :fix fix-using-properties-as-tags}]
|
|
|
["65.5" {:fix remove-block-order-for-tags}]
|
|
|
["65.6" {:fix update-extends-to-cardinality-many}]
|
|
|
["65.7" {:fix add-quick-add-page}]
|
|
|
["65.8" {:fix add-missing-page-name}]
|
|
|
- ["65.9" {:properties [:logseq.property.embedding/hnsw-label-updated-at]}]
|
|
|
- ["65.10" {:properties [:logseq.property/test1]}]
|
|
|
- ["65.11" {:rename-db-idents [{:db-ident :logseq.property/test1
|
|
|
- :new-db-ident :logseq.property/test2}]}]])
|
|
|
+ ["65.9" {:properties [:logseq.property.embedding/hnsw-label-updated-at]}]])
|
|
|
|
|
|
(let [[major minor] (last (sort (map (comp (juxt :major :minor) db-schema/parse-schema-version first)
|
|
|
schema-version->updates)))]
|
|
|
@@ -382,14 +415,6 @@
|
|
|
(when (neg? compare-result)
|
|
|
(js/console.warn (str "Current db schema-version is " db-schema/version ", max available schema-version is " max-schema-version))))))
|
|
|
|
|
|
-;;; some validations of schema-version->updates
|
|
|
-(doseq [[version migrate-updates] schema-version->updates]
|
|
|
- (when (contains? (set (keys migrate-updates)) :fix)
|
|
|
- (assert (= 1 (count migrate-updates))
|
|
|
- (common-util/format
|
|
|
- "migration(%s): :fix type cannot coexist with other types (:properties, :classes, :rename-db-idents)"
|
|
|
- version))))
|
|
|
-
|
|
|
(defn ensure-built-in-data-exists!
|
|
|
"Return tx-data"
|
|
|
[conn]
|
|
|
@@ -473,7 +498,8 @@
|
|
|
new-class-idents (keep (fn [class]
|
|
|
(when-let [db-ident (:db/ident class)]
|
|
|
{:db/ident db-ident})) new-classes)
|
|
|
- rename-db-idents-tx-data (rename-db-ident/rename-db-idents-migration-tx-data db rename-db-idents)
|
|
|
+ [rename-db-idents-tx-data rename-db-idents-coll]
|
|
|
+ (rename-db-ident/rename-db-idents-migration-tx-data db rename-db-idents)
|
|
|
fixes (when (fn? fix)
|
|
|
(fix db))
|
|
|
tx-data (if db-based?
|
|
|
@@ -482,18 +508,20 @@
|
|
|
tx-data' (concat
|
|
|
[(sqlite-util/kv :logseq.kv/schema-version version)]
|
|
|
tx-data)
|
|
|
- r (ldb/transact! conn tx-data' {:db-migrate? true})]
|
|
|
+ r (ldb/transact! conn tx-data' {:db-migrate? true})
|
|
|
+ migrate-updates (cond-> migrate-updates
|
|
|
+ (seq rename-db-idents-coll) (assoc :rename-db-idents rename-db-idents-coll))]
|
|
|
(println "DB schema migrated to" version)
|
|
|
(assoc r :migrate-updates migrate-updates)))
|
|
|
|
|
|
(defn migrate
|
|
|
"Migrate 'frontend' datascript schema and data. To add a new migration,
|
|
|
add an entry to schema-version->updates and bump db-schema/version"
|
|
|
- [conn]
|
|
|
+ [conn & {:keys [target-version] :or {target-version db-schema/version}}]
|
|
|
(when (ldb/db-based-graph? @conn)
|
|
|
(let [db @conn
|
|
|
version-in-db (db-schema/parse-schema-version (or (:kv/value (d/entity db :logseq.kv/schema-version)) 0))
|
|
|
- compare-result (db-schema/compare-schema-version db-schema/version version-in-db)]
|
|
|
+ compare-result (db-schema/compare-schema-version target-version version-in-db)]
|
|
|
(cond
|
|
|
(zero? compare-result)
|
|
|
nil
|
|
|
@@ -507,7 +535,7 @@
|
|
|
updates (keep (fn [[v updates]]
|
|
|
(let [v* (db-schema/parse-schema-version v)]
|
|
|
(when (and (neg? (db-schema/compare-schema-version version-in-db v*))
|
|
|
- (not (pos? (db-schema/compare-schema-version v* db-schema/version))))
|
|
|
+ (not (pos? (db-schema/compare-schema-version v* target-version))))
|
|
|
[v updates])))
|
|
|
schema-version->updates)
|
|
|
result-ks [:tx-data :db-before :db-after :migrate-updates]
|
|
|
@@ -519,9 +547,9 @@
|
|
|
(swap! *upgrade-result-coll conj
|
|
|
(select-keys (ensure-built-in-data-exists! conn) result-ks))
|
|
|
{:from-version version-in-db
|
|
|
- :to-version db-schema/version
|
|
|
+ :to-version target-version
|
|
|
:upgrade-result-coll @*upgrade-result-coll})
|
|
|
(catch :default e
|
|
|
- (prn :error (str "DB migration failed to migrate to " db-schema/version " from " version-in-db ":"))
|
|
|
+ (prn :error (str "DB migration failed to migrate to " target-version " from " version-in-db ":"))
|
|
|
(js/console.error e)
|
|
|
(throw e)))))))
|