From 1dac3406441320f3a2ba5ee5fd6b0bcf6800a7c5 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 23 Aug 2024 17:47:32 +0200 Subject: [PATCH] accept fail of drop-db --- src/dda/backup/postgresql.clj | 4 +++- src/dda/backup/postgresql/domain.clj | 14 +++++++++---- test/dda/backup/postgresql/domain_test.clj | 24 +++++++++++++++------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/dda/backup/postgresql.clj b/src/dda/backup/postgresql.clj index dd65dea..68a2d19 100644 --- a/src/dda/backup/postgresql.clj +++ b/src/dda/backup/postgresql.clj @@ -27,4 +27,6 @@ (defn-spec drop-create-db! nil? [config ::pg-config] (let [config-w-defaults (merge default config)] - (i/execute! (domain/db-drop-create-command config-w-defaults) config-w-defaults))) + (try (i/execute! (domain/db-drop-command config-w-defaults) config-w-defaults) + (catch Exception e (println (.getMessage e)))) + (i/execute! (domain/db-create-command config-w-defaults) config-w-defaults))) diff --git a/src/dda/backup/postgresql/domain.clj b/src/dda/backup/postgresql/domain.clj index a4ef19d..df26fa2 100644 --- a/src/dda/backup/postgresql/domain.clj +++ b/src/dda/backup/postgresql/domain.clj @@ -68,11 +68,17 @@ (defn-spec pgpass string? [config ::pg-config] (let [{:keys [pg-host pg-port pg-db pg-user pg-password]} config] - (str pg-host ":" pg-port ":" pg-db ":" pg-user ":" pg-password))) + (str pg-host ":" pg-port ":" pg-db ":" pg-user ":" pg-password "\n" + pg-host ":" pg-port ":template1:" pg-user ":" pg-password "\n"))) -(defn-spec db-drop-create-command ::cd/commands +(defn-spec db-drop-command ::cd/commands [config ::pg-config] (let [{:keys [pg-db]} config config-w-template (merge config {:pg-db "template1"})] - [(psql-command config-w-template ["-c" (str "\"DROP DATABASE \\\"" pg-db "\\\";\"")]) - (psql-command config-w-template ["-c" (str "\"CREATE DATABASE \\\"" pg-db "\\\";\"")])])) + [(psql-command config-w-template ["-c" (str "DROP DATABASE \"" pg-db "\";")])])) + +(defn-spec db-create-command ::cd/commands + [config ::pg-config] + (let [{:keys [pg-db]} config + config-w-template (merge config {:pg-db "template1"})] + [(psql-command config-w-template ["-c" (str "CREATE DATABASE \"" pg-db "\";")])])) \ No newline at end of file diff --git a/test/dda/backup/postgresql/domain_test.clj b/test/dda/backup/postgresql/domain_test.clj index cb01c1f..6ed97ba 100644 --- a/test/dda/backup/postgresql/domain_test.clj +++ b/test/dda/backup/postgresql/domain_test.clj @@ -5,10 +5,11 @@ [dda.backup.postgresql.domain :as cut])) (st/instrument `cut/pgpass) -(st/instrument `cut/db-drop-create-command) +(st/instrument `cut/db-drop-command) +(st/instrument `cut/db-create-command) (deftest should-calculate-pgpass - (is (= "localhost:5432:mydb:user:password" + (is (= "localhost:5432:mydb:user:password\nlocalhost:5432:template1:user:password\n" (cut/pgpass {:restic-repository "repo" :backup-path "dir-at-repo" :pg-host "localhost" @@ -17,12 +18,21 @@ :pg-user "user" :pg-password "password"})))) -(deftest should-calculate-db-drop-create-command +(deftest should-calculate-db-drop-command (is (= [["psql" "-d" "template1" "-h" "localhost" "-p" "5432" "-U" "user" - "--no-password" "-c" "\"DROP DATABASE \\\"mydb\\\";\""] - ["psql" "-d" "template1" "-h" "localhost" "-p" "5432" "-U" "user" - "--no-password" "-c" "\"CREATE DATABASE \\\"mydb\\\";\""]] - (cut/db-drop-create-command {:restic-repository "repo" + "--no-password" "-c" "DROP DATABASE \"mydb\";"]] + (cut/db-drop-command {:restic-repository "repo" + :backup-path "dir-at-repo" + :pg-host "localhost" + :pg-port 5432 + :pg-db "mydb" + :pg-user "user" + :pg-password "password"})))) + +(deftest should-calculate-db-create-command + (is (= [["psql" "-d" "template1" "-h" "localhost" "-p" "5432" "-U" "user" + "--no-password" "-c" "CREATE DATABASE \"mydb\";"]] + (cut/db-create-command {:restic-repository "repo" :backup-path "dir-at-repo" :pg-host "localhost" :pg-port 5432