1
0
Эх сурвалжийг харах

fix: merging exports for same journal

or page and their blocks. Also fix lint
Gabriel Horner 8 сар өмнө
parent
commit
0ccb59a2ad

+ 2 - 0
deps/db/.carve/ignore

@@ -1,4 +1,6 @@
 ;; API
+logseq.db.file-based.rules/rules
+;; API
 logseq.db.file-based.schema/retract-attributes
 ;; API
 logseq.db.frontend.rules/db-query-dsl-rules

+ 8 - 3
deps/db/src/logseq/db/sqlite/export.cljs

@@ -213,9 +213,14 @@
 (defn- merge-export-maps [& export-maps]
   (let [pages-and-blocks
         (->> (mapcat :pages-and-blocks export-maps)
-             ;; TODO: Group by more correct identity, same as check-for-existing-entities
-             (group-by #(get-in % [:page :block/title]))
-             (mapv #(apply merge (second %))))
+             ;; TODO: Group by more correct identity for title, same as check-for-existing-entities
+             (group-by #(select-keys (:page %) [:block/title :build/journal]))
+             (mapv #(apply merge-with (fn [e1 e2]
+                                        ;; merge :page and add :blocks
+                                        (if (and (map? e1) (map e2))
+                                          (merge e1 e2)
+                                          (into e1 e2)))
+                           (second %))))
         ;; Use merge-with to preserve new-property? and to allow full copies to overwrite shallow ones
         properties (apply merge-with merge (keep :properties export-maps))
         classes (apply merge-with merge (keep :classes export-maps))]

+ 34 - 0
deps/db/test/logseq/db/sqlite/export_test.cljs

@@ -9,6 +9,8 @@
             [logseq.db.test.helper :as db-test]
             [medley.core :as medley]))
 
+;; Test helpers
+;; ============
 (defn- export-block-and-import-to-another-block
   "Exports given block from one graph/conn, imports it to a 2nd block and then
    exports the 2nd block. The two blocks do not have to be in the same graph"
@@ -54,6 +56,38 @@
                  (assoc :block/title (name k)))]))
        (into {})))
 
+;; Tests
+;; =====
+
+(deftest merge-export-maps
+  (is (= {:pages-and-blocks
+          [{:page {:block/title "page1"}
+            :blocks [{:block/title "b1"}
+                     {:block/title "b2"}]}
+           {:page {:block/title "page2"}}]}
+         (#'sqlite-export/merge-export-maps
+          {:pages-and-blocks
+           [{:page {:block/title "page1"}
+             :blocks [{:block/title "b1"}]}]}
+          {:pages-and-blocks
+           [{:page {:block/title "page1"}
+             :blocks [{:block/title "b2"}]}
+            {:page {:block/title "page2"}}]}))
+      "In :pages-and-blocks, identical pages and their :blocks are merged")
+
+  (is (= {:pages-and-blocks
+          [{:page {:build/journal 20250220}
+            :blocks [{:block/title "b1"}]}
+           {:page {:build/journal 20250221}}]}
+       (#'sqlite-export/merge-export-maps
+          {:pages-and-blocks
+           [{:page {:build/journal 20250220}
+             :blocks [{:block/title "b1"}]}]}
+          {:pages-and-blocks
+           [{:page {:build/journal 20250220}}
+            {:page {:build/journal 20250221}}]}))
+      "In :pages-and-blocks, identical journals and their :blocks are merged"))
+
 (deftest import-block-in-same-graph
   (let [original-data
         {:properties {:user.property/default-many {:logseq.property/type :default :db/cardinality :db.cardinality/many}}