Last active
January 31, 2018 20:33
-
-
Save prasann/11e62167434eb76a675b54a5b5b672e4 to your computer and use it in GitHub Desktop.
Clojure utility to run database migration with flyway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns app.migration | |
(:require [environ.core :refer [env]]) | |
(:import org.flywaydb.core.Flyway | |
org.flywaydb.core.internal.info.MigrationInfoDumper)) | |
;; Build DB String from the Environment Variables | |
(def db-url (str "jdbc:postgresql://" | |
(env :pg-db-host) ":" | |
(env :pg-db-port) "/" (env :pg-db-name))) | |
;; Initialize Flyway object | |
(def flyway | |
(let [locations (into-array String ["classpath:db/migration"])] | |
(doto (new Flyway) | |
(.setDataSource db-url (env :pg-db-user) (env :pg-db-password) (into-array String [])) | |
(.setLocations locations)))) | |
(defn migrate [] (.migrate flyway)) | |
(defn clean [] (.clean flyway)) | |
(defn reset [] (clean) (migrate)) | |
(defn info [] | |
(println (MigrationInfoDumper/dumpToAsciiTable (.all (.info flyway))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment