Browse Source

enhance: save git sha of new graph in kv/value

Will be useful for pinpointing if certain graphs are impacted by bugs.
graph-initial-schema-version is helpful but can stay the same for
awhile.
Closes
https://test.logseq.com/#/page/6835e455-69f6-4bbd-9865-b76fa57ce457
Gabriel Horner 4 months ago
parent
commit
e7583b8b8c

+ 4 - 2
deps/db/src/logseq/db/sqlite/create_graph.cljs

@@ -193,7 +193,7 @@
 (defn build-db-initial-data
   "Builds tx of initial data for a new graph including key values, initial files,
    built-in properties and built-in classes"
-  [config-content & {:keys [import-type]}]
+  [config-content & {:keys [import-type graph-git-sha]}]
   (assert (string? config-content))
   (let [initial-data (cond->
                       [(sqlite-util/kv :logseq.kv/db-type "db")
@@ -204,7 +204,9 @@
                        {:db/ident :logseq.property/empty-placeholder
                         :block/uuid (common-uuid/gen-uuid :builtin-block-uuid :logseq.property/empty-placeholder)}]
                        import-type
-                       (into (sqlite-util/import-tx import-type)))
+                       (into (sqlite-util/import-tx import-type))
+                       graph-git-sha
+                       (conj (sqlite-util/kv :logseq.kv/graph-git-sha graph-git-sha)))
         initial-files [{:block/uuid (common-uuid/gen-uuid :builtin-block-uuid "logseq/config.edn")
                         :file/path (str "logseq/" "config.edn")
                         :file/content config-content

+ 2 - 1
src/main/frontend/handler/repo.cljs

@@ -180,7 +180,8 @@
   (->
    (p/let [config (common-config/create-config-for-db-graph config/config-default-content)
            _ (persist-db/<new full-graph-name
-                              (cond-> {:config config}
+                              (cond-> {:config config
+                                       :graph-git-sha config/revision}
                                 file-graph-import? (assoc :import-type :file-graph)))
            _ (start-repo-db-if-not-exists! full-graph-name)
            _ (state/add-repo! {:url full-graph-name :root (config/get-local-dir full-graph-name)})

+ 12 - 13
src/main/frontend/worker/db_worker.cljs

@@ -326,7 +326,7 @@
   (.exec db "PRAGMA journal_mode=WAL"))
 
 (defn- create-or-open-db!
-  [repo {:keys [config import-type datoms]}]
+  [repo {:keys [config import-type datoms] :as opts}]
   (when-not (worker-state/get-sqlite-conn repo)
     (p/let [[db search-db client-ops-db :as dbs] (get-dbs repo)
             storage (new-sqlite-storage db)
@@ -363,7 +363,7 @@
         (when (and db-based? (not initial-data-exists?) (not datoms))
           (let [config (or config "")
                 initial-data (sqlite-create-graph/build-db-initial-data config
-                                                                        (when import-type {:import-type import-type}))]
+                                                                        (select-keys opts [:import-type :graph-git-sha]))]
             (d/transact! conn initial-data {:initial-db? true})))
 
         ;; TODO: remove this once we can ensure there's no bug for missing addresses
@@ -467,7 +467,6 @@
 ;; [graph service]
 (defonce *service (atom []))
 (defonce fns {"remoteInvoke" thread-api/remote-function})
-(declare <init-service!)
 
 (defn- start-db!
   [repo {:keys [close-other-db?]
@@ -846,14 +845,14 @@
            (js/console.error (str "DB is not found for " repo))))))))
 
 (defn- on-become-master
-  [repo config import?]
+  [repo start-opts]
   (js/Promise.
    (m/sp
-     (c.m/<? (init-sqlite-module!))
-     (when-not import?
-       (c.m/<? (start-db! repo {:config config}))
-       (assert (some? (worker-state/get-datascript-conn repo))))
-     (m/? (rtc.core/new-task--rtc-start true)))))
+    (c.m/<? (init-sqlite-module!))
+    (when-not (:import-type start-opts)
+      (c.m/<? (start-db! repo start-opts))
+      (assert (some? (worker-state/get-datascript-conn repo))))
+    (m/? (rtc.core/new-task--rtc-start true)))))
 
 (def broadcast-data-types
   (set (map
@@ -866,7 +865,7 @@
          :rtc-sync-state])))
 
 (defn- <init-service!
-  [graph config import?]
+  [graph start-opts]
   (let [[prev-graph service] @*service]
     (some-> prev-graph close-db!)
     (when graph
@@ -874,9 +873,9 @@
         service
         (p/let [service (shared-service/<create-service graph
                                                         (bean/->js fns)
-                                                        #(on-become-master graph config import?)
+                                                        #(on-become-master graph start-opts)
                                                         broadcast-data-types
-                                                        {:import? import?})]
+                                                        {:import? (:import-type? start-opts)})]
           (assert (p/promise? (get-in service [:status :ready])))
           (reset! *service [graph service])
           service)))))
@@ -897,7 +896,7 @@
                                 ;; because shared-service operates at the graph level,
                                 ;; creating a new database or switching to another one requires re-initializing the service.
                                 (let [[graph opts] (ldb/read-transit-str (last args))]
-                                  (p/let [service (<init-service! graph (:config opts) (some? (:import-type opts)))]
+                                  (p/let [service (<init-service! graph opts)]
                                     (get-in service [:status :ready])
                                     ;; wait for service ready
                                     (js-invoke (:proxy service) k args)))