Forráskód Böngészése

enhance(dev): humanize option for validation script

also fix nil entity query results
Gabriel Horner 1 éve
szülő
commit
8ec360759a

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

@@ -28,7 +28,7 @@
         [dir db-name] (get-dir-and-db-name graph-dir)
         conn (sqlite-db/open-db! dir db-name)
         results (if ((set args) "-e")
-                  (map #(let [ent (d/entity @conn (edn/read-string %))]
+                  (map #(when-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 '$ '%])]

+ 21 - 8
deps/db/script/validate_client_db.cljs

@@ -11,31 +11,42 @@
             [babashka.cli :as cli]
             ["path" :as node-path]
             ["os" :as os]
-            [cljs.pprint :as pprint]))
+            [cljs.pprint :as pprint]
+            [malli.error :as me]))
 
 (defn validate-client-db
   "Validate datascript db as a vec of entity maps"
-  [db ent-maps* {:keys [verbose group-errors closed-maps]}]
+  [db ent-maps* {:keys [verbose group-errors humanize closed-maps]}]
   (let [ent-maps (db-malli-schema/update-properties-in-ents db ent-maps*)
         schema (db-validate/update-schema db-malli-schema/DB db {:closed-schema? closed-maps})]
-    (if-let [errors (->> ent-maps
+    (if-let [explanation (->> ent-maps
                          (m/explain schema)
-                         :errors)]
+                         not-empty)]
       (do
         (if group-errors
-          (let [ent-errors (db-validate/group-errors-by-entity db ent-maps errors)]
+          (let [ent-errors (db-validate/group-errors-by-entity db ent-maps (:errors explanation))]
             (println "Found" (count ent-errors) "entities in errors:")
-            (if verbose
+            (cond
+              verbose
               (pprint/pprint ent-errors)
+              humanize
+              (pprint/pprint (map #(-> (dissoc % :errors-by-type)
+                                       (update :errors (fn [errs] (me/humanize {:errors errs}))))
+                                  ent-errors))
+              :else
               (pprint/pprint (map :entity ent-errors))))
-          (do
+          (let [errors (:errors explanation)]
             (println "Found" (count errors) "errors:")
-            (if verbose
+            (cond
+              verbose
               (pprint/pprint
                (map #(assoc %
                             :entity (get ent-maps (-> % :in first))
                             :schema (m/form (:schema %)))
                     errors))
+              humanize
+              (pprint/pprint (me/humanize {:errors errors}))
+              :else
               (pprint/pprint errors))))
         (js/process.exit 1))
       (println "Valid!"))))
@@ -44,6 +55,8 @@
   "Options spec"
   {:help {:alias :h
           :desc "Print help"}
+   :humanize {:alias :H
+              :desc "Humanize errors as an alternative to -v"}
    :verbose {:alias :v
              :desc "Print more info"}
    :closed-maps {:alias :c

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

@@ -53,7 +53,9 @@
                            (update :block/page
                                    (fn [id] (select-keys (d/entity db id)
                                                          [:block/name :block/type :db/id :block/created-at])))))
+               :errors errors'
                ;; Group by type to reduce verbosity
+               ;; TODO: Move/remove this to another fn if unused
                :errors-by-type
                (->> (group-by :type errors')
                     (map (fn [[type' type-errors]]