/
precondition.clj
303 lines (263 loc) · 13.1 KB
/
precondition.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
(ns clj-liquibase.precondition
"Clojure wrappers for liquibase.change.Change implementations.
See also:
http://www.liquibase.org/documentation/index (Available Database Refactorings)"
(:require
[clj-jdbcutil.core :as sp]
[clj-miscutil.core :as mu]
[clj-liquibase.internal :as in])
(:import
(liquibase.precondition Precondition PreconditionLogic)
(liquibase.precondition.core ChangeLogPropertyDefinedPrecondition
ChangeSetExecutedPrecondition
ColumnExistsPrecondition
DBMSPrecondition
ForeignKeyExistsPrecondition
IndexExistsPrecondition
PrimaryKeyExistsPrecondition
RunningAsPrecondition
SequenceExistsPrecondition
SqlPrecondition
TableExistsPrecondition
ViewExistsPrecondition
;; -- containers ---
AndPrecondition
NotPrecondition
OrPrecondition
PreconditionContainer)))
(defn pre-cond?
"Return true if given argument is a pre-condition, false otherwise."
[x]
(instance? Precondition x))
(defn changelog-prop-defined
"Change-log property defined"
^ChangeLogPropertyDefinedPrecondition
[prop value] {:post [(mu/verify-cond (instance? ChangeLogPropertyDefinedPrecondition %))]
:pre [(mu/verify-arg (or (keyword? prop) (string? prop)))
(mu/verify-arg (or (keyword? value) (string? value)))]}
(let [pc (ChangeLogPropertyDefinedPrecondition.)]
(doto pc
(.setProperty (mu/as-string prop))
(.setValue (mu/as-string value)))
pc))
(defn changeset-executed
"Change-set executed"
^ChangeSetExecutedPrecondition
[file id author] {:post [(mu/verify-cond (instance? ChangeSetExecutedPrecondition %))]
:pre [(mu/verify-arg (string? file))
(mu/verify-arg (mu/not-nil? id))
(mu/verify-arg (or (keyword? author) (string? author)))]}
(let [pc (ChangeSetExecutedPrecondition.)]
(doto pc
(.setChangeLogFile ^String file)
(.setId (mu/as-string id))
(.setAuthor (mu/as-string author)))
pc))
(defn column-exists
"Specified column exists"
^ColumnExistsPrecondition
[schema-name table-name column-name] {:post [(mu/verify-cond (instance? ColumnExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? table-name) (string? table-name)))
(mu/verify-arg (or (keyword? column-name) (string? column-name)))]}
(let [pc (ColumnExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setTableName (sp/db-iden table-name))
(.setColumnName (sp/db-iden column-name)))
pc))
;; wrapper-based custom preconditions are not supported (they need class name)
(defn dbms
"Check database type. Example:
(dbms :mysql)"
^DBMSPrecondition
[db-type] {:post [(mu/verify-cond (instance? DBMSPrecondition %))]
:pre [(mu/verify-arg (or (keyword? db-type) (string? db-type)))]}
(let [pc (DBMSPrecondition.)]
(doto pc
(.setType (mu/as-string db-type)))
pc))
;; TODO - do we really need *both* `table-name` and `key-name` arguments?
(defn foreign-key-exists
"Return Precondition that asserts given Foreign key exists"
^ForeignKeyExistsPrecondition
[schema-name table-name key-name] {:post [(mu/verify-cond (instance? ForeignKeyExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? table-name) (string? table-name)))
(mu/verify-arg (or (keyword? key-name) (string? key-name)))]}
(let [pc (ForeignKeyExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setForeignKeyTableName (sp/db-iden table-name))
(.setForeignKeyName (sp/db-iden key-name)))
pc))
(defn index-exists
^IndexExistsPrecondition
[schema-name table-name column-names index-name] {:post [(mu/verify-cond (instance? IndexExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? table-name) (string? table-name)))
(mu/verify-arg (or (keyword? column-names) (string? column-names)
(and (coll? column-names)
(every? #(or (keyword? %) (string? %))
column-names))))
(mu/verify-arg (or (keyword? index-name) (string? index-name)))]}
(let [pc (IndexExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setTableName (sp/db-iden table-name))
(.setColumnNames (mu/comma-sep-str
(map sp/db-iden (mu/as-vector column-names))))
(.setIndexName (sp/db-iden index-name)))
pc))
(defn primary-key-exists
^PrimaryKeyExistsPrecondition
[schema-name table-name primary-key-name] {:post [(mu/verify-cond (instance? PrimaryKeyExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? primary-key-name) (string? primary-key-name)))
(mu/verify-arg (or (keyword? table-name) (string? table-name)))]}
(let [pc (PrimaryKeyExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setPrimaryKeyName (sp/db-iden primary-key-name))
(.setTableName (sp/db-iden table-name)))
pc))
(defn running-as
"Verify database user name"
^RunningAsPrecondition
[^String user-name] {:post [(mu/verify-cond (instance? RunningAsPrecondition %))]
:pre [(mu/verify-arg (string? user-name))]}
(let [pc (RunningAsPrecondition.)]
(.setUsername pc user-name)
pc))
(defn sequence-exists
"Verify that given sequence exists"
^SequenceExistsPrecondition
[schema-name sequence-name] {:post [(mu/verify-cond (instance? SequenceExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? sequence-name) (string? sequence-name)))]}
(let [pc (SequenceExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setSequenceName (sp/db-iden sequence-name)))
pc))
(defn sql
"SQL Check"
^SqlPrecondition
[expected ^String sql-stmt] {:post [(mu/verify-cond (instance? SqlPrecondition %))]
:pre [(mu/verify-arg (string? sql-stmt))]}
(let [pc (SqlPrecondition.)]
(doto pc
(.setExpectedResult (or (nil? expected) nil
(mu/as-string expected)))
(.setSql sql-stmt))
pc))
(defn table-exists
"Verify that said table exists"
^TableExistsPrecondition
[schema-name table-name] {:post [(mu/verify-cond (instance? TableExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? table-name) (string? table-name)))]}
(let [pc (TableExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setTableName (sp/db-iden table-name)))
pc))
(defn view-exists
"Verify that view exists"
^ViewExistsPrecondition
[schema-name view-name] {:post [(mu/verify-cond (instance? ViewExistsPrecondition %))]
:pre [(mu/verify-arg (or (keyword? schema-name) (string? schema-name)))
(mu/verify-arg (or (keyword? view-name) (string? view-name)))]}
(let [pc (ViewExistsPrecondition.)]
(doto pc
(.setSchemaName (sp/db-iden schema-name))
(.setViewName (sp/db-iden view-name)))
pc))
(defn pc-and
"Verify that ALL immediate nested preconditions are met"
^AndPrecondition
[pc & more] {:post [(mu/verify-cond (instance? AndPrecondition %))]
:pre [(mu/verify-arg (every? #(instance? Precondition %)
(into [pc] more)))]}
(let [pl (AndPrecondition.)]
(doseq [each (into [pc] more)]
(.addNestedPrecondition pl each))
pl))
(defn pc-not
"Verify that NONE OF immediate nested preconditions is met"
^NotPrecondition
[pc & more] {:post [(mu/verify-cond (instance? NotPrecondition %))]
:pre [(mu/verify-arg (every? #(instance? Precondition %)
(into [pc] more)))]}
(let [pl (NotPrecondition.)]
(doseq [each (into [pc] more)]
(.addNestedPrecondition pl each))
pl))
(defn pc-or
"Verify that ANY OF immediate nested preconditions is met"
^OrPrecondition
[pc & more] {:post [(mu/verify-cond (instance? OrPrecondition %))]
:pre [(mu/verify-arg (every? #(instance? Precondition %)
(into [pc] more)))]}
(let [pl (OrPrecondition.)]
(doseq [each (into [pc] more)]
(.addNestedPrecondition pl each))
pl))
(def on-fail-error-values
{:halt "HALT" ; Immediately halt execution of entire change log [default]
:continue "CONTINUE" ; Skip over change set. Execution of change set will be attempted again on the next update. Continue with change log.
:mark-ran "MARK_RAN" ; Skip over change set, but mark it as ran. Continue with change log
:warn "WARN" ; Output warning and continue executing change set as normal.
})
(def on-update-sql-values
{:ignore "IGNORE" ; Ignore the preCondition in updateSQL mode
:test "TEST" ; Test the changeSet in updateSQL mode
:fail "FAIL" ; Fail the preCondition in updateSQL mode
})
(defn pre-cond
"Return a PreconditionContainer that verifies that ALL immediate nested
preconditions are met.
Optional args:
:on-fail What to do when preconditions fail; either of
:halt (default), :continue, :mark-ran, :warn
:on-fail-msg Custom message (string) to output when preconditions fail
:on-error What to do when preconditions error; either of
:halt (default), :continue, :mark-ran, :warn
:on-error-msg Custom message (string) to output when preconditions fail
:on-update-sql What to do in updateSQL mode; either of
:run, :fail, :ignore
See also:
http://www.liquibase.org/documentation/preconditions"
^PreconditionContainer
[pre-cond-list
& {:keys [on-fail ; onFail -- What to do when preconditions fail
on-fail-msg ; onFailMessage -- Custom message to output when preconditions fail
on-error ; onError -- What to do when preconditions error
on-error-msg ; onErrorMessage -- Custom message to output when preconditions fail
on-update-sql ; onUpdateSQL -- What to do in updateSQL mode
]
:or {on-fail nil
on-fail-msg nil
on-error nil
on-error-msg nil
on-update-sql nil}
:as opt}]
{:post [(mu/verify-cond (instance? PreconditionContainer %))]
:pre [(mu/verify-opt #{:on-fail :on-fail-msg :on-error :on-error-msg
:on-update-sql} opt)
(mu/verify-arg (and (coll? pre-cond-list)
(every? #(instance? Precondition %) pre-cond-list)))
(mu/verify-arg (or (nil? on-fail) (contains? on-fail-error-values on-fail)))
(mu/verify-arg (or (nil? on-error) (contains? on-fail-error-values on-error)))
(mu/verify-arg (or (nil? on-update-sql) (contains? on-update-sql-values on-update-sql)))
(mu/verify-arg (or (nil? on-fail-msg) (string? on-fail-msg)))
(mu/verify-arg (or (nil? on-error-msg) (string? on-error-msg)))]}
(let [pc (PreconditionContainer.)]
(doseq [each pre-cond-list]
(.addNestedPrecondition pc each))
(when on-fail (.setOnFail pc (on-fail on-fail-error-values)))
(when on-error (.setOnError pc (on-error on-fail-error-values)))
(when on-update-sql (.setOnSqlOutput pc ^String (on-update-sql on-update-sql-values)))
(when on-fail-msg (.setOnFailMessage pc on-fail-msg))
(when on-error-msg (.setOnErrorMessage pc on-error-msg))
pc))