mirror of https://github.com/logseq/logseq
refactor(migration): add file/path migration
parent
62b4f0a253
commit
d5d3857466
|
@ -160,7 +160,7 @@
|
|||
|
||||
(cond
|
||||
(nil? base)
|
||||
(js/console.log "path join global directory" segments)
|
||||
(js/console.log "path join with nil global directory" segments)
|
||||
(= base "")
|
||||
(js/console.error "BUG: should not join with empty dir" segments)
|
||||
:else
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
(ns logseq.db.schema
|
||||
"Main db schema for the Logseq app")
|
||||
|
||||
(defonce version 1)
|
||||
(defonce version 2)
|
||||
(defonce ast-version 1)
|
||||
;; A page is a special block, a page can corresponds to multiple files with the same ":block/name".
|
||||
(def ^:large-vars/data-var schema
|
||||
|
|
|
@ -69,19 +69,22 @@
|
|||
|
||||
[logseq.db.default built-in-pages-names built-in-pages])
|
||||
|
||||
(defn get-schema-version [db]
|
||||
(d/q
|
||||
'[:find ?v .
|
||||
:where
|
||||
[_ :schema/version ?v]]
|
||||
db))
|
||||
|
||||
(defn old-schema?
|
||||
(defn- old-schema?
|
||||
"Requires migration if the schema version is older than db-schema/version"
|
||||
[db]
|
||||
(let [v (get-schema-version db)]
|
||||
(if (integer? v)
|
||||
(> db-schema/version v)
|
||||
;; backward compatibility
|
||||
(let [v (db-migrate/get-schema-version db)
|
||||
;; backward compatibility
|
||||
v (if (integer? v) v 0)]
|
||||
(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)))
|
||||
|
||||
;; persisting DBs between page reloads
|
||||
|
|
|
@ -1,22 +1,87 @@
|
|||
(ns ^:no-doc frontend.db.migrate
|
||||
(:require [datascript.core :as d]))
|
||||
(ns frontend.db.migrate
|
||||
"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
|
||||
[db]
|
||||
(d/q
|
||||
'[:find [?b ...]
|
||||
:where
|
||||
[?b :block/properties ?properties]
|
||||
[(get ?properties :collapsed) ?collapsed]
|
||||
[(= true ?collapsed)]]
|
||||
db))
|
||||
'[:find [?b ...]
|
||||
:where
|
||||
[?b :block/properties ?properties]
|
||||
[(get ?properties :collapsed) ?collapsed]
|
||||
[(= true ?collapsed)]]
|
||||
db))
|
||||
|
||||
(defn migrate
|
||||
[db]
|
||||
(defn migrate-collapsed-blocks [db]
|
||||
(when db
|
||||
(let [collapsed-blocks (get-collapsed-blocks db)]
|
||||
(if (seq collapsed-blocks)
|
||||
(let [tx-data (map (fn [id] {:db/id id
|
||||
:block/collapsed? true}) collapsed-blocks)]
|
||||
(prn :migrate-collapsed-blocks {:count (count collapsed-blocks)})
|
||||
(d/db-with db tx-data))
|
||||
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