validate_client_db.cljs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (ns validate-client-db
  2. "Script that validates the datascript db of a DB graph
  3. NOTE: This script is also used in CI to confirm our db's schema is up to date"
  4. (:require [logseq.db.sqlite.cli :as sqlite-cli]
  5. [logseq.db.frontend.malli-schema :as db-malli-schema]
  6. [logseq.db.frontend.validate :as db-validate]
  7. [logseq.db.frontend.property :as db-property]
  8. [datascript.core :as d]
  9. [clojure.string :as string]
  10. [nbb.core :as nbb]
  11. [malli.core :as m]
  12. [babashka.cli :as cli]
  13. ["path" :as node-path]
  14. ["os" :as os]
  15. [cljs.pprint :as pprint]
  16. [malli.error :as me]))
  17. (defn validate-client-db
  18. "Validate datascript db as a vec of entity maps"
  19. [db ent-maps* {:keys [verbose group-errors humanize closed-maps]}]
  20. (let [ent-maps (db-malli-schema/update-properties-in-ents db ent-maps*)
  21. schema (db-validate/update-schema db-malli-schema/DB db {:closed-schema? closed-maps})]
  22. (if-let [explanation (->> ent-maps
  23. (m/explain schema)
  24. not-empty)]
  25. (do
  26. (if group-errors
  27. (let [ent-errors (db-validate/group-errors-by-entity db ent-maps (:errors explanation))]
  28. (println "Found" (count ent-errors) "entities in errors:")
  29. (cond
  30. verbose
  31. (pprint/pprint ent-errors)
  32. humanize
  33. (pprint/pprint (map #(-> (dissoc % :errors-by-type)
  34. (update :errors (fn [errs] (me/humanize {:errors errs}))))
  35. ent-errors))
  36. :else
  37. (pprint/pprint (map :entity ent-errors))))
  38. (let [errors (:errors explanation)]
  39. (println "Found" (count errors) "errors:")
  40. (cond
  41. verbose
  42. (pprint/pprint
  43. (map #(assoc %
  44. :entity (get ent-maps (-> % :in first))
  45. :schema (m/form (:schema %)))
  46. errors))
  47. humanize
  48. (pprint/pprint (me/humanize {:errors errors}))
  49. :else
  50. (pprint/pprint errors))))
  51. (js/process.exit 1))
  52. (println "Valid!"))))
  53. (def spec
  54. "Options spec"
  55. {:help {:alias :h
  56. :desc "Print help"}
  57. :humanize {:alias :H
  58. :desc "Humanize errors as an alternative to -v"}
  59. :verbose {:alias :v
  60. :desc "Print more info"}
  61. :closed-maps {:alias :c
  62. :desc "Validate maps marked with closed as :closed"}
  63. :group-errors {:alias :g
  64. :desc "Groups errors by their entity id"}})
  65. (defn- validate-graph [graph-dir options]
  66. (let [[dir db-name] (if (string/includes? graph-dir "/")
  67. (let [graph-dir'
  68. (node-path/join (or js/process.env.ORIGINAL_PWD ".") graph-dir)]
  69. ((juxt node-path/dirname node-path/basename) graph-dir'))
  70. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
  71. conn (try (sqlite-cli/open-db! dir db-name)
  72. (catch :default e
  73. (println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
  74. (js/process.exit 1)))
  75. datoms (d/datoms @conn :eavt)
  76. ent-maps (db-malli-schema/datoms->entities datoms)]
  77. (println "Read graph" (str db-name " with " (count datoms) " datoms, "
  78. (count ent-maps) " entities, "
  79. (count (filter :block/name ent-maps)) " pages, "
  80. (count (filter :block/title ent-maps)) " blocks, "
  81. (count (filter #(= (:block/type %) "tag") ent-maps)) " tags, "
  82. (count (filter #(seq (:block/tags %)) ent-maps)) " objects, "
  83. (count (filter #(= (:block/type %) "property") ent-maps)) " properties and "
  84. (count (mapcat db-property/properties ent-maps)) " property pairs"))
  85. (validate-client-db @conn ent-maps options)))
  86. (defn -main [argv]
  87. (let [{:keys [args opts]} (cli/parse-args argv {:spec spec})
  88. _ (when (or (empty? args) (:help opts))
  89. (println (str "Usage: $0 GRAPH-NAME [& ADDITIONAL-GRAPHS] [OPTIONS]\nOptions:\n"
  90. (cli/format-opts {:spec spec})))
  91. (js/process.exit 1))]
  92. (doseq [graph-dir args]
  93. (validate-graph graph-dir opts))))
  94. (when (= nbb/*file* (:file (meta #'-main)))
  95. (-main *command-line-args*))