Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host and port to backup, restore, and migrate build_db functions #137

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/triangulum/build_db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,28 @@
(.read is array 0 5)
(String. array))))

(defn- run-backup [database file admin-pass verbose]
(defn- run-backup [host port database file admin-pass verbose]
(println "Backing up database...")
(sh-wrapper "./"
{:PGPASSWORD admin-pass}
verbose
(format-str "pg_dump -U postgres -d %d --format=custom --compress=4 --file=%f"
(format-str "pg_dump -h %h -p %p -U postgres -d %d --format=custom --compress=4 --file=%f"
host
port
database
file)))

(defn- run-restore [file admin-pass verbose]
(defn- run-restore [host port file admin-pass verbose]
;; TODO check database against 'pg_restore --list file'
(println "Restoring database...")
(if (= "PGDMP" (read-file-tag file))
(sh-wrapper "./"
{:PGPASSWORD admin-pass}
verbose
(str "pg_restore -U postgres -d postgres --clean --if-exists --create --jobs=12 "
file))
(format-str "pg_restore -h %h -p %p -U postgres -d postgres --clean --if-exists --create --jobs=12 %f"
host
port
file))
(println "Invalid .dump file.")))

(def ^:private cli-options
Expand Down Expand Up @@ -210,9 +214,11 @@
(or user dbname)
(or password dbname) ; user-pass
verbose)
:backup (run-backup dbname file admin-pass verbose)
:restore (run-restore file admin-pass verbose)
:migrate (migrate! dbname ; TODO we might need consider host and port here.
:backup (run-backup host port dbname file admin-pass verbose)
:restore (run-restore host port file admin-pass verbose)
:migrate (migrate! host
port
dbname
(or user dbname)
(or password dbname) ; user-pass
verbose)
Expand Down
8 changes: 5 additions & 3 deletions src/triangulum/migrate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
(defn- migration-path [filename]
(io/file *migrations-dir* filename))

(defn- get-conn [database user user-pass]
(defn- get-conn [host port database user user-pass]
(jdbc/get-connection {:dbtype "postgresql"
:dbname database
:host host
:port port
:user user
:password user-pass
:reWriteBatchedInserts true}))
Expand Down Expand Up @@ -104,10 +106,10 @@
and include a SHA-256 hash of the migration file contents. If a migration has
been altered, the migrations will fail. This is to ensure consistency as migrations
are added."
[database user user-pass verbose?]
[host port database user user-pass verbose?]
(when verbose? (println "Applying changes..."))

(with-open [^Connection db-conn (get-conn database user user-pass)]
(with-open [^Connection db-conn (get-conn host port database user user-pass)]
(setup-migrations-table! db-conn)
(let [all-files (get-migration-files)
completed (get-completed-changes db-conn)
Expand Down
20 changes: 11 additions & 9 deletions test/triangulum/migrate_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
;; Debugging
(def ^:private verbose? false)
(def ^:private tri-test "tri_test")
(def ^:private pg-host (or (System/getenv "PGHOST") "localhost"))
(def ^:private pg-port (or (System/getenv "PGPORT") 5432))

;; Helpers
(defn- get-conn [config]
Expand All @@ -19,15 +21,15 @@
(catch Exception _ (println "Unable to connect to db using:" config))))

(defn- get-admin-conn []
(get-conn {:host (or (System/getenv "PGHOST") "localhost")
:port (or (System/getenv "PGPORT") 5432)
(get-conn {:host pg-host
:port pg-port
:dbname (or (System/getenv "PGDATABASE") "postgres")
:user (or (System/getenv "PGUSERNAME") "postgres")
:password (or (System/getenv "PGPASSWORD") "")}))

(defn- get-tri-test-conn []
(get-conn {:host (or (System/getenv "PGHOST") "localhost")
:port (or (System/getenv "PGPORT") 5432)
(get-conn {:host pg-host
:port pg-port
:dbname tri-test
:user tri-test
:password tri-test}))
Expand Down Expand Up @@ -71,7 +73,7 @@
; Arrange

; Act
(migrate! tri-test tri-test tri-test verbose?)
(migrate! pg-host pg-port tri-test tri-test tri-test verbose?)

; Assert
(with-open [^Connection conn (get-tri-test-conn)]
Expand All @@ -89,7 +91,7 @@
(spit (str m/*migrations-dir* filename) contents)

; Act
(migrate! tri-test tri-test tri-test verbose?)
(migrate! pg-host pg-port tri-test tri-test tri-test verbose?)

; Assert
(with-open [^Connection conn (get-tri-test-conn)]
Expand All @@ -105,7 +107,7 @@

; Act
(dotimes [_ 5]
(migrate! tri-test tri-test tri-test verbose?))
(migrate! pg-host pg-port tri-test tri-test tri-test verbose?))

; Assert
(with-open [^Connection conn (get-tri-test-conn)]
Expand All @@ -126,7 +128,7 @@
"CREATE TABLE pets (id serial PRIMARY KEY, pet_name varchar);")

; Act
(is (thrown? Exception (migrate! tri-test tri-test tri-test verbose?)))
(is (thrown? Exception (migrate! pg-host pg-port tri-test tri-test tri-test verbose?)))

; Assert
(with-open [^Connection conn (get-tri-test-conn)]
Expand All @@ -143,4 +145,4 @@
(spit (str m/*migrations-dir* "01-create-users-table.sql") "CREATE TABLE users (id serial PRIMARY KEY);")

; Act/Assert
(is (thrown? Exception (migrate! tri-test tri-test tri-test false)))))
(is (thrown? Exception (migrate! pg-host pg-port tri-test tri-test tri-test false)))))
Loading