1
0

validate_client_db.cljs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. (ns validate-client-db
  2. "Script that validates the datascript db of a DB graph"
  3. (:require [logseq.db.sqlite.cli :as sqlite-cli]
  4. [logseq.db.sqlite.db :as sqlite-db]
  5. [logseq.db.schema :as db-schema]
  6. [logseq.db.malli-schema :as db-malli-schema]
  7. [datascript.core :as d]
  8. [clojure.string :as string]
  9. [nbb.core :as nbb]
  10. [clojure.walk :as walk]
  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. (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. (let [db-schema-with-property-vals (db-malli-schema/update-properties-in-schema db-schema db)]
  45. (if closed-maps
  46. ;; closes maps that don't have an explicit :closed option
  47. (walk/postwalk (fn [e]
  48. (if (and (vector? e)
  49. (= :map (first e)))
  50. (if (map? (second e))
  51. (if (not (contains? (second e) :closed))
  52. (assoc e 1 (assoc (second e) :closed true))
  53. e)
  54. (into [:map {:closed true}] (rest e)))
  55. e))
  56. db-schema-with-property-vals)
  57. db-schema-with-property-vals)))
  58. (defn validate-client-db
  59. "Validate datascript db as a vec of entity maps"
  60. [db ent-maps* {:keys [verbose group-errors] :as options}]
  61. (let [ent-maps (vec (db-malli-schema/update-properties-in-ents (vals ent-maps*)))
  62. schema (update-schema db-malli-schema/DB db options)]
  63. (if-let [errors (->> ent-maps
  64. (m/explain schema)
  65. :errors)]
  66. (do
  67. (if group-errors
  68. (let [ent-errors (build-grouped-errors db ent-maps errors)]
  69. (println "Found" (count ent-errors) "entities in errors:")
  70. (if verbose
  71. (pprint/pprint ent-errors)
  72. (pprint/pprint (map :entity ent-errors))))
  73. (do
  74. (println "Found" (count errors) "errors:")
  75. (if verbose
  76. (pprint/pprint
  77. (map #(assoc %
  78. :entity (get ent-maps (-> % :in first))
  79. :schema (m/form (:schema %)))
  80. errors))
  81. (pprint/pprint errors))))
  82. (js/process.exit 1))
  83. (println "Valid!"))))
  84. (defn- datoms->entity-maps
  85. "Returns entity maps for given :eavt datoms"
  86. [datoms]
  87. (->> datoms
  88. (reduce (fn [acc m]
  89. (if (contains? db-schema/card-many-attributes (:a m))
  90. (update acc (:e m) update (:a m) (fnil conj #{}) (:v m))
  91. (update acc (:e m) assoc (:a m) (:v m))))
  92. {})))
  93. (def spec
  94. "Options spec"
  95. {:help {:alias :h
  96. :desc "Print help"}
  97. :verbose {:alias :v
  98. :desc "Print more info"}
  99. :closed-maps {:alias :c
  100. :desc "Validate maps marked with closed as :closed"}
  101. :group-errors {:alias :g
  102. :desc "Groups errors by their entity id"}})
  103. (defn- validate-graph [graph-dir options]
  104. (let [[dir db-name] (if (string/includes? graph-dir "/")
  105. (let [graph-dir'
  106. (node-path/join (or js/process.env.ORIGINAL_PWD ".") graph-dir)]
  107. ((juxt node-path/dirname node-path/basename) graph-dir'))
  108. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
  109. _ (try (sqlite-db/open-db! dir db-name)
  110. (catch :default e
  111. (println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
  112. (js/process.exit 1)))
  113. conn (sqlite-cli/read-graph db-name)
  114. datoms (d/datoms @conn :eavt)
  115. ent-maps (datoms->entity-maps datoms)]
  116. (println "Read graph" (str db-name " with " (count datoms) " datoms, "
  117. (count ent-maps) " entities and "
  118. (count (mapcat :block/properties (vals ent-maps))) " properties"))
  119. (validate-client-db @conn ent-maps options)))
  120. (defn -main [argv]
  121. (let [{:keys [args opts]} (cli/parse-args argv {:spec spec})
  122. _ (when (or (empty? args) (:help opts))
  123. (println (str "Usage: $0 GRAPH-NAME [& ADDITIONAL-GRAPHS] [OPTIONS]\nOptions:\n"
  124. (cli/format-opts {:spec spec})))
  125. (js/process.exit 1))]
  126. (doseq [graph-dir args]
  127. (validate-graph graph-dir opts))))
  128. (when (= nbb/*file* (:file (meta #'-main)))
  129. (-main *command-line-args*))