Parcourir la source

add closed-map option to validation script

use it to flesh out most of our datascript schema
Gabriel Horner il y a 2 ans
Parent
commit
0857a4e4a8
1 fichiers modifiés avec 73 ajouts et 14 suppressions
  1. 73 14
      scripts/src/logseq/tasks/db_graph/validate_client_db.cljs

+ 73 - 14
scripts/src/logseq/tasks/db_graph/validate_client_db.cljs

@@ -7,35 +7,85 @@
             [clojure.string :as string]
             [clojure.string :as string]
             [nbb.core :as nbb]
             [nbb.core :as nbb]
             [clojure.pprint :as pprint]
             [clojure.pprint :as pprint]
+            [clojure.walk :as walk]
             [malli.core :as m]
             [malli.core :as m]
+            [babashka.cli :as cli]
             ["path" :as node-path]
             ["path" :as node-path]
             ["os" :as os]))
             ["os" :as os]))
 
 
 (def client-db-schema
 (def client-db-schema
   [:sequential
   [:sequential
    [:map
    [:map
+    {:closed false}
+    [:block/uuid :uuid]
+    [:block/name {:optional true} :string]
+    [:block/original-name {:optional true} :string]
+    [:block/type {:optional true} [:enum "property" "class" "object"]]
+    [:block/content {:optional true} :string]
+    [:block/properties {:optional true}
+     [:map-of :uuid [:or
+                     :string
+                     :int
+                     :boolean
+                     :uuid
+                     :map
+                     [:vector [:or :keyword :uuid]]
+                     [:set :uuid]]]]
+    [:block/created-at {:optional true} :int]
+    [:block/updated-at {:optional true} :int]
+    ;; refs
+    [:block/left {:optional true} :int]
+    [:block/parent {:optional true} :int]
+    [:block/page {:optional true} :int]
+    [:block/namespace {:optional true} :int]
+    [:block/link {:optional true} :int]
+    [:block/path-refs {:optional true} :any] ;;TODO
+    [:block/refs {:optional true} :any] ;;TODO
+    [:block/tags {:optional true} :any] ;;TODO
+    ;; other
+    [:block/collapsed? {:optional true} :boolean]
+    [:block/journal? {:optional true} :boolean]
+    [:block/journal-day {:optional true} :int]
+    [:block/format {:optional true} [:enum :markdown]]
+    [:block/tx-id {:optional true} :int]
     [:block/schema
     [:block/schema
      {:optional true}
      {:optional true}
      [:map
      [:map
+      {:closed false}
       ;; TODO: only validate most of these for property blocks
       ;; TODO: only validate most of these for property blocks
       [:type {:optional true} :keyword]
       [:type {:optional true} :keyword]
       [:cardinality {:optional true} [:enum :one :many]]
       [:cardinality {:optional true} [:enum :one :many]]
       [:classes {:optional true} [:set :uuid]]
       [:classes {:optional true} [:set :uuid]]
       [:description {:optional true} :string]
       [:description {:optional true} :string]
+      [:hide? {:optional true} :boolean]
       ;; TODO: require this for class blocks
       ;; TODO: require this for class blocks
-      [:properties {:optional true} [:vector :uuid]]]]]])
+      [:properties {:optional true} [:vector :uuid]]]]
+
+    [:file/content {:optional true} :string]
+    ;; TODO: Remove when bug is fixed
+    [:file/last-modified-at {:optional true} :any]
+    [:file/path {:optional true} :string]]])
 
 
 (defn validate-client-db
 (defn validate-client-db
   "Validate datascript db as a vec of entity maps"
   "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!")))
+  [ent-maps {:keys [closed-maps]}]
+  (let [schema (if closed-maps
+                 (walk/postwalk (fn [e]
+                                  (if (and (vector? e)
+                                           (= :map (first e))
+                                           (contains? (second e) :closed))
+                                    (assoc e 1 (assoc (second e) :closed true))
+                                    e))
+                                client-db-schema)
+                 client-db-schema)]
+    (if-let [errors (->> ent-maps
+                        (m/explain schema)
+                        :errors)]
+     (do
+       (println "Found" (count errors) "errors:")
+       (pprint/pprint errors)
+       (js/process.exit 1))
+     (println "Valid!"))))
 
 
 (defn- datoms->entity-maps
 (defn- datoms->entity-maps
   "Returns entity maps for given :eavt datoms"
   "Returns entity maps for given :eavt datoms"
@@ -46,11 +96,20 @@
                {})
                {})
        vals))
        vals))
 
 
+(def spec
+  "Options spec"
+  {:help {:alias :h
+          :desc "Print help"}
+   :closed-maps {:alias :c
+                 :desc "Validate maps marked with closed as :closed"}})
+
 (defn -main [args]
 (defn -main [args]
-  (when (not= 1 (count args))
-    (println "Usage: $0 GRAPH-DIR")
-    (js/process.exit 1))
   (let [graph-dir (first args)
   (let [graph-dir (first args)
+        options (cli/parse-opts args {:spec spec})
+        _ (when (or (nil? graph-dir) (:help options))
+            (println (str "Usage: $0 GRAPH-NAME [OPTIONS]\nOptions:\n"
+                          (cli/format-opts {:spec spec})))
+            (js/process.exit 1))
         [dir db-name] (if (string/includes? graph-dir "/")
         [dir db-name] (if (string/includes? graph-dir "/")
                         ((juxt node-path/dirname node-path/basename) graph-dir)
                         ((juxt node-path/dirname node-path/basename) graph-dir)
                         [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
                         [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
@@ -59,7 +118,7 @@
         datoms (d/datoms @conn :eavt)
         datoms (d/datoms @conn :eavt)
         ent-maps (datoms->entity-maps datoms)]
         ent-maps (datoms->entity-maps datoms)]
     (println "Read graph" (str db-name " with " (count datoms) " datoms!"))
     (println "Read graph" (str db-name " with " (count datoms) " datoms!"))
-    (validate-client-db ent-maps)))
+    (validate-client-db ent-maps options)))
 
 
 (when (= nbb/*file* (:file (meta #'-main)))
 (when (= nbb/*file* (:file (meta #'-main)))
   (-main *command-line-args*))
   (-main *command-line-args*))