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

refactor: make CLI and testing validation consistent

Both have needs that are more for local graphs
Gabriel Horner 3 сар өмнө
parent
commit
c8f99d1ff3

+ 11 - 34
deps/cli/src/logseq/cli/commands/validate.cljs

@@ -2,44 +2,21 @@
   "Validate graph command"
   (:require ["fs" :as fs]
             [cljs.pprint :as pprint]
-            [datascript.core :as d]
             [logseq.cli.util :as cli-util]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
-            [logseq.db.frontend.malli-schema :as db-malli-schema]
-            [logseq.db.frontend.validate :as db-validate]
-            [malli.error :as me]))
-
-(defn- validate-db*
-  "Validate datascript db as a vec of entity maps"
-  [db ent-maps* {:keys [open-schema]}]
-  (let [ent-maps (db-malli-schema/update-properties-in-ents db ent-maps*)
-        explainer (db-validate/get-schema-explainer (not open-schema))]
-    (if-let [explanation (binding [db-malli-schema/*db-for-validate-fns* db
-                                   db-malli-schema/*closed-values-validate?* true]
-                           (->> (map (fn [e] (dissoc e :db/id)) ent-maps) explainer not-empty))]
-      (let [ent-errors
-            (->> (db-validate/group-errors-by-entity db ent-maps (:errors explanation))
-                 (map #(update % :errors
-                               (fn [errs]
-                                 ;; errs looks like: {178 {:logseq.property/hide? ["disallowed key"]}}
-                                 ;; map is indexed by :in which is unused since all errors are for the same map
-                                 (->> (me/humanize {:errors errs})
-                                      vals
-                                      (apply merge-with into))))))]
-        (println "Found" (count ent-errors)
-                 (if (= 1 (count ent-errors)) "entity" "entities")
-                 "with errors:")
-        (pprint/pprint ent-errors)
-        (js/process.exit 1))
-      (println "Valid!"))))
+            [logseq.db.frontend.validate :as db-validate]))
 
 (defn- validate-db [db db-name options]
-  (let [datoms (d/datoms db :eavt)
-        ent-maps (db-malli-schema/datoms->entities datoms)]
-    (println "Read graph" (str db-name " with counts: "
-                               (pr-str (assoc (db-validate/graph-counts db ent-maps)
-                                              :datoms (count datoms)))))
-    (validate-db* db ent-maps options)))
+  (if-let [errors (:errors (db-validate/validate-local-db!
+                            db
+                            (merge options {:db-name db-name :verbose true})))]
+    (do
+      (println "Found" (count errors)
+               (if (= 1 (count errors)) "entity" "entities")
+               "with errors:")
+      (pprint/pprint errors)
+      (js/process.exit 1))
+    (println "Valid!")))
 
 (defn- validate-graph [graph options]
   (if (fs/existsSync (cli-util/get-graph-path graph))

+ 26 - 0
deps/db/src/logseq/db/frontend/validate.cljs

@@ -114,3 +114,29 @@
      ;; Objects that aren't classes or properties
      :objects (- (count (d/datoms db :avet :block/tags)) classes-count properties-count)
      :property-pairs (count (mapcat #(-> % db-property/properties (dissoc :block/tags)) entities))}))
+
+(defn validate-local-db!
+  "Validates a local (non-RTC) DB like validate-db! but with default behavior,
+  options and logging specific to a local DB. Used by CLI and tests"
+  [db & {:keys [db-name open-schema verbose]}]
+  (let [datoms (d/datoms db :eavt)
+        ent-maps (db-malli-schema/datoms->entities datoms)
+        _ (when verbose
+            (println "Read graph" (str db-name " with counts: "
+                                       (pr-str (assoc (graph-counts db ent-maps)
+                                                      :datoms (count datoms))))))
+        ent-maps (db-malli-schema/update-properties-in-ents db ent-maps)
+        explainer (get-schema-explainer (not open-schema))]
+    (when-let [explanation (binding [db-malli-schema/*db-for-validate-fns* db
+                                     db-malli-schema/*closed-values-validate?* true]
+                             (->> (map (fn [e] (dissoc e :db/id)) ent-maps) explainer not-empty))]
+      (let [ent-errors
+            (->> (group-errors-by-entity db ent-maps (:errors explanation))
+                 (map #(update % :errors
+                               (fn [errs]
+                                 ;; errs looks like: {178 {:logseq.property/hide? ["disallowed key"]}}
+                                 ;; map is indexed by :in which is unused since all errors are for the same map
+                                 (->> (me/humanize {:errors errs})
+                                      vals
+                                      (apply merge-with into))))))]
+        {:errors ent-errors}))))

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

@@ -1070,7 +1070,7 @@
     (let [import-conn (db-test/create-conn)
           {:keys [init-tx block-props-tx misc-tx] :as _txs} (build-import export-edn @import-conn {})
           _ (d/transact! import-conn (concat init-tx block-props-tx misc-tx))
-          validation (db-validate/validate-db! @import-conn)]
+          validation (db-validate/validate-local-db! @import-conn)]
       (when-let [errors (seq (:errors validation))]
         (js/console.error "Exported edn has the following invalid errors when imported into a new graph:")
         (pprint/pprint errors)

+ 2 - 2
deps/db/test/logseq/db/sqlite/create_graph_test.cljs

@@ -127,7 +127,7 @@
 
 (deftest new-graph-is-valid
   (let [conn (db-test/create-conn)
-        validation (db-validate/validate-db! @conn)]
+        validation (db-validate/validate-local-db! @conn)]
     ;; For debugging
     ;; (println (count (:errors validation)) "errors of" (count (:entities validation)))
     ;; (cljs.pprint/pprint (:errors validation))
@@ -149,7 +149,7 @@
                    ;; :url macros are used for consistently building urls with the same hostname e.g. docs graph
                    {:block/title "b2" :build/properties {:url "{{docs-base-url test}}"}}]}]})
 
-      (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
+      (is (empty? (map :entity (:errors (db-validate/validate-local-db! @conn))))
           "Graph with different :url blocks has no validation errors"))))
 
 (deftest build-db-initial-data-test

+ 1 - 1
deps/db/test/logseq/db/sqlite/export_test.cljs

@@ -19,7 +19,7 @@
 (defn- validate-db
   "Validate db, usually after transacting an import"
   [db]
-  (let [validation (db-validate/validate-db! db)]
+  (let [validation (db-validate/validate-local-db! db)]
     (when (seq (:errors validation)) (cljs.pprint/pprint {:validate (:errors validation)}))
     (is (empty? (map :entity (:errors validation))) "Imported graph has no validation errors")))