From cdefea9a59451be59fe2897919065677ec44e822 Mon Sep 17 00:00:00 2001 From: Walter Leibbrandt <23798+walterl@users.noreply.github.com> Date: Fri, 18 Sep 2020 05:17:38 +0000 Subject: [PATCH] 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 --- src/metabase/cmd.clj | 9 ++++++--- src/metabase/cmd/dump_to_h2.clj | 16 +++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/metabase/cmd.clj b/src/metabase/cmd.clj index b1c56553cfe..c01468aaf79 100644 --- a/src/metabase/cmd.clj +++ b/src/metabase/cmd.clj @@ -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))))) diff --git a/src/metabase/cmd/dump_to_h2.clj b/src/metabase/cmd/dump_to_h2.clj index 38863329a66..eaf89f8543f 100644 --- a/src/metabase/cmd/dump_to_h2.clj +++ b/src/metabase/cmd/dump_to_h2.clj @@ -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)))) -- GitLab