Skip to content

Commit 59540d8

Browse files
Move JDBC / Migratus logic to database component (#50)
1 parent 5112225 commit 59540d8

File tree

3 files changed

+72
-47
lines changed

3 files changed

+72
-47
lines changed

components/database/src/com/vennbilling/database/core.clj

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
11
(ns com.vennbilling.database.core
22
(:require
3+
[honey.sql :as sql]
4+
[migratus.core :as migratus]
35
[next.jdbc :as jdbc]
4-
[honey.sql :as sql])
6+
[next.jdbc.connection :refer [jdbc-url]]
57

6-
(:import (javax.sql DataSource)))
8+
[com.vennbilling.logging.interface :as log])
9+
10+
(:import [javax.sql DataSource]
11+
[java.sql SQLException]
12+
[com.zaxxer.hikari HikariDataSource]))
13+
14+
(defn- build-jdbc-url
15+
[dbtype config]
16+
(let [{:keys [database-name]} config
17+
conn (dissoc config :database-name)
18+
db (assoc conn :dbname database-name :dbtype dbtype)]
19+
(jdbc-url db)))
20+
21+
(defn establish-connection
22+
[dbtype config]
23+
(let [jdbc-url (build-jdbc-url dbtype config)
24+
ds (HikariDataSource.)]
25+
26+
(try
27+
;; Configure the datastore
28+
(-> ds
29+
(.setJdbcUrl jdbc-url))
30+
31+
;; Test DB Connection
32+
(log/info (str "Testing database connection to " dbtype "..."))
33+
(let [test-conn (.getConnection ds)]
34+
(log/info (str "Database connection to " dbtype " is healthy"))
35+
(.close test-conn)
36+
37+
;; Return the datasource
38+
ds)
39+
40+
(catch SQLException e
41+
(let [ex (ex-info "Unable to connect to database" {:cause :sql-exception :exception e})]
42+
(throw ex))))))
43+
44+
(defn bootstrap
45+
[migratus-config]
46+
(if (seq (migratus/pending-list migratus-config))
47+
(do
48+
(log/info "Bootstrapping database. Running all migrations...")
49+
(migratus/migrate migratus-config)
50+
(log/info "Bootstrapping complete."))
51+
52+
(log/info "Skipped bootstrapping database. Schema is up to date.")))
753

854
(defn exec!
955
[conn q]

components/database/src/com/vennbilling/database/interface.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
(ns com.vennbilling.database.interface
22
(:require [com.vennbilling.database.core :as db]))
33

4+
(defn establish-connection
5+
"Establishes a connection database using the provided configuration and returns a javax.sql.Datasource."
6+
[dbtype config]
7+
(db/establish-connection dbtype config))
8+
9+
(defn bootstrap
10+
"Bootstraps a database, running all pending migrations with migratus"
11+
[migratus-config]
12+
(db/bootstrap migratus-config))
13+
414
(defn exec!
515
"Executes a query designed to not return any rows"
616
[conn q]

components/system/src/com/vennbilling/system/storage.clj

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
(ns com.vennbilling.system.storage
22
(:require [integrant.core :as ig]
33
[malli.core :as m]
4-
[next.jdbc.connection :refer [jdbc-url]]
54

65
[com.vennbilling.logging.interface :as log]
7-
[com.vennbilling.database.interface]
8-
[migratus.core :as migratus])
6+
[com.vennbilling.database.interface :as database])
97

10-
(:import [com.zaxxer.hikari HikariDataSource]
11-
[java.sql Connection SQLException]
12-
13-
[clojure.lang ExceptionInfo]))
8+
(:import [clojure.lang ExceptionInfo]))
149

1510
;; Supported Storage Engines
1611
(derive :storage.type/postgresql :db/connection)
@@ -42,16 +37,9 @@
4237
[:password {:optional true} :string]
4338
[:migrations DBMigrationsConfig]])
4439

45-
(defn- build-jdbc-url
46-
[dbtype config]
47-
(let [{:keys [database-name]} config
48-
conn (dissoc config :database-name)
49-
db (assoc conn :dbname database-name :dbtype dbtype)]
50-
(jdbc-url db)))
51-
5240
(defn- build-migrations-config
5341
"Returns a map that represents a configuration used by migratus.core"
54-
[^HikariDataSource datasource {:keys [directory managed-connection]}]
42+
[datasource {:keys [directory managed-connection]}]
5543
{:store :database
5644
:migration-dir directory
5745
:db {:datasource datasource
@@ -71,38 +59,19 @@
7159
(try
7260
(let [valid-db-config (m/coerce DBConfig db-config)
7361
dbtype (name storage-type)
74-
db (dissoc valid-db-config :migrations)
75-
jdbc-url (build-jdbc-url dbtype db)
76-
^HikariDataSource ds (new HikariDataSource)]
77-
78-
;; Configure the datastore
79-
(-> ds
80-
(.setJdbcUrl jdbc-url))
81-
82-
;; Test DB Connection
83-
(log/info (str "Testing database connection to " dbtype "..."))
84-
(let [^Connection test-conn (.getConnection ds)]
85-
(log/info (str "Database connection to " dbtype " is healthy"))
86-
(.close test-conn)
87-
88-
;; Configure Migrations
89-
(let [migratus-config (build-migrations-config ds migrations)]
90-
91-
(if (and (bootstrap? storage-type) (seq (migratus/pending-list migratus-config)))
92-
(do
93-
(log/info "Bootstrapping database. Running all migrations...")
94-
(migratus/migrate migratus-config)
95-
(log/info "Bootstrapping complete."))
96-
(log/info "Skipped bootstrapping database. Schema is up to date."))
97-
98-
(log/info (str "Database " dbtype " is ready to accept connections."))
99-
{:datasource ds :migrations-config migratus-config})))
62+
db-connection-config (dissoc valid-db-config :migrations)
63+
ds (database/establish-connection dbtype db-connection-config)
64+
migratus-config (build-migrations-config ds migrations)]
65+
66+
(when (bootstrap? storage-type)
67+
(database/bootstrap migratus-config))
68+
69+
(log/info (str "Database " dbtype " is configured and ready to accept connections."))
70+
71+
{:datasource ds :migrations-config migratus-config})
10072
(catch ExceptionInfo e
10173
(log/exception e)
102-
(throw (IllegalArgumentException. (ex-message e))))
103-
(catch SQLException e
104-
(log/exception (ex-info "Unable to connect to database" {:cause :sql-exception :exception e}))
105-
(throw e))))
74+
(throw (IllegalArgumentException. (ex-message e))))))
10675

10776
(defmethod ig/halt-key! :db/connection
10877
[storage-type db]

0 commit comments

Comments
 (0)