Procházet zdrojové kódy

enhance: export edn handles assets

Addresses https://github.com/logseq/db-test/issues/656.
Should also address most :entity built in properties
Gabriel Horner před 2 dny
rodič
revize
991e38419a

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

@@ -114,8 +114,10 @@
                                          ;; Reasonable default for properties like logseq.property/default-value
                                          {:entity :number})
                                      built-in-type)]
-        {:db/ident k
-         :logseq.property/type built-in-type'}))
+        ;; Don't build property value entity if values are :block/uuid refs
+        (when (if (set? v) (not (vector? (first v))) (not (vector? v)))
+          {:db/ident k
+           :logseq.property/type built-in-type'})))
     (when (and (db-property-type/value-ref-property-types (get-in properties-config [k :logseq.property/type]))
                ;; Don't build property value entity if values are :block/uuid refs
                (if (set? v) (not (vector? (first v))) (not (vector? v))))

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

@@ -36,6 +36,11 @@
   [ent]
   (or (:block/raw-title ent) (:block/title ent)))
 
+;; nbb-compatible version of db-property/property-value-content
+(defn- property-value-content [pvalue]
+  (or (block-title pvalue)
+      (:logseq.property/value pvalue)))
+
 (defn- shallow-copy-page
   "Given a page or journal entity, shallow copies it e.g. no properties or tags info included.
    Pages that are shallow copied are at the edges of export and help keep the export size reasonable and
@@ -59,9 +64,7 @@
                                     {:keys [include-pvalue-uuid-fn]
                                      :or {include-pvalue-uuid-fn (constantly false)}
                                      :as options}]
-  (let [;; nbb-compatible version of db-property/property-value-content
-        property-value-content (or (block-title pvalue)
-                                   (:logseq.property/value pvalue))
+  (let [property-value-content' (property-value-content pvalue)
         ;; TODO: Add support for ref properties here and in sqlite.build
         build-properties
         (some->> ent-properties
@@ -72,7 +75,7 @@
                  (into {}))]
     (if (or (seq ent-properties) (seq (:block/tags pvalue)) (include-pvalue-uuid-fn (:block/uuid pvalue)))
       (cond-> {:build/property-value :block
-               :block/title property-value-content}
+               :block/title property-value-content'}
         (seq (:block/tags pvalue))
         (assoc :build/tags (->build-tags (:block/tags pvalue)))
 
@@ -84,7 +87,7 @@
 
         (:include-timestamps? options)
         (merge (select-keys pvalue [:block/created-at :block/updated-at])))
-      property-value-content)))
+      property-value-content')))
 
 (defonce ignored-properties [:logseq.property/created-by-ref :logseq.property.embedding/hnsw-label-updated-at])
 
@@ -96,7 +99,8 @@
             [db' property-ent pvalue properties-config' {:keys [property-value-uuids?] :as options'}]
             (if-let [build-page (and (not property-value-uuids?) (build-pvalue-entity-for-build-page pvalue))]
               build-page
-              (if (contains? #{:node :date} (:logseq.property/type property-ent))
+              (if (and (contains? #{:node :date :entity} (:logseq.property/type property-ent))
+                       (not= :logseq.property/default-value (:db/ident property-ent)))
                 ;; Idents take precedence over uuid because they keep data graph-agnostic
                 (if (:db/ident pvalue)
                   (:db/ident pvalue)
@@ -259,7 +263,7 @@
                          (build-node-properties db entity ent-properties (dissoc options :shallow-copy? :include-uuid-fn)))
         build-properties (when (and (not shallow-copy?) (seq ent-properties))
                            (buildable-properties db ent-properties (merge properties new-properties) options))
-        build-node (cond-> {:block/title (block-title entity)}
+        build-node (cond-> {:block/title (property-value-content entity)}
                      (some? (:block/collapsed? entity))
                      (assoc :block/collapsed? (:block/collapsed? entity))
                      (:block/link entity)
@@ -1157,4 +1161,4 @@
   (let [diff (->> (data/diff (prepare-export-to-diff export-map) (prepare-export-to-diff export-map2))
                   butlast)]
     (when-not (= [nil nil] diff)
-      diff)))
+      diff)))

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

@@ -954,3 +954,53 @@
                    export-edn)
     (is (empty? @empty-build-properties)
         "Export should omit :build/properties when it would otherwise be an empty map")))
+
+(deftest import-graph-with-assets
+  (let [asset-uuid (random-uuid)
+        asset2-uuid (random-uuid)
+        original-data
+        {:pages-and-blocks
+         [{:page {:block/title "page1"}
+           :blocks [{:block/title "asset block"
+                     :block/uuid asset-uuid
+                     :build/keep-uuid? true
+                     :build/tags [:logseq.class/Asset]
+                     :build/properties {:logseq.property.asset/type "pdf"
+                                        :logseq.property.asset/checksum "abc"
+                                        :logseq.property.asset/size 42}}
+                    {:block/title "annotation block"
+                     :build/tags [:logseq.class/Pdf-annotation]
+                     :build/properties {:logseq.property/asset [:block/uuid asset-uuid]}}]}
+          {:page {:block/title "page2"}
+           :blocks [{:block/title "asset image block"
+                     :block/uuid asset2-uuid
+                     :build/keep-uuid? true
+                     :build/tags [:logseq.class/Asset]
+                     :build/properties {:logseq.property.asset/type "png"
+                                        :logseq.property.asset/checksum "img-checksum"
+                                        :logseq.property.asset/width 100
+                                        :logseq.property.asset/height 200
+                                        :logseq.property.asset/size 300}}
+                    {:block/title "annotation with image"
+                     :build/tags [:logseq.class/Pdf-annotation]
+                     :build/properties {:logseq.property.pdf/hl-image [:block/uuid asset2-uuid]}}]}]}
+        conn (db-test/create-conn-with-blocks original-data)
+        conn2 (db-test/create-conn)
+        imported-graph (export-graph-and-import-to-another-graph conn conn2 {:exclude-built-in-pages? true})
+        annotation-block (->> (:pages-and-blocks imported-graph)
+                              (mapcat :blocks)
+                              (filter map?)
+                              (some #(when (= "annotation block" (:block/title %)) %)))
+        annotation-image (->> (:pages-and-blocks imported-graph)
+                              (mapcat :blocks)
+                              (filter map?)
+                              (some #(when (= "annotation with image" (:block/title %)) %)))]
+    ;; (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 (= [:block/uuid asset-uuid]
+           (get-in annotation-block [:build/properties :logseq.property/asset]))
+        ":logseq.property/asset should export as a uuid ref")
+    (is (= [:block/uuid asset2-uuid]
+           (get-in annotation-image [:build/properties :logseq.property.pdf/hl-image]))
+        ":logseq.property.pdf/hl-image should export as a uuid ref")))