Skip to content

Commit edbb7da

Browse files
authored
Merge pull request #137 from sig-gis/oliver-patch-build-db
Add host and port to backup, restore, and migrate build_db functions
2 parents b1a5e25 + 9629efa commit edbb7da

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/triangulum/build_db.clj

+14-8
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,28 @@
140140
(.read is array 0 5)
141141
(String. array))))
142142

143-
(defn- run-backup [database file admin-pass verbose]
143+
(defn- run-backup [host port database file admin-pass verbose]
144144
(println "Backing up database...")
145145
(sh-wrapper "./"
146146
{:PGPASSWORD admin-pass}
147147
verbose
148-
(format-str "pg_dump -U postgres -d %d --format=custom --compress=4 --file=%f"
148+
(format-str "pg_dump -h %h -p %p -U postgres -d %d --format=custom --compress=4 --file=%f"
149+
host
150+
port
149151
database
150152
file)))
151153

152-
(defn- run-restore [file admin-pass verbose]
154+
(defn- run-restore [host port file admin-pass verbose]
153155
;; TODO check database against 'pg_restore --list file'
154156
(println "Restoring database...")
155157
(if (= "PGDMP" (read-file-tag file))
156158
(sh-wrapper "./"
157159
{:PGPASSWORD admin-pass}
158160
verbose
159-
(str "pg_restore -U postgres -d postgres --clean --if-exists --create --jobs=12 "
160-
file))
161+
(format-str "pg_restore -h %h -p %p -U postgres -d postgres --clean --if-exists --create --jobs=12 %f"
162+
host
163+
port
164+
file))
161165
(println "Invalid .dump file.")))
162166

163167
(def ^:private cli-options
@@ -210,9 +214,11 @@
210214
(or user dbname)
211215
(or password dbname) ; user-pass
212216
verbose)
213-
:backup (run-backup dbname file admin-pass verbose)
214-
:restore (run-restore file admin-pass verbose)
215-
:migrate (migrate! dbname ; TODO we might need consider host and port here.
217+
:backup (run-backup host port dbname file admin-pass verbose)
218+
:restore (run-restore host port file admin-pass verbose)
219+
:migrate (migrate! host
220+
port
221+
dbname
216222
(or user dbname)
217223
(or password dbname) ; user-pass
218224
verbose)

src/triangulum/migrate.clj

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
(defn- migration-path [filename]
1919
(io/file *migrations-dir* filename))
2020

21-
(defn- get-conn [database user user-pass]
21+
(defn- get-conn [host port database user user-pass]
2222
(jdbc/get-connection {:dbtype "postgresql"
2323
:dbname database
24+
:host host
25+
:port port
2426
:user user
2527
:password user-pass
2628
:reWriteBatchedInserts true}))
@@ -104,10 +106,10 @@
104106
and include a SHA-256 hash of the migration file contents. If a migration has
105107
been altered, the migrations will fail. This is to ensure consistency as migrations
106108
are added."
107-
[database user user-pass verbose?]
109+
[host port database user user-pass verbose?]
108110
(when verbose? (println "Applying changes..."))
109111

110-
(with-open [^Connection db-conn (get-conn database user user-pass)]
112+
(with-open [^Connection db-conn (get-conn host port database user user-pass)]
111113
(setup-migrations-table! db-conn)
112114
(let [all-files (get-migration-files)
113115
completed (get-completed-changes db-conn)

test/triangulum/migrate_test.clj

+11-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
;; Debugging
1212
(def ^:private verbose? false)
1313
(def ^:private tri-test "tri_test")
14+
(def ^:private pg-host (or (System/getenv "PGHOST") "localhost"))
15+
(def ^:private pg-port (or (System/getenv "PGPORT") 5432))
1416

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

2123
(defn- get-admin-conn []
22-
(get-conn {:host (or (System/getenv "PGHOST") "localhost")
23-
:port (or (System/getenv "PGPORT") 5432)
24+
(get-conn {:host pg-host
25+
:port pg-port
2426
:dbname (or (System/getenv "PGDATABASE") "postgres")
2527
:user (or (System/getenv "PGUSERNAME") "postgres")
2628
:password (or (System/getenv "PGPASSWORD") "")}))
2729

2830
(defn- get-tri-test-conn []
29-
(get-conn {:host (or (System/getenv "PGHOST") "localhost")
30-
:port (or (System/getenv "PGPORT") 5432)
31+
(get-conn {:host pg-host
32+
:port pg-port
3133
:dbname tri-test
3234
:user tri-test
3335
:password tri-test}))
@@ -71,7 +73,7 @@
7173
; Arrange
7274

7375
; Act
74-
(migrate! tri-test tri-test tri-test verbose?)
76+
(migrate! pg-host pg-port tri-test tri-test tri-test verbose?)
7577

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

9193
; Act
92-
(migrate! tri-test tri-test tri-test verbose?)
94+
(migrate! pg-host pg-port tri-test tri-test tri-test verbose?)
9395

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

106108
; Act
107109
(dotimes [_ 5]
108-
(migrate! tri-test tri-test tri-test verbose?))
110+
(migrate! pg-host pg-port tri-test tri-test tri-test verbose?))
109111

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

128130
; Act
129-
(is (thrown? Exception (migrate! tri-test tri-test tri-test verbose?)))
131+
(is (thrown? Exception (migrate! pg-host pg-port tri-test tri-test tri-test verbose?)))
130132

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

145147
; Act/Assert
146-
(is (thrown? Exception (migrate! tri-test tri-test tri-test false)))))
148+
(is (thrown? Exception (migrate! pg-host pg-port tri-test tri-test tri-test false)))))

0 commit comments

Comments
 (0)