-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.clj
78 lines (63 loc) · 2.25 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
67
68
69
70
71
72
73
74
75
76
77
78
(ns andrewslai.clj.persistence.migrations
(:require [andrewslai.clj.utils :as util]
[clojure.java.jdbc :as sql]
[migratus.core :as m]))
(defn pg-db->migratus-config [{:keys [host dbname user password]}]
{:migration-dirs "migrations"
:store :database
:db {:classname "com.postgresql.jdbc.Driver"
:subprotocol "postgresql"
:subname (str "//" host "/" dbname)
:user user
:password password}})
#_(pg-db->migratus-config (util/pg-conn))
#_{:dbtype "postgresql"
, :dbname "andrewslai_db",
:host "localhost",
:user "andrewslai",
:password "andrewslai"}
(defn -main [& [v & args]]
(let [ops {"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}
op (or (ops v) m/migrate)]
(apply op (concat [(pg-db->migratus-config (util/pg-conn))]
args))))
(comment
;; MIGHT HAVE TO REQUIRE SOME MODULES... THIS WAS FAILING UNTIL I EVALUATED
;; THE BUFFER WITH THIS MODULE
(-main "create" "seed-articles-table")
(-main "init")
(-main "migrate")
(-main "up")
(require '[migratus.database :as mig-db])
(require '[migratus.protocols :as prot])
(def my-connection (atom (mig-db/connect* (:db (pg-db->migratus-config (util/pg-conn))))))
(def mystore
(mig-db/->Database (:connection @my-connection) (pg-db->migratus-config (util/pg-conn))))
@my-connection
mystore
(import '[java.sql SQLException])
(sql/with-db-transaction
[t-con mystore]
(println "*********************\n\n")
(try
(println "t-conn" t-con)
(sql/db-set-rollback-only! t-con)
(sql/query t-con [(str "SELECT 1 FROM " "schema_migrations")])
true
(catch SQLException _
false)))
(mig-db/table-exists? mystore "schema_migrations")
(import [org.postgresql.util PSQLException])
(try (mig-db/table-exists? mystore "schema_migrations")
#_(throw PSQLException)
(catch org.postgresql.util.PSQLException _
(println "Caught PSQL exception")) (catch java.sql.SQLException _
(println "Caught SQL exception")))
)