validate_client_db.cljs 5.1 KB

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