fix: ensure local graph uuid

This commit is contained in:
Tienson Qin
2026-05-27 07:24:52 +08:00
parent 969f0b4037
commit d21906dd88
2 changed files with 64 additions and 7 deletions

View File

@@ -11,6 +11,7 @@
[frontend.util :as util]
[logseq.common.config :as common-config]
[logseq.common.graph-registry :as graph-registry]
[logseq.common.uuid :as common-uuid]
[logseq.db :as ldb]
[promesa.core :as p]))
@@ -96,6 +97,20 @@
(ldb/get-graph-local-uuid db*))
str))))
(defn- new-local-graph-uuid
[]
(uuid (str "00000000" (subs (str (common-uuid/gen-uuid)) 8))))
(defn- <ensure-local-graph-uuid!
[repo db*]
(if-let [local-graph-uuid (ldb/get-graph-local-uuid db*)]
(p/resolved local-graph-uuid)
(let [local-graph-uuid (new-local-graph-uuid)]
(p/let [_ (db/transact! repo
[(ldb/kv :logseq.kv/local-graph-uuid local-graph-uuid)]
{:graph-open/ensure-local-graph-uuid? true})]
local-graph-uuid))))
(defn remember-current-graph-id-in-tab!
[]
(when-let [repo (state/get-current-repo)]
@@ -106,13 +121,15 @@
[]
(when-let [repo (state/get-current-repo)]
(when-let [db* (db/get-db repo)]
(<upsert-graph-registry-entry!
{:repo repo
:graph-name (common-config/strip-leading-db-version-prefix repo)
:local-graph-id (some-> (ldb/get-graph-local-uuid db*) str)
:graph-id (some-> (or (ldb/get-graph-rtc-uuid db*)
(ldb/get-graph-local-uuid db*))
str)}))))
(p/let [local-graph-uuid (<ensure-local-graph-uuid! repo db*)
db* (or (db/get-db repo) db*)
graph-uuid (or (ldb/get-graph-rtc-uuid db*)
local-graph-uuid)]
(<upsert-graph-registry-entry!
{:repo repo
:graph-name (common-config/strip-leading-db-version-prefix repo)
:local-graph-id (str local-graph-uuid)
:graph-id (some-> graph-uuid str)})))))
(defn settle-metadata-to-local!
[m]

View File

@@ -1,10 +1,14 @@
(ns frontend.handler.graph-test
(:require [cljs.test :refer [async deftest is testing]]
[datascript.core :as d]
[frontend.db :as db]
[frontend.common.idb :as idb]
[frontend.handler.graph]
[frontend.state :as state]
[frontend.util.url :as url-util]
[logseq.common.graph-registry :as graph-registry]
[logseq.db :as ldb]
[logseq.db.frontend.schema :as db-schema]
[promesa.core :as p]))
(deftest graph-registry-key-is-indexeddb-compatible-test
@@ -49,6 +53,42 @@
:graph-id "remote-uuid"}
@stored-graph))))))
(deftest upsert-current-graph-registry-repairs-missing-local-graph-uuid-test
(async done
(let [upsert-current-f (some-> (resolve 'frontend.handler.graph/<upsert-current-graph-registry!) deref)
conn (d/create-conn db-schema/schema)
registry-entry (atom nil)]
(is (fn? upsert-current-f) "Current graph registry upsert should exist")
(d/transact! conn [{:db/ident :logseq.kv/schema-version
:kv/value db-schema/version}])
(p/with-redefs [state/get-current-repo (constantly "logseq_db_broken")
db/get-db (fn
([repo]
(when (= "logseq_db_broken" repo) @conn))
([repo deref?]
(when (= "logseq_db_broken" repo)
(if deref? @conn conn))))
db/transact! (fn [repo tx-data tx-meta]
(is (= "logseq_db_broken" repo))
(d/transact! conn tx-data tx-meta)
(p/resolved nil))
frontend.handler.graph/<upsert-graph-registry-entry!
(fn [entry]
(reset! registry-entry entry)
(p/resolved nil))]
(-> (upsert-current-f)
(.then (fn [_]
(let [local-graph-uuid (ldb/get-graph-local-uuid @conn)]
(is (uuid? local-graph-uuid))
(is (= (str local-graph-uuid)
(:local-graph-id @registry-entry)))
(is (= (:local-graph-id @registry-entry)
(:graph-id @registry-entry)))
(done))))
(.catch (fn [e]
(is false (str e))
(done))))))))
(deftest resolve-startup-repo-prefers-tab-repo-before-global-current-test
(let [resolve-f (some-> (resolve 'frontend.handler.graph/resolve-startup-repo) deref)]
(is (fn? resolve-f) "Startup repo resolver should exist")