From 1c26d17ad9111e89e844d6805ac32d3f963faa75 Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Sat, 21 Jan 2017 19:11:17 +0200 Subject: [PATCH] Run pending DB migrations at startup Run pending DB migrations at startup to make deployement simpler (eliminates need to clone repo and run "lein migratus migrate" on the server) --- src/clj/commiteth/db/core.clj | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/clj/commiteth/db/core.clj b/src/clj/commiteth/db/core.clj index cb9c69e..2af3561 100644 --- a/src/clj/commiteth/db/core.clj +++ b/src/clj/commiteth/db/core.clj @@ -4,7 +4,8 @@ [clojure.java.jdbc :as jdbc] [conman.core :as conman] [commiteth.config :refer [env]] - [mount.core :refer [defstate]]) + [mount.core :refer [defstate]] + [migratus.core :as migratus]) (:import org.postgresql.util.PGobject java.sql.Array clojure.lang.IPersistentMap @@ -15,8 +16,18 @@ Timestamp PreparedStatement])) + +(defn start [] + (let [db (env :jdbc-database-url) + migratus-config {:store :database + :migration-dir "migrations/" + :migration-table-name "schema_migrations" + :db db}] + (migratus/migrate migratus-config) + (conman/connect! {:jdbc-url db}))) + (defstate ^:dynamic *db* - :start (conman/connect! {:jdbc-url (env :jdbc-database-url)}) + :start (start) :stop (conman/disconnect! *db*)) (conman/bind-connection *db* "sql/queries.sql")