validate_client_db.cljs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.db :as sqlite-db]
  5. [logseq.db.frontend.malli-schema :as db-malli-schema]
  6. [datascript.core :as d]
  7. [clojure.string :as string]
  8. [nbb.core :as nbb]
  9. [malli.core :as m]
  10. [malli.util :as mu]
  11. [babashka.cli :as cli]
  12. ["path" :as node-path]
  13. ["os" :as os]
  14. [cljs.pprint :as pprint]))
  15. (defn- build-grouped-errors [db full-maps errors]
  16. (->> errors
  17. (group-by #(-> % :in first))
  18. (map (fn [[idx errors']]
  19. {:entity (cond-> (get full-maps idx)
  20. ;; Provide additional page info for debugging
  21. (:block/page (get full-maps idx))
  22. (update :block/page
  23. (fn [id] (select-keys (d/entity db id)
  24. [:block/name :block/type :db/id :block/created-at]))))
  25. ;; Group by type to reduce verbosity
  26. :errors-by-type
  27. (->> (group-by :type errors')
  28. (map (fn [[type' type-errors]]
  29. [type'
  30. {:in-value-distinct (->> type-errors
  31. (map #(select-keys % [:in :value]))
  32. distinct
  33. vec)
  34. :schema-distinct (->> (map :schema type-errors)
  35. (map m/form)
  36. distinct
  37. vec)}]))
  38. (into {}))}))))
  39. (defn- update-schema
  40. "Updates the db schema to add a datascript db for property validations
  41. and to optionally close maps"
  42. [db-schema db {:keys [closed-maps]}]
  43. (cond-> db-schema
  44. true
  45. (db-malli-schema/update-properties-in-schema db)
  46. closed-maps
  47. mu/closed-schema))
  48. (defn validate-client-db
  49. "Validate datascript db as a vec of entity maps"
  50. [db ent-maps* {:keys [verbose group-errors] :as options}]
  51. (let [ent-maps (vec (db-malli-schema/update-properties-in-ents (vals ent-maps*)))
  52. schema (update-schema db-malli-schema/DB db options)]
  53. (if-let [errors (->> ent-maps
  54. (m/explain schema)
  55. :errors)]
  56. (do
  57. (if group-errors
  58. (let [ent-errors (build-grouped-errors db ent-maps errors)]
  59. (println "Found" (count ent-errors) "entities in errors:")
  60. (if verbose
  61. (pprint/pprint ent-errors)
  62. (pprint/pprint (map :entity ent-errors))))
  63. (do
  64. (println "Found" (count errors) "errors:")
  65. (if verbose
  66. (pprint/pprint
  67. (map #(assoc %
  68. :entity (get ent-maps (-> % :in first))
  69. :schema (m/form (:schema %)))
  70. errors))
  71. (pprint/pprint errors))))
  72. (js/process.exit 1))
  73. (println "Valid!"))))
  74. (def spec
  75. "Options spec"
  76. {:help {:alias :h
  77. :desc "Print help"}
  78. :verbose {:alias :v
  79. :desc "Print more info"}
  80. :closed-maps {:alias :c
  81. :desc "Validate maps marked with closed as :closed"}
  82. :group-errors {:alias :g
  83. :desc "Groups errors by their entity id"}})
  84. (defn- validate-graph [graph-dir options]
  85. (let [[dir db-name] (if (string/includes? graph-dir "/")
  86. (let [graph-dir'
  87. (node-path/join (or js/process.env.ORIGINAL_PWD ".") graph-dir)]
  88. ((juxt node-path/dirname node-path/basename) graph-dir'))
  89. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
  90. conn (try (sqlite-db/open-db! dir db-name)
  91. (catch :default e
  92. (println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
  93. (js/process.exit 1)))
  94. datoms (d/datoms @conn :eavt)
  95. ent-maps (db-malli-schema/datoms->entity-maps datoms)]
  96. (println "Read graph" (str db-name " with " (count datoms) " datoms, "
  97. (count ent-maps) " entities and "
  98. (count (mapcat :block/properties (vals ent-maps))) " properties"))
  99. (validate-client-db @conn ent-maps options)))
  100. (defn -main [argv]
  101. (let [{:keys [args opts]} (cli/parse-args argv {:spec spec})
  102. _ (when (or (empty? args) (:help opts))
  103. (println (str "Usage: $0 GRAPH-NAME [& ADDITIONAL-GRAPHS] [OPTIONS]\nOptions:\n"
  104. (cli/format-opts {:spec spec})))
  105. (js/process.exit 1))]
  106. (doseq [graph-dir args]
  107. (validate-graph graph-dir opts))))
  108. (when (= nbb/*file* (:file (meta #'-main)))
  109. (-main *command-line-args*))