|
1 | 1 | (ns com.vennbilling.system.storage |
2 | 2 | (:require [integrant.core :as ig] |
3 | 3 | [malli.core :as m] |
4 | | - [next.jdbc.connection :refer [jdbc-url]] |
5 | 4 |
|
6 | 5 | [com.vennbilling.logging.interface :as log] |
7 | | - [com.vennbilling.database.interface] |
8 | | - [migratus.core :as migratus]) |
| 6 | + [com.vennbilling.database.interface :as database]) |
9 | 7 |
|
10 | | - (:import [com.zaxxer.hikari HikariDataSource] |
11 | | - [java.sql Connection SQLException] |
12 | | - |
13 | | - [clojure.lang ExceptionInfo])) |
| 8 | + (:import [clojure.lang ExceptionInfo])) |
14 | 9 |
|
15 | 10 | ;; Supported Storage Engines |
16 | 11 | (derive :storage.type/postgresql :db/connection) |
|
42 | 37 | [:password {:optional true} :string] |
43 | 38 | [:migrations DBMigrationsConfig]]) |
44 | 39 |
|
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 | | - |
52 | 40 | (defn- build-migrations-config |
53 | 41 | "Returns a map that represents a configuration used by migratus.core" |
54 | | - [^HikariDataSource datasource {:keys [directory managed-connection]}] |
| 42 | + [datasource {:keys [directory managed-connection]}] |
55 | 43 | {:store :database |
56 | 44 | :migration-dir directory |
57 | 45 | :db {:datasource datasource |
|
71 | 59 | (try |
72 | 60 | (let [valid-db-config (m/coerce DBConfig db-config) |
73 | 61 | 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}) |
100 | 72 | (catch ExceptionInfo e |
101 | 73 | (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)))))) |
106 | 75 |
|
107 | 76 | (defmethod ig/halt-key! :db/connection |
108 | 77 | [storage-type db] |
|
0 commit comments