/
migrations.clj
66 lines (54 loc) · 1.88 KB
/
migrations.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns andrewslai.clj.persistence.rdbms.migrations
(:require [andrewslai.clj.persistence.rdbms :as rdbms]
[andrewslai.clj.persistence.rdbms.live-pg :as live-pg]
[migratus.core :as m]
[next.jdbc :as next]))
(def MIGRATUS-COMMANDS
{"migrate" m/migrate
"pending" m/pending-list
"rollback" m/rollback
"reset" m/reset
"up" m/up
"down" m/down
"init" m/init
"create" m/create})
(defn -main
"Entry point for running migrations.
Migratus commands take database `config` as their first argument and
additional args after that."
[& [v & args]]
(let [op (get MIGRATUS-COMMANDS v m/migrate)
pg (live-pg/pg-conn (System/getenv))]
(with-open [connection (next/get-connection pg)]
(apply op (concat [{:migration-dirs "migrations"
:store :database
:db {:connection connection}}]
args)))))
(comment
;; MIGHT HAVE TO REQUIRE SOME MODULES... THIS WAS FAILING UNTIL I EVALUATED
;; THE BUFFER WITH THIS MODULE
(m/create {:migration-dirs "migrations"}
"add-image-metadata-table")
(-main "init")
(-main "migrate")
(-main "up")
(require '[migratus.database :as mig-db])
(def connection
(next/fresh-connection (live-pg/pg-conn (System/getenv))))
(def migratus-config
(->migratus-config connection))
(def mystore
(mig-db/->Database connection migratus-config))
(mig-db/table-exists? mystore "schema_migrations")
(import '[java.sql SQLException])
;; Try a transaction
(sql/with-db-transaction [conn mystore]
(println "*********************\n\n")
(try
(println "Connection for the transaction" conn)
(sql/db-set-rollback-only! conn)
(sql/query conn [(str "SELECT 1 FROM " "schema_migrations")])
true
(catch SQLException _
false)))
)