enhance(cli): Add validate option to export-edn command

Useful for sanity checking exports and dev
This commit is contained in:
Gabriel Horner
2025-12-04 10:30:03 -05:00
parent 8fc56bfaa3
commit 07a0feb856
3 changed files with 19 additions and 4 deletions

View File

@@ -19,23 +19,36 @@
(= :graph (:export-type options))
(assoc :graph-options (dissoc options :file :export-type :graph))))
(defn- local-export [{{:keys [graph] :as options} :opts}]
(defn- validate-export
[export-map {:keys [catch-validation-errors?]}]
(println "Validating export which can take awhile on large graphs ...")
(if-let [error (:error (sqlite-export/validate-export export-map))]
(if catch-validation-errors?
(js/console.error error)
(cli-util/error error))
(println "Valid export!")))
(defn- local-export [{{:keys [graph validate] :as options} :opts}]
(when-not graph
(cli-util/error "Command missing required option 'graph'"))
(if (fs/existsSync (cli-util/get-graph-path graph))
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
_ (cli-util/ensure-db-graph-for-command @conn)
export-map (sqlite-export/build-export @conn (build-export-options options))]
(when validate
(validate-export export-map options))
(write-export-edn-map export-map options))
(cli-util/error "Graph" (pr-str graph) "does not exist")))
(defn- api-export
[{{:keys [api-server-token] :as options} :opts}]
[{{:keys [api-server-token validate] :as options} :opts}]
(let [opts (build-export-options options)]
(-> (p/let [resp (cli-util/api-fetch api-server-token "logseq.cli.export_edn" [(clj->js opts)])]
(if (= 200 (.-status resp))
(p/let [body (.json resp)
export-map (sqlite-util/transit-read (aget body "export-body"))]
(when validate
(validate-export export-map options))
(write-export-edn-map export-map (assoc options :graph (.-graph body))))
(cli-util/api-handle-error-response resp)))
(p/catch cli-util/command-catch-handler))))

View File

@@ -17,6 +17,8 @@
:desc "Include timestamps in export"}
:file {:alias :f
:desc "File to save export"}
:validate {:alias :v
:desc "Validates export by importing into a temp graph and validating it"}
:catch-validation-errors? {:alias :c
:desc "Catch validation errors for dev"}
:exclude-namespaces {:alias :e

View File

@@ -1072,9 +1072,9 @@
_ (d/transact! import-conn (concat init-tx block-props-tx misc-tx))
validation (db-validate/validate-local-db! @import-conn)]
(when-let [errors (seq (:errors validation))]
(js/console.error "Exported edn has the following invalid errors when imported into a new graph:")
(js/console.error "Exported EDN has the following invalid errors when imported into a new graph:")
(pprint/pprint errors)
{:error (str "The exported EDN has " (count errors) " error(s). See the javascript console for more details.")}))
{:error (str "The exported EDN has " (count errors) " validation error(s)")}))
(catch :default e
(js/console.error "Unexpected export-edn validation error:" e)
{:error (str "The exported EDN is unexpectedly invalid: " (pr-str (ex-message e)))})))