Procházet zdrojové kódy

Add a script to validate datascript db for db graphs

fix schema script that was importing invalid descriptions and
was the source of CreativeWork failing
Gabriel Horner před 2 roky
rodič
revize
bdd3d7e05e

+ 3 - 1
scripts/nbb.edn

@@ -1,6 +1,8 @@
 {:paths ["src"]
  :deps
- {logseq/graph-parser
+ {metosin/malli
+  {:mvn/version "0.10.0"}
+  logseq/graph-parser
   {:local/root "../deps/graph-parser"}
   logseq/outliner
   {:local/root "../deps/outliner"}

+ 8 - 2
scripts/src/logseq/tasks/db_graph/create_graph_with_schema_org.cljs

@@ -32,6 +32,12 @@
   (or (class-uuids class-id)
       (throw (ex-info (str "No :block/uuid for " class-id) {}))))
 
+(defn- get-comment-string
+  [rdfs-comment]
+  (if (map? rdfs-comment)
+    (get rdfs-comment "@value")
+    rdfs-comment))
+
 (defn- ->class-page [class-m class-db-ids class-uuids class-properties property-uuids {:keys [verbose]}]
   (let [parent-class* (class-m "rdfs:subClassOf")
         parent-class (cond
@@ -57,7 +63,7 @@
              :properties (cond->
                           {:url (string/replace-first (class-m "@id") "schema:" "https://schema.org/")}
                            (class-m "rdfs:comment")
-                           (assoc :description (class-m "rdfs:comment")))}
+                           (assoc :description (get-comment-string (class-m "rdfs:comment"))))}
       parent-class
       (assoc :block/namespace {:db/id (get-class-db-id class-db-ids parent-class)})
       (seq properties)
@@ -102,7 +108,7 @@
                  (= schema-type :page)
                  (assoc :cardinality :many)
                  (property-m "rdfs:comment")
-                 (assoc :description (property-m "rdfs:comment"))
+                 (assoc :description (get-comment-string (property-m "rdfs:comment")))
                  (= schema-type :page)
                  (assoc :classes (let [invalid-classes (remove class-uuids range-includes)
                                        _ (when (seq invalid-classes)

+ 64 - 0
scripts/src/logseq/tasks/db_graph/validate_client_db.cljs

@@ -0,0 +1,64 @@
+(ns logseq.tasks.db-graph.validate-client-db
+  "Script that validates the datascript db of a db graph.
+   Currently only validates :block/schema but it will validate much more ..."
+  (:require [logseq.db.sqlite.cli :as sqlite-cli]
+            [logseq.db.sqlite.db :as sqlite-db]
+            [datascript.core :as d]
+            [clojure.string :as string]
+            [nbb.core :as nbb]
+            [clojure.pprint :as pprint]
+            [malli.core :as m]
+            ["path" :as node-path]
+            ["os" :as os]))
+
+(def client-db-schema
+  [:sequential
+   [:map
+    [:block/schema
+     {:optional true}
+     [:map
+      ;; TODO: only validate most of these for property blocks
+      [:type {:optional true} :keyword]
+      [:cardinality {:optional true} [:enum :one :many]]
+      [:classes {:optional true} [:set :uuid]]
+      [:description {:optional true} :string]
+      ;; TODO: require this for class blocks
+      [:properties {:optional true} [:vector :uuid]]]]]])
+
+(defn validate-client-db
+  "Validate datascript db as a vec of entity maps"
+  [ent-maps]
+  (if-let [errors (->> ent-maps
+                       (m/explain client-db-schema)
+                       :errors)]
+    (do
+      (println "Found" (count errors) "errors:")
+      (pprint/pprint errors)
+      (js/process.exit 1))
+    (println "Valid!")))
+
+(defn- datoms->entity-maps
+  "Returns entity maps for given :eavt datoms"
+  [datoms]
+  (->> datoms
+       (reduce (fn [acc m]
+                 (update acc (:e m) assoc (:a m) (:v m)))
+               {})
+       vals))
+
+(defn -main [args]
+  (when (not= 1 (count args))
+    (println "Usage: $0 GRAPH-DIR")
+    (js/process.exit 1))
+  (let [graph-dir (first args)
+        [dir db-name] (if (string/includes? graph-dir "/")
+                        ((juxt node-path/dirname node-path/basename) graph-dir)
+                        [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
+        _ (sqlite-db/open-db! dir db-name)
+        conn (sqlite-cli/read-graph db-name)
+        datoms (d/datoms @conn :eavt)
+        ent-maps (datoms->entity-maps datoms)]
+    (println "Read graph" (str db-name " with " (count datoms) " datoms!"))
+    (validate-client-db ent-maps)))
+
+(when (= nbb/*file* (:file (meta #'-main))) (-main *command-line-args*))