-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
130 lines (116 loc) · 3.44 KB
/
core.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(ns firma-analysis.ports.sql.core
(:require [clojure.java.io :as io]
[firma-analysis.configs :as configs]
[next.jdbc :as jdbc]
[next.jdbc.prepare :as jdbc.prepare]
[next.jdbc.result-set :as jdbc.result-set]
[outpace.config :refer [defconfig]])
(:import (clojure.lang PersistentVector Var$Unbound)
[java.sql
Array
Date
PreparedStatement
Time
Timestamp]))
(defconfig db-type "h2:mem")
(defconfig subname "")
(defconfig subprotocol "")
(defconfig host "")
(defconfig port "")
(defconfig db-name "firma;MODE=PostgreSQL")
(defconfig username "")
(defconfig password "")
(def raw-connection
{:dbtype db-type
:subname subname
:subprotocol subprotocol
:host host
:port port
:dbname db-name
:user username
:password password
:ssl false})
(def ^:private connection ;removes unbound values
(into {} (filter (fn [[_ v]]
(not
(= (class v) Var$Unbound)))
raw-connection)))
(def datasource
(jdbc/get-datasource connection))
(defn exec-migration-file
[_ file]
(println "migrating" (.getName file))
(let [sql-command (slurp file)]
(jdbc/with-transaction
[tx datasource]
(jdbc/execute! tx [sql-command]))))
(defn migrate
[]
(let [files (->> (io/resource "migrations")
io/file
file-seq
(filter #(re-matches #"^[1-9]{1,}.sql$" (.getName %)))
(sort-by #(.getName %)))]
(jdbc/with-transaction
[tx datasource]
(mapv #(exec-migration-file tx %) files))))
(defn- file-seq-if-existing
[folder]
(if folder (file-seq folder)
[]))
(defn- get-files-map
[res-path]
(->> (io/resource res-path)
io/file
file-seq-if-existing
(filter #(re-matches #"^[1-9]{1,}.sql$" (.getName %)))
(sort-by #(.getName %))
(mapv (fn [f] (let [n (.getName f)] [n f])))
(into {})))
(defn migrate-test
[]
(let [files (get-files-map "migrations")
test-files (->> (get-files-map "migrations_test")
(merge files))]
(jdbc/with-transaction
[tx datasource]
(mapv #(exec-migration-file tx %) (vals test-files)))))
(defn teardown
[]
(let [t-file (io/file "resources/migrations/teardown.sql")]
(jdbc/with-transaction
[tx datasource]
(exec-migration-file tx t-file))))
(defn convert-sql-array [^Array v]
(let [x (first (.getArray v))
f (cond (instance? Date x) #(.toLocalDate %)
(instance? Time x) #(.toLocalTime %)
(instance? Timestamp x) #(.toLocalDateTime %)
:else identity)]
(vec (map f (.getArray v)))))
(defn override-protocols
[]
(extend-protocol jdbc.prepare/SettableParameter
PersistentVector
(set-parameter [^PersistentVector v ^PreparedStatement ps ^long i]
(.setObject ps i (into-array v))))
(extend-protocol jdbc.result-set/ReadableColumn
Array
(read-column-by-label [^Array v _]
(convert-sql-array v))
(read-column-by-index [^Array v _2 _3]
(convert-sql-array v))))
(defn start
[migrate?]
(override-protocols)
(cond migrate? (do
(when (configs/env-test?)
(teardown))
(migrate)
(when (configs/env-test?)
(teardown)))
:else nil))
(defn reset
[]
(teardown)
(migrate))