Browse Source

Provide consistent sort for graph's pages-and-blocks

Also allow full timestamp test now that all timestamp issues are fixed
Gabriel Horner 7 months ago
parent
commit
9232d10ac2

+ 10 - 1
deps/db/src/logseq/db/sqlite/export.cljs

@@ -526,6 +526,14 @@
                            :blocks (sqlite-build/update-each-block blocks remove-uuid-if-not-ref)})
                         pages-and-blocks))))))
 
+(defn sort-pages-and-blocks
+  "Provide a reliable sort order since this tends to be large. Helps with diffing
+   and readability"
+  [pages-and-blocks]
+  (vec
+   (sort-by #(or (get-in % [:page :block/title]) (get-in % [:page :block/title]))
+            pages-and-blocks)))
+
 (defn- build-graph-export
   "Exports whole graph. Has the following options:
    * :include-timestamps? - When set timestamps are included on all blocks"
@@ -542,7 +550,8 @@
         files (build-graph-files db options)
         ;; Remove all non-ref uuids after all nodes are built.
         ;; Only way to ensure all pvalue uuids present across block types
-        graph-export' (remove-uuids-if-not-ref graph-export all-ref-uuids)]
+        graph-export' (-> (remove-uuids-if-not-ref graph-export all-ref-uuids)
+                          (update :pages-and-blocks sort-pages-and-blocks))]
     (merge graph-export'
            {::graph-files files})))
 

+ 13 - 17
deps/db/test/logseq/db/sqlite/export_test.cljs

@@ -1,18 +1,18 @@
 (ns logseq.db.sqlite.export-test
   (:require [cljs.pprint]
             [cljs.test :refer [deftest is testing]]
+            [clojure.walk :as walk]
             [datascript.core :as d]
             [logseq.common.config :as common-config]
+            [logseq.common.util :as common-util]
             [logseq.common.util.date-time :as date-time-util]
             [logseq.common.util.page-ref :as page-ref]
+            [logseq.common.uuid :as common-uuid]
             [logseq.db :as ldb]
             [logseq.db.frontend.validate :as db-validate]
             [logseq.db.sqlite.export :as sqlite-export]
             [logseq.db.test.helper :as db-test]
-            [medley.core :as medley]
-            [clojure.walk :as walk]
-            [logseq.common.util :as common-util]
-            [logseq.common.uuid :as common-uuid]))
+            [medley.core :as medley]))
 
 ;; Test helpers
 ;; ============
@@ -114,6 +114,8 @@
                  (assoc :block/title (name k)))]))
        (into {})))
 
+(def sort-pages-and-blocks sqlite-export/sort-pages-and-blocks)
+
 ;; Tests
 ;; =====
 
@@ -635,9 +637,9 @@
 
     ;; (cljs.pprint/pprint (set (:pages-and-blocks original-data)))
     ;; (cljs.pprint/pprint (set (:pages-and-blocks imported-graph)))
-    ;; (cljs.pprint/pprint (butlast (clojure.data/diff (set (:pages-and-blocks original-data))
-    ;;                                                 (set (:pages-and-blocks imported-graph)))))
-    (is (= (set (:pages-and-blocks original-data)) (set (:pages-and-blocks imported-graph))))
+    ;; (cljs.pprint/pprint (butlast (clojure.data/diff (sort-pages-and-blocks (:pages-and-blocks original-data))
+    ;;                                                 (:pages-and-blocks imported-graph))))
+    (is (= (sort-pages-and-blocks (:pages-and-blocks original-data)) (:pages-and-blocks imported-graph)))
     (is (= 1 (count (d/datoms @conn2 :avet :block/title "page object")))
         "No duplicate pages for pvalue uuids used more than once")
     (is (= (expand-properties (:properties original-data)) (:properties imported-graph)))
@@ -670,19 +672,13 @@
         conn (db-test/create-conn-with-blocks (dissoc original-data ::sqlite-export/graph-files))
         _ (d/transact! conn (::sqlite-export/graph-files original-data))
         conn2 (db-test/create-conn)
-        imported-graph (export-graph-and-import-to-another-graph conn conn2 {:include-timestamps? true})
-        ignore-timestamps-if-built-in
-        (fn [m]
-          (if (get-in m [:page :build/properties :logseq.property/built-in?])
-            (update m :page dissoc :block/created-at :block/updated-at)
-            m))]
+        imported-graph (export-graph-and-import-to-another-graph conn conn2 {:include-timestamps? true})]
 
     ;; (cljs.pprint/pprint (set (:pages-and-blocks original-data)))
     ;; (cljs.pprint/pprint (set (:pages-and-blocks imported-graph)))
-    ;; (cljs.pprint/pprint (butlast (clojure.data/diff (set (map ignore-timestamps-if-built-in (:pages-and-blocks original-data)))
-    ;;                                                 (set (map ignore-timestamps-if-built-in (:pages-and-blocks imported-graph))))))
-    (is (= (set (map ignore-timestamps-if-built-in (:pages-and-blocks original-data)))
-           (set (map ignore-timestamps-if-built-in (:pages-and-blocks imported-graph)))))
+    ;; (cljs.pprint/pprint (butlast (clojure.data/diff (sort-pages-and-blocks (:pages-and-blocks original-data))
+    ;;                                                 (:pages-and-blocks imported-graph))))
+    (is (= (sort-pages-and-blocks (:pages-and-blocks original-data)) (:pages-and-blocks imported-graph)))
     (is (= (expand-properties (:properties original-data)) (:properties imported-graph)))
     (is (= (expand-classes (:classes original-data)) (:classes imported-graph)))
     (is (= (::sqlite-export/graph-files original-data) (::sqlite-export/graph-files imported-graph))