diff_graphs.cljs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. (ns diff-graphs
  2. "A script that diffs two DB graphs through their sqlite.build EDN"
  3. (:require ["os" :as os]
  4. ["path" :as node-path]
  5. [babashka.cli :as cli]
  6. [clojure.data :as data]
  7. [clojure.pprint :as pprint]
  8. [clojure.string :as string]
  9. [logseq.common.config :as common-config]
  10. [logseq.db.common.sqlite-cli :as sqlite-cli]
  11. [logseq.db.sqlite.export :as sqlite-export]
  12. [nbb.core :as nbb]))
  13. (defn- get-dir-and-db-name
  14. "Gets dir and db name for use with open-db! Works for relative and absolute paths and
  15. defaults to ~/logseq/graphs/ when no '/' present in name"
  16. [graph-dir]
  17. (if (string/includes? graph-dir "/")
  18. (let [resolve-path' #(if (node-path/isAbsolute %) %
  19. ;; $ORIGINAL_PWD used by bb tasks to correct current dir
  20. (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
  21. ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
  22. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
  23. (def spec
  24. "Options spec"
  25. {:help {:alias :h
  26. :desc "Print help"}
  27. :exclude-namespaces {:alias :e
  28. :coerce #{}
  29. :desc "Namespaces to exclude from properties and classes"}
  30. :exclude-built-in-pages? {:alias :b
  31. :desc "Exclude built-in pages"}
  32. :set-diff {:alias :s
  33. :desc "Use set to reduce noisy diff caused by ordering"}
  34. :include-timestamps? {:alias :t
  35. :desc "Include timestamps in export"}})
  36. (defn -main [args]
  37. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  38. [graph-dir graph-dir2] args'
  39. _ (when (or (nil? graph-dir) (nil? graph-dir2) (:help options))
  40. (println (str "Usage: $0 GRAPH-NAME GRAPH-NAME2 [& ARGS] [OPTIONS]\nOptions:\n"
  41. (cli/format-opts {:spec spec})))
  42. (js/process.exit 1))
  43. conn (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir))
  44. conn2 (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir2))
  45. export-options (select-keys options [:include-timestamps? :exclude-namespaces :exclude-built-in-pages?])
  46. export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})
  47. export-map2 (sqlite-export/build-export @conn2 {:export-type :graph :graph-options export-options})
  48. prepare-export-to-diff
  49. (fn [m]
  50. (cond->
  51. (-> m
  52. ;; TODO: Fix order of these build keys
  53. (update :classes update-vals (fn [m] (update m :build/class-properties sort)))
  54. (update :properties update-vals (fn [m] (update m :build/property-classes sort)))
  55. (update ::sqlite-export/kv-values
  56. (fn [kvs]
  57. ;; Ignore extra metadata that a copied graph can add
  58. (vec (remove #(#{:logseq.kv/import-type :logseq.kv/imported-at} (:db/ident %)) kvs))))
  59. ;; TODO: fix built-in views for schema export
  60. (update :pages-and-blocks (fn [pbs]
  61. (vec (remove #(= (:block/title (:page %)) common-config/views-page-name) pbs)))))
  62. (:set-diff options)
  63. (update-vals set)))
  64. diff (->> (data/diff (prepare-export-to-diff export-map) (prepare-export-to-diff export-map2))
  65. butlast)]
  66. (if (= diff [nil nil])
  67. (println "The two graphs are equal!")
  68. (do (pprint/pprint diff)
  69. (js/process.exit 1)))))
  70. (when (= nbb/*file* (nbb/invoked-file))
  71. (-main *command-line-args*))