Created
May 18, 2017 14:16
-
-
Save Igosuki/c3ba30513ab8d1f978d9746abc0340da to your computer and use it in GitHub Desktop.
Initializing a database and migrating with ragtime
This file contains 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 db | |
(:require [[ragtime.jdbc :as ragjdbc] | |
[clojure.tools.logging :as log] | |
[clojure.java.jdbc :as jdbc] | |
[ragtime.repl :as ragrepl]]) | |
(defn ragtime-config [database] | |
{:datastore (ragtime.jdbc/sql-database database) | |
:migrations (ragtime.jdbc/load-resources "migrations")}) | |
(defn migrate! [database] | |
(try | |
(log/info ::migrate! "Applying migrations...") | |
(ragtime.repl/migrate (ragtime-config database)) | |
(log/info ::migrate! "Migration successful.") | |
(catch SQLException e | |
(log/error ::migrate! "Failed to migrate the database" | |
:exception e)))) | |
(defn rollback! [database] | |
(try | |
(log/info ::rollback! "Rolling back migrations...") | |
(ragtime.repl/migrate (ragtime-config database)) | |
(log/info ::rollback! "Rollback successful.") | |
(catch SQLException e | |
(log/error ::rollback! "Failed to rollback the database" | |
:exception e)))) | |
(defn -main | |
[& [command-uri database-uri username password]] | |
(doall | |
(jdbc/with-db-connection | |
[database {:uri command-uri}] | |
(log/debug ::-main database) | |
(ensure-user! database username password) | |
(ensure-db! database db username) | |
(ensure-privileges! database db username)) | |
(jdbc/with-db-connection | |
[database {:uri database-uri}] | |
(migrate! database)))) | |
This file contains 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
(defproject ragtime-noice | |
:dependencies [[org.clojure/tools.logging "0.3.1"] | |
; JDBC driver | |
[org.clojure/java.jdbc "0.6.1"] | |
[ragtime "0.6.3"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment