Skip to content
Snippets Groups Projects
Unverified Commit cdefea9a authored by Walter Leibbrandt's avatar Walter Leibbrandt Committed by GitHub
Browse files

Add --keep-existing flag to dump-to-h2 command (#13260)

* Add --keep-existing flag to dump-to-h2 command

* Explain --keep-existing flag in docstrings

* Don't log db password
parent cb258fb6
No related branches found
No related tags found
No related merge requests found
......@@ -37,11 +37,14 @@
((resolve 'metabase.cmd.load-from-h2/load-from-h2!) h2-connection-string))))
(defn ^:command dump-to-h2
"Transfer data from existing database to newly created H2 DB."
[h2-filename]
"Transfer data from existing database to newly created H2 DB with specified filename.
Target H2 file is deleted before dump, unless the --keep-existing flag is given."
[h2-filename & opts]
(classloader/require 'metabase.cmd.dump-to-h2)
(binding [mdb/*disable-data-migrations* true]
(let [return-code ((resolve 'metabase.cmd.dump-to-h2/dump-to-h2!) h2-filename)]
(let [keep-existing (boolean (some #{"--keep-existing"} opts))
return-code ((resolve 'metabase.cmd.dump-to-h2/dump-to-h2!) h2-filename keep-existing)]
(when (pos-int? return-code)
(System/exit return-code)))))
......
......@@ -79,7 +79,7 @@
(println-ok))
(defn- load-data! [target-db-conn]
(println "Source db:" (mdb/jdbc-spec))
(println "Source db:" (dissoc (mdb/jdbc-spec) :password))
(jdbc/with-db-connection [db-conn (mdb/jdbc-spec)]
(doseq [{table-name :table, :as e} entities
:let [rows (jdbc/query db-conn [(str "SELECT * FROM " (name table-name))])]
......@@ -92,17 +92,19 @@
;;; --------------------------------------------------- Public Fns ---------------------------------------------------
(defn dump-to-h2!
"Transfer data from existing database specified by connection string
to the H2 DB specified by env vars. Intended as a tool for migrating
from one instance to another using H2 as serialization target.
"Transfer data from existing database specified by connection string to the H2 DB specified by env vars. Intended as a
tool for migrating from one instance to another using H2 as serialization target.
Defaults to using `@metabase.db/db-file` as the connection string."
[h2-filename]
Defaults to using `@metabase.db/db-file` as the connection string.
Target H2 DB will be deleted if it exists, unless `keep-existing` is truthy."
[h2-filename keep-existing]
(let [h2-filename (or h2-filename "metabase_dump.h2")]
(println "Dumping to " h2-filename)
(doseq [filename [h2-filename
(str h2-filename ".mv.db")]]
(when (.exists (io/file filename))
(when (and (.exists (io/file filename))
(not keep-existing))
(fs/delete filename)
(println (u/format-color 'red (trs "Output H2 database already exists: %s, removing.") filename))))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment