validate_db.cljs 4.1 KB

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