瀏覽代碼

chore: reuse common helper for stringifying datoms

Gabriel Horner 2 年之前
父節點
當前提交
48009adaf9

+ 18 - 1
deps/db/src/logseq/db/sqlite/util.cljs

@@ -1,7 +1,9 @@
 (ns logseq.db.sqlite.util
 (ns logseq.db.sqlite.util
   "Utils fns for backend sqlite db"
   "Utils fns for backend sqlite db"
   (:require [cljs-time.coerce :as tc]
   (:require [cljs-time.coerce :as tc]
-            [cljs-time.core :as t]))
+            [cljs-time.core :as t]
+            [cognitect.transit :as transit]
+            [logseq.db.schema :as db-schema]))
 
 
 (defn- type-of-block
 (defn- type-of-block
   "
   "
@@ -40,3 +42,18 @@
    :datoms (:datoms b)
    :datoms (:datoms b)
    :created_at (or (:block/created-at b) (time-ms))
    :created_at (or (:block/created-at b) (time-ms))
    :updated_at (or (:block/updated-at b) (time-ms))})
    :updated_at (or (:block/updated-at b) (time-ms))})
+
+(defn block-map->datoms-str
+  "Given a block map and all existing blocks, return the block as transit data
+   to be stored in the `datoms` column. This is currently only used in testing"
+  [blocks m]
+  (let [t-writer (transit/writer :json)]
+    (->> (dissoc m :db/id)
+         ;; This fn should match pipeline/datom->av-vector
+         (map (fn m->av-vector [[a v]]
+                [a v]
+                (if (contains? db-schema/ref-type-attributes a)
+                  [a
+                   [:block/uuid (str (some #(when (= (:db/id %) (:db/id v)) (:block/uuid %)) blocks))]]
+                  [a v])))
+         (transit/write t-writer))))

+ 2 - 17
deps/db/test/logseq/db/sqlite/restore_test.cljs

@@ -3,9 +3,7 @@
             ["fs" :as fs]
             ["fs" :as fs]
             ["path" :as node-path]
             ["path" :as node-path]
             [cljs-bean.core :as bean]
             [cljs-bean.core :as bean]
-            [cognitect.transit :as t]
             [datascript.core :as d]
             [datascript.core :as d]
-            [logseq.db.schema :as db-schema]
             [logseq.db.sqlite.db :as sqlite-db]
             [logseq.db.sqlite.db :as sqlite-db]
             [logseq.db.sqlite.restore :as sqlite-restore]
             [logseq.db.sqlite.restore :as sqlite-restore]
             [logseq.db.sqlite.util :as sqlite-util]))
             [logseq.db.sqlite.util :as sqlite-util]))
@@ -25,19 +23,6 @@
   [dir db-name]
   [dir db-name]
   (fs/mkdirSync (node-path/join dir db-name) #js {:recursive true}))
   (fs/mkdirSync (node-path/join dir db-name) #js {:recursive true}))
 
 
-(defn- block-map->datoms-str
-  [blocks m]
-  (let [t-writer (t/writer :json)]
-    (->> (dissoc m :db/id)
-         ;; This fn should match pipeline/datom->av-vector
-         (map (fn m->av-vector [[a v]]
-                [a v]
-                (if (contains? db-schema/ref-type-attributes a)
-                  [a
-                   [:block/uuid (str (some #(when (= (:db/id %) (:db/id v)) (:block/uuid %)) blocks))]]
-                  [a v])))
-         (t/write t-writer))))
-
 (deftest restore-initial-data
 (deftest restore-initial-data
   (testing "Restore a journal page with its block"
   (testing "Restore a journal page with its block"
     (create-graph-dir "tmp/graphs" "test-db")
     (create-graph-dir "tmp/graphs" "test-db")
@@ -59,7 +44,7 @@
                             :block/updated-at created-at
                             :block/updated-at created-at
                             :page_uuid page-uuid}]
                             :page_uuid page-uuid}]
           blocks (mapv #(sqlite-util/ds->sqlite-block
           blocks (mapv #(sqlite-util/ds->sqlite-block
-                         (assoc % :datoms (block-map->datoms-str frontend-blocks %)))
+                         (assoc % :datoms (sqlite-util/block-map->datoms-str frontend-blocks %)))
                        frontend-blocks)
                        frontend-blocks)
           _ (sqlite-db/upsert-blocks! "test-db" (bean/->js blocks))
           _ (sqlite-db/upsert-blocks! "test-db" (bean/->js blocks))
           {:keys [conn]} (sqlite-restore/restore-initial-data (bean/->js (sqlite-db/get-initial-data "test-db")))]
           {:keys [conn]} (sqlite-restore/restore-initial-data (bean/->js (sqlite-db/get-initial-data "test-db")))]
@@ -88,7 +73,7 @@
                             :page_uuid page-uuid
                             :page_uuid page-uuid
                             :block/created-at created-at}]
                             :block/created-at created-at}]
           blocks (mapv #(sqlite-util/ds->sqlite-block
           blocks (mapv #(sqlite-util/ds->sqlite-block
-                         (assoc % :datoms (block-map->datoms-str frontend-blocks %)))
+                         (assoc % :datoms (sqlite-util/block-map->datoms-str frontend-blocks %)))
                        frontend-blocks)
                        frontend-blocks)
           _ (sqlite-db/upsert-blocks! "test-db" (bean/->js blocks))
           _ (sqlite-db/upsert-blocks! "test-db" (bean/->js blocks))
           {:keys [uuid->db-id-map conn]}
           {:keys [uuid->db-id-map conn]}

+ 1 - 16
deps/graph-parser/test/logseq/graph_parser/cli_test.cljs

@@ -8,8 +8,6 @@
             [logseq.db.sqlite.restore :as sqlite-restore]
             [logseq.db.sqlite.restore :as sqlite-restore]
             [logseq.db.sqlite.util :as sqlite-util]
             [logseq.db.sqlite.util :as sqlite-util]
             [cljs-bean.core :as bean]
             [cljs-bean.core :as bean]
-            [cognitect.transit :as t]
-            [logseq.db.schema :as db-schema]
             [logseq.db :as ldb]
             [logseq.db :as ldb]
             [clojure.set :as set]
             [clojure.set :as set]
             ["fs" :as fs]
             ["fs" :as fs]
@@ -96,19 +94,6 @@
                       (string/join "\n" (map #(str "- " (:block/content %)) blocks))))
                       (string/join "\n" (map #(str "- " (:block/content %)) blocks))))
   (let [{:keys [conn]} (gp-cli/parse-graph graph-dir {:verbose false})] @conn))
   (let [{:keys [conn]} (gp-cli/parse-graph graph-dir {:verbose false})] @conn))
 
 
-(defn- block-map->datoms-str
-  [blocks m]
-  (let [t-writer (t/writer :json)]
-    (->> (dissoc m :db/id)
-         ;; This fn should match pipeline/datom->av-vector
-         (map (fn m->av-vector [[a v]]
-                [a v]
-                (if (contains? db-schema/ref-type-attributes a)
-                  [a
-                   [:block/uuid (str (some #(when (= (:db/id %) (:db/id v)) (:block/uuid %)) blocks))]]
-                  [a v])))
-         (t/write t-writer))))
-
 (defn- create-frontend-blocks
 (defn- create-frontend-blocks
   "For a map of pages to their blocks, this creates frontend blocks assuming only top level blocks
   "For a map of pages to their blocks, this creates frontend blocks assuming only top level blocks
    are desired. Anything more complex starts to recreate outliner namespaces"
    are desired. Anything more complex starts to recreate outliner namespaces"
@@ -148,7 +133,7 @@
   (sqlite-db/open-db! dir db-name)
   (sqlite-db/open-db! dir db-name)
   (let [frontend-blocks (create-frontend-blocks pages-to-blocks)
   (let [frontend-blocks (create-frontend-blocks pages-to-blocks)
         blocks (mapv #(sqlite-util/ds->sqlite-block
         blocks (mapv #(sqlite-util/ds->sqlite-block
-                       (assoc % :datoms (block-map->datoms-str frontend-blocks %)))
+                       (assoc % :datoms (sqlite-util/block-map->datoms-str frontend-blocks %)))
                      frontend-blocks)
                      frontend-blocks)
         _ (sqlite-db/upsert-blocks! db-name (bean/->js blocks))
         _ (sqlite-db/upsert-blocks! db-name (bean/->js blocks))
         {:keys [uuid->db-id-map conn]}
         {:keys [uuid->db-id-map conn]}