diff --git a/backend/src/app/db.clj b/backend/src/app/db.clj index 2a8d7b785..8925c12d0 100644 --- a/backend/src/app/db.clj +++ b/backend/src/app/db.clj @@ -46,29 +46,26 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (declare instrument-jdbc!) +(declare apply-migrations!) (s/def ::name keyword?) (s/def ::uri ::us/not-empty-string) (s/def ::min-pool-size ::us/integer) (s/def ::max-pool-size ::us/integer) (s/def ::migrations map?) +(s/def ::read-only ::us/boolean) (defmethod ig/pre-init-spec ::pool [_] - (s/keys :req-un [::uri ::name ::min-pool-size ::max-pool-size ::mtx/metrics] - :opt-un [::migrations])) + (s/keys :req-un [::uri ::name ::min-pool-size ::max-pool-size] + :opt-un [::migrations ::mtx/metrics ::read-only])) (defmethod ig/init-key ::pool - [_ {:keys [migrations metrics] :as cfg}] - (l/info :action "initialize connection pool" - :name (d/name (:name cfg)) - :uri (:uri cfg)) - (instrument-jdbc! (:registry metrics)) + [_ {:keys [migrations metrics name] :as cfg}] + (l/info :action "initialize connection pool" :name (d/name name) :uri (:uri cfg)) + (some-> metrics :registry instrument-jdbc!) + (let [pool (create-pool cfg)] - (when (seq migrations) - (with-open [conn ^AutoCloseable (open pool)] - (mg/setup! conn) - (doseq [[name steps] migrations] - (mg/migrate! conn {:name (d/name name) :steps steps})))) + (some->> (seq migrations) (apply-migrations! pool)) pool)) (defmethod ig/halt-key! ::pool @@ -85,6 +82,13 @@ :name "database_query_total" :help "An absolute counter of database queries."})) +(defn- apply-migrations! + [pool migrations] + (with-open [conn ^AutoCloseable (open pool)] + (mg/setup! conn) + (doseq [[name steps] migrations] + (mg/migrate! conn {:name (d/name name) :steps steps})))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; API & Impl ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -94,28 +98,34 @@ "SET idle_in_transaction_session_timeout = 200000;")) (defn- create-datasource-config - [{:keys [metrics] :as cfg}] + [{:keys [metrics read-only] :or {read-only false} :as cfg}] (let [dburi (:uri cfg) username (:username cfg) password (:password cfg) - config (HikariConfig.) - mtf (PrometheusMetricsTrackerFactory. (:registry metrics))] + config (HikariConfig.)] (doto config (.setJdbcUrl (str "jdbc:" dburi)) (.setPoolName (d/name (:name cfg))) (.setAutoCommit true) - (.setReadOnly false) + (.setReadOnly read-only) (.setConnectionTimeout 10000) ;; 10seg (.setValidationTimeout 10000) ;; 10seg (.setIdleTimeout 120000) ;; 2min (.setMaxLifetime 1800000) ;; 30min (.setMinimumIdle (:min-pool-size cfg 0)) (.setMaximumPoolSize (:max-pool-size cfg 30)) - (.setMetricsTrackerFactory mtf) (.setConnectionInitSql initsql) (.setInitializationFailTimeout -1)) + + ;; When metrics namespace is provided + (when metrics + (->> (:registry metrics) + (PrometheusMetricsTrackerFactory.) + (.setMetricsTrackerFactory config))) + (when username (.setUsername config username)) (when password (.setPassword config password)) + config)) (defn pool? diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index 08c8ef10d..456cd4725 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -44,8 +44,6 @@ :help "A total number of bytes processed by update-file." :type :counter}}} - - :app.migrations/all {:main (ig/ref :app.migrations/migrations)} diff --git a/backend/src/app/notifications.clj b/backend/src/app/notifications.clj index f426923a9..62490b212 100644 --- a/backend/src/app/notifications.clj +++ b/backend/src/app/notifications.clj @@ -69,6 +69,7 @@ :mtx-messages mtx-messages :mtx-sessions mtx-sessions )] + (-> #(handler cfg %) (wrap-session) (wrap-keyword-params)