Browse Source

fix: schema graph creation for previous properties schema

Also add db/id to query script and fix outdated case insensitive
constraint on property db idents
Gabriel Horner 1 year ago
parent
commit
cbcff4bae8

+ 2 - 1
deps/db/script/query.cljs

@@ -28,7 +28,8 @@
         [dir db-name] (get-dir-and-db-name graph-dir)
         conn (sqlite-db/open-db! dir db-name)
         results (if ((set args) "-e")
-                  (map #(into {} (d/entity @conn (edn/read-string %))) (drop 2 args))
+                  (map #(let [ent (d/entity @conn (edn/read-string %))]
+                          (into {:db/id (:db/id ent)} ent)) (drop 2 args))
                   ;; assumes no :in are in queries
                   (let [query (into (edn/read-string query*) [:in '$ '%])]
                     (mapv first (d/q query @conn (rules/extract-rules rules/db-query-dsl-rules)))))]

+ 1 - 1
deps/db/src/logseq/db/frontend/property.cljs

@@ -287,7 +287,7 @@
    NOTE: Only use this when creating a db-ident for a property name. Using this
    in read-only contexts like querying can result in db-ident conflicts"
   [property-name]
-  (let [n (-> (string/lower-case property-name)
+  (let [n (-> property-name
               (string/replace #"(^:\s*|\s*:$)" "")
               (string/replace #"\s*:\s*$" "")
               (string/replace-first #"^\d+" "")

+ 4 - 3
deps/db/src/logseq/db/sqlite/util.cljs

@@ -69,9 +69,10 @@
 (defn build-new-property
   "Build a standard new property so that it is is consistent across contexts. Takes
    an optional map with following keys:
-   * :original-name - Case sensitive property name. Defaults to deriving this from db-ident"
+   * :original-name - Case sensitive property name. Defaults to deriving this from db-ident
+   * :block-uuid - :block/uuid for property"
   ([db-ident prop-schema] (build-new-property db-ident prop-schema {}))
-  ([db-ident prop-schema {:keys [original-name]}]
+  ([db-ident prop-schema {:keys [original-name block-uuid]}]
    (assert (keyword? db-ident))
    (let [db-ident' (if (qualified-keyword? db-ident)
                      db-ident
@@ -85,7 +86,7 @@
         :block/format :markdown
         :block/schema (merge {:type :default} prop-schema)
         :block/name (common-util/page-name-sanity-lc (name prop-name))
-        :block/uuid (d/squuid)
+        :block/uuid (or block-uuid (d/squuid))
         :block/original-name (name prop-name)
         :db/index true
         :db/cardinality (if (= :many (:cardinality prop-schema))

+ 6 - 4
scripts/src/logseq/tasks/db_graph/create_graph.cljs

@@ -10,6 +10,7 @@
             [logseq.outliner.cli.pipeline :as cli-pipeline]
             [logseq.common.util :as common-util]
             [clojure.string :as string]
+            [clojure.set :as set]
             [datascript.core :as d]
             ["fs" :as fs]
             ["path" :as node-path]
@@ -85,9 +86,8 @@
   (let [property-uuids (->> pages-and-blocks
                             (map #(-> (:blocks %) vec (conj (:page %))))
                             (mapcat #(->> % (map :properties) (mapcat keys)))
-                            set
+                            ((fn [x] (set/difference (set x) (set (keys properties)))))
                             (map #(vector % (random-uuid)))
-                            ;; TODO: Dedupe with above to avoid squashing a previous definition
                             (concat (map (fn [[k v]]
                                            [k (or (:block/uuid v) (random-uuid))])
                                          properties))
@@ -109,7 +109,7 @@
 (defn- build-property-refs [properties property-db-ids]
   (mapv
    (fn [prop-name]
-     (db-property/create-user-property-ident-from-name (name prop-name)))
+     {:db/ident (db-property/create-user-property-ident-from-name (name prop-name))})
    (keys properties)))
 
 (def current-db-id (atom 0))
@@ -183,7 +183,9 @@
                                     {:db/id (or (property-db-ids (name prop-name))
                                                 (throw (ex-info "No :db/id for property" {:property prop-name})))}}))
                                 [(merge
-                                  (sqlite-util/build-new-property prop-name (get-in properties [prop-name :block/schema]))
+                                  (sqlite-util/build-new-property prop-name
+                                                                  (get-in properties [prop-name :block/schema])
+                                                                  {:block-uuid (get-in properties [prop-name :block/uuid])})
                                   {:db/id (or (property-db-ids (name prop-name))
                                               (throw (ex-info "No :db/id for property" {:property prop-name})))}
                                   (when-let [props (not-empty (get-in properties [prop-name :properties]))]

+ 14 - 11
scripts/src/logseq/tasks/db_graph/create_graph_with_schema_org.cljs

@@ -12,6 +12,7 @@
      * schema.org assumes no cardinality. For now, only :page properties are given a :cardinality :many"
   (:require [logseq.tasks.db-graph.create-graph :as create-graph]
             [logseq.common.util :as common-util]
+            [logseq.db.frontend.property :as db-property]
             [clojure.string :as string]
             [datascript.core :as d]
             ["path" :as node-path]
@@ -77,7 +78,7 @@
       parent-class
       (assoc :class/parent {:db/id (get-class-db-id class-db-ids parent-class)})
       (seq properties)
-      (assoc :block/schema {:properties (mapv property-uuids properties)}))))
+      (assoc :class/schema.properties (mapv #(hash-map :block/uuid (property-uuids %)) properties)))))
 
 (def schema->logseq-data-types
   "Schema datatypes, https://schema.org/DataType, mapped to their Logseq equivalents"
@@ -322,6 +323,7 @@
         ;; Debug: Uncomment to generate a narrower graph of classes
         ;; select-class-ids ["schema:Person" "schema:CreativeWorkSeries"
         ;;                   "schema:Movie" "schema:CreativeWork" "schema:Thing"]
+        ;; select-class-ids ["schema:Thing"]
         ;; Generate class uuids as they are needed for properties (:page) and pages
         class-uuids (->> all-classes
                          (map #(vector (% "@id") (random-uuid)))
@@ -356,9 +358,9 @@
              :desc "Verbose mode"}})
 
 (defn- write-debug-file [blocks-tx db]
-  (let [block-uuid->name* (->> (d/q '[:find (pull ?b [:block/name :block/uuid]) :where [?b :block/name]] db)
+  (let [block-uuid->name* (->> (d/q '[:find (pull ?b [:block/original-name :block/uuid]) :where [?b :block/original-name]] db)
                                (map first)
-                               (map (juxt :block/uuid :block/name))
+                               (map (juxt :block/uuid :block/original-name))
                                (into {}))
         block-uuid->name #(or (block-uuid->name* %) (throw (ex-info (str "No entity found for " %) {})))
         ;; TODO: Figure out why some Thing's properties don't exist
@@ -368,14 +370,15 @@
                       (pr-str
                        (->> blocks-tx
                             (map (fn [m]
-                                   (cond-> (select-keys m [:block/name :block/type :block/original-name
-                                                           :block/properties :block/schema])
-                                     (seq (:block/properties m))
-                                     (update :block/properties #(update-keys % block-uuid->name))
-                                     (seq (get-in m [:block/schema :properties]))
-                                     (update-in [:block/schema :properties] #(mapv (partial block-uuid->name-please-fixme m) %))
-                                     (seq (get-in m [:block/schema :classes]))
-                                     (update-in [:block/schema :classes] #(mapv block-uuid->name %)))))
+                                   (let [props (db-property/properties m)]
+                                     (cond-> (select-keys m [:block/name :block/type :block/original-name :block/schema])
+                                       (seq props)
+                                       (assoc :block/properties (update-keys props name))
+                                       (seq (:class/schema.properties m))
+                                       (assoc-in [:block/schema :properties] (mapv (partial block-uuid->name-please-fixme m)
+                                                                                   (map :block/uuid (:class/schema.properties m))))
+                                       (seq (get-in m [:block/schema :classes]))
+                                       (update-in [:block/schema :classes] #(mapv block-uuid->name %))))))
                             set)))))
 
 (defn -main [args]