mirror of https://github.com/logseq/logseq
refactor(migration): add file/path migration
parent
62b4f0a253
commit
d5d3857466
|
@ -160,7 +160,7 @@
|
||||||
|
|
||||||
(cond
|
(cond
|
||||||
(nil? base)
|
(nil? base)
|
||||||
(js/console.log "path join global directory" segments)
|
(js/console.log "path join with nil global directory" segments)
|
||||||
(= base "")
|
(= base "")
|
||||||
(js/console.error "BUG: should not join with empty dir" segments)
|
(js/console.error "BUG: should not join with empty dir" segments)
|
||||||
:else
|
:else
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(ns logseq.db.schema
|
(ns logseq.db.schema
|
||||||
"Main db schema for the Logseq app")
|
"Main db schema for the Logseq app")
|
||||||
|
|
||||||
(defonce version 1)
|
(defonce version 2)
|
||||||
(defonce ast-version 1)
|
(defonce ast-version 1)
|
||||||
;; A page is a special block, a page can corresponds to multiple files with the same ":block/name".
|
;; A page is a special block, a page can corresponds to multiple files with the same ":block/name".
|
||||||
(def ^:large-vars/data-var schema
|
(def ^:large-vars/data-var schema
|
||||||
|
|
|
@ -69,19 +69,22 @@
|
||||||
|
|
||||||
[logseq.db.default built-in-pages-names built-in-pages])
|
[logseq.db.default built-in-pages-names built-in-pages])
|
||||||
|
|
||||||
(defn get-schema-version [db]
|
(defn- old-schema?
|
||||||
(d/q
|
"Requires migration if the schema version is older than db-schema/version"
|
||||||
'[:find ?v .
|
|
||||||
:where
|
|
||||||
[_ :schema/version ?v]]
|
|
||||||
db))
|
|
||||||
|
|
||||||
(defn old-schema?
|
|
||||||
[db]
|
[db]
|
||||||
(let [v (get-schema-version db)]
|
(let [v (db-migrate/get-schema-version db)
|
||||||
(if (integer? v)
|
;; backward compatibility
|
||||||
(> db-schema/version v)
|
v (if (integer? v) v 0)]
|
||||||
;; backward compatibility
|
(cond
|
||||||
|
(= db-schema/version v)
|
||||||
|
false
|
||||||
|
|
||||||
|
(< db-schema/version v)
|
||||||
|
(do
|
||||||
|
(js/console.error "DB schema version is newer than the app, please update the app. " ":db-version" v)
|
||||||
|
false)
|
||||||
|
|
||||||
|
:else
|
||||||
true)))
|
true)))
|
||||||
|
|
||||||
;; persisting DBs between page reloads
|
;; persisting DBs between page reloads
|
||||||
|
|
|
@ -1,22 +1,87 @@
|
||||||
(ns ^:no-doc frontend.db.migrate
|
(ns frontend.db.migrate
|
||||||
(:require [datascript.core :as d]))
|
"Do DB migration, in a version-by-version style.
|
||||||
|
|
||||||
|
`:schema/version` is not touched"
|
||||||
|
(:require [clojure.string :as string]
|
||||||
|
[datascript.core :as d]
|
||||||
|
[frontend.config :as config]
|
||||||
|
[frontend.state :as state]
|
||||||
|
[logseq.common.path :as path]
|
||||||
|
[logseq.db.schema :as db-schema]))
|
||||||
|
|
||||||
|
|
||||||
|
(defn get-schema-version
|
||||||
|
"Get schema version from db, the current version is defined in db-schema/version"
|
||||||
|
[db]
|
||||||
|
(d/q
|
||||||
|
'[:find ?v .
|
||||||
|
:where
|
||||||
|
[_ :schema/version ?v]]
|
||||||
|
db))
|
||||||
|
|
||||||
(defn get-collapsed-blocks
|
(defn get-collapsed-blocks
|
||||||
[db]
|
[db]
|
||||||
(d/q
|
(d/q
|
||||||
'[:find [?b ...]
|
'[:find [?b ...]
|
||||||
:where
|
:where
|
||||||
[?b :block/properties ?properties]
|
[?b :block/properties ?properties]
|
||||||
[(get ?properties :collapsed) ?collapsed]
|
[(get ?properties :collapsed) ?collapsed]
|
||||||
[(= true ?collapsed)]]
|
[(= true ?collapsed)]]
|
||||||
db))
|
db))
|
||||||
|
|
||||||
(defn migrate
|
(defn migrate-collapsed-blocks [db]
|
||||||
[db]
|
|
||||||
(when db
|
(when db
|
||||||
(let [collapsed-blocks (get-collapsed-blocks db)]
|
(let [collapsed-blocks (get-collapsed-blocks db)]
|
||||||
(if (seq collapsed-blocks)
|
(if (seq collapsed-blocks)
|
||||||
(let [tx-data (map (fn [id] {:db/id id
|
(let [tx-data (map (fn [id] {:db/id id
|
||||||
:block/collapsed? true}) collapsed-blocks)]
|
:block/collapsed? true}) collapsed-blocks)]
|
||||||
|
(prn :migrate-collapsed-blocks {:count (count collapsed-blocks)})
|
||||||
(d/db-with db tx-data))
|
(d/db-with db tx-data))
|
||||||
db))))
|
db))))
|
||||||
|
|
||||||
|
(defn migrate-absolute-file-path-to-relative [db]
|
||||||
|
(when db
|
||||||
|
(let [all-files (d/q
|
||||||
|
'[:find [(pull ?b [:db/id :file/path]) ...]
|
||||||
|
:where
|
||||||
|
[?b :file/path]]
|
||||||
|
db)
|
||||||
|
repo-dir (config/get-repo-dir (state/get-current-repo))]
|
||||||
|
(if (seq all-files)
|
||||||
|
(let [tx-data (->> all-files
|
||||||
|
(filter (fn [db-file]
|
||||||
|
(and (path/absolute? (:file/path db-file))
|
||||||
|
(string/starts-with? (:file/path db-file) repo-dir))))
|
||||||
|
(mapv (fn [db-file] {:db/id (:db/id db-file)
|
||||||
|
:file/path (path/trim-dir-prefix repo-dir (:file/path db-file))})))]
|
||||||
|
(when tx-data
|
||||||
|
(state/pub-event! [:notification/show
|
||||||
|
{:content [:div "Migrated from an old version of DB, please re-index the graph from the graph list dropdown."]
|
||||||
|
:status :warning
|
||||||
|
:clear? false}]))
|
||||||
|
(prn :migrate-absolute-file-path-to-relative {:count (count tx-data)})
|
||||||
|
(d/db-with db tx-data))
|
||||||
|
db))))
|
||||||
|
|
||||||
|
|
||||||
|
(defmulti do-migration get-schema-version)
|
||||||
|
|
||||||
|
(defmethod do-migration 0
|
||||||
|
[db]
|
||||||
|
(-> db
|
||||||
|
migrate-collapsed-blocks
|
||||||
|
migrate-absolute-file-path-to-relative))
|
||||||
|
|
||||||
|
(defmethod do-migration 1
|
||||||
|
[db]
|
||||||
|
(-> db
|
||||||
|
migrate-absolute-file-path-to-relative))
|
||||||
|
|
||||||
|
(defmethod do-migration :default
|
||||||
|
[db]
|
||||||
|
db)
|
||||||
|
|
||||||
|
(defn migrate
|
||||||
|
[db]
|
||||||
|
(prn ::migrate {:from (get-schema-version db) :to db-schema/version})
|
||||||
|
(do-migration db))
|
||||||
|
|
Loading…
Reference in New Issue