forked from budu/lobos
/
migration.clj
236 lines (212 loc) · 8.92 KB
/
migration.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
;; Copyright (c) Nicolas Buduroi. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 which can be found in the file
;; epl-v10.html at the root of this distribution. By using this software
;; in any fashion, you are agreeing to be bound by the terms of this
;; license.
;; You must not remove this notice, or any other, from this software.
(ns lobos.test.migration
(:refer-clojure :exclude [complement create alter drop])
(:require (lobos [connectivity :as conn]
[schema :as schema]))
(:use (clojure.java [io :only [delete-file]])
clojure.test
(lobos [internal :only [query]]
migration
test)))
;;;; Fixtures
(defn with-config-file-global-connection-fixture [f]
(spit (ns-file *config-namespace*)
(str "(ns lobos.config (:use lobos.connectivity))"
"(open-global " h2-spec ")"))
(f)
(conn/close-global nil true))
(use-fixtures :once
remove-tmp-files-fixture
with-config-file-global-connection-fixture)
;;;; Tests
(deftest test-reverse-rename
(is (= (reverse-rename '(column :foo :to :bar))
'(column :bar :to :foo))
"Should reverse the column definition rename special case.")
(is (= (reverse-rename '(alter :rename
(table :foo
(column :a :to :b)
(column :c :to :d))))
'(alter :rename
(table :foo
(column :b :to :a)
(column :d :to :c))))
(str "Should reverse all column definitions rename special cases "
"inside nested form.")))
(deftest test-complement-create
(is (= (complement '(create (table :users (integer :id))))
'(drop (table :users)))
"Should return a drop table action without cascade clause")
(is (= (complement '(create db (schema :users)))
'(drop db (schema :users) :cascade))
"Should return a drop schema action with a cascade clause"))
(deftest test-complement-alter
(is (= (complement '(alter :add (table :foo (integer :foo))))
'(alter :drop (table :foo (integer :foo))))
"Should return an alter add action"))
(deftest test-append-to-stash-file
(delete-file *stash-file* true)
(append-to-stash-file 'foo)
(is (= (slurp *stash-file*) "\nfoo\n")
"Should create a stash file containing 'foo'")
(append-to-stash-file 'bar)
(is (= (slurp *stash-file*) "\nfoo\n\nbar\n")
"Should appende 'bar' to the stash file"))
(deftest test-clear-stash-file
(delete-file *stash-file* true)
(append-to-stash-file 'foo)
(clear-stash-file)
(is (= (slurp *stash-file*) "")
"Should be empty"))
(deftest test-read-stash-file
(delete-file *stash-file* true)
(append-to-stash-file ':foo)
(append-to-stash-file ':bar)
(is (= (read-stash-file) [:foo :bar])
"Should read the stash file forms into a vector"))
(deftest test-migrations-file
(binding [*migrations-namespace* "foo.bar"]
(is (= (.getPath (migrations-file))
(-> "src/foo/bar.clj"
(.replace \/ java.io.File/separatorChar)))
(str "Should return a File object with a path to the migrations "
"namespace"))))
(deftest test-create-mfile
(delete-file (migrations-file) true)
(create-mfile)
(is (= (slurp (migrations-file))
(.replace
(slurp "test/lobos/test/sample_migrations.clj")
"#_" ""))
"Should create an empty migrations file"))
(deftest test-append-to-mfile
(delete-file (migrations-file) true)
(create-mfile)
(append-to-mfile 'foo "this is a test" '[(println "up")])
(is (.endsWith
(slurp (migrations-file))
"(defmigration foo \"this is a test\" (up [] (println \"up\")))\n")
(str "Should create a migrations file ending with "
"a migration with a docstring and an up clause"))
(append-to-mfile 'bar nil '(println "up") '[(println "down")])
(is (.endsWith
(slurp (migrations-file))
(str "(defmigration bar (up [] println \"up\") "
"(down [] (println \"down\")))\n"))
(str "Should append a migration without a docstring and "
" an up and down clauses")))
(deftest test-list-migrations
(delete-file (migrations-file) true)
(is (nil? (list-migrations))
"Should return nil")
(doseq [name '(foo bar baz)]
(append-to-mfile name nil '[(println "up")]))
(is (= (count (list-migrations)) 3)
"Should return a collection of three elements")
(is (every? #(satisfies? Migration %) (list-migrations))
"All items returned should satisfy the Migration protocol"))
(defmacro with-migrations-table [& body]
`(binding [*db* h2-spec]
(conn/with-connection *db*
(with-schema [~'lobos :lobos]
(create-migrations-table *db* :lobos)
~@body))))
(deftest test-create-migrations-table
(with-migrations-table
(is (= (inspect-schema :elements :lobos_migrations)
(schema/table :lobos_migrations
(schema/varchar :name 255)))
"A table named 'lobos_migrations' should be created")))
(deftest test-insert-and-delete-migrations
(with-migrations-table
(insert-migrations *db* :lobos 'foo)
(is (= (query *db* :lobos :lobos_migrations)
(list {:name "foo"}))
"Should insert a migration entry named 'foo'")
(insert-migrations *db* :lobos 'bar 'baz)
(is (= (query *db* :lobos :lobos_migrations)
(list {:name "foo"}
{:name "bar"}
{:name "baz"}))
"Should insert two migration entries named 'bar' and 'baz'")
(delete-migrations *db* :lobos 'foo)
(is (= (query *db* :lobos :lobos_migrations)
(list {:name "bar"} {:name "baz"}))
"Should delete a migration entry named 'foo'")
(delete-migrations *db* :lobos 'bar 'baz)
(is (empty? (query *db* :lobos :lobos_migrations))
"Should delete all migration entries")))
(deftest test-record
(delete-file *stash-file* true)
(record 'foo)
(is (= (slurp *stash-file*) "\nfoo\n")
"Should record the given action to the stash file")
(clear-stash-file)
(binding [*record* nil] (record 'foo))
(is (= (slurp *stash-file*) "")
"Should not record the given action to the stash file"))
(deftest test-list-migrations-names
(delete-file (migrations-file) true)
(is (empty? (list-migrations-names))
"Should return an empty list")
(doseq [name '(foo bar baz)]
(append-to-mfile name nil '[(println "up")]))
(is (= (list-migrations-names) (list "foo" "bar" "baz"))
"Should return a list containing 'foo' 'bar' and 'baz'"))
(deftest test-query-migrations-table
(with-migrations-table
(is (empty? (query-migrations-table *db* :lobos))
"Should return an empty list")
(insert-migrations *db* :lobos 'foo 'bar)
(is (= (query-migrations-table *db* :lobos)
(list "foo" "bar"))
"Should return a list containing 'foo' and 'bar'")))
(deftest test-pending-migrations
(with-migrations-table
(delete-file (migrations-file) true)
(is (empty? (pending-migrations *db* :lobos))
"Should return an empty list")
(doseq [name '(foo bar baz)]
(append-to-mfile name nil '[(println "up")]))
(is (= (pending-migrations *db* :lobos) (list "foo" "bar" "baz"))
"Should return a list containing 'foo' 'bar' and 'baz'")
(insert-migrations *db* :lobos 'foo 'bar)
(is (= (pending-migrations *db* :lobos) (list "baz"))
"Should return a list containing 'baz'")))
(deftest test-do-migrations
(with-migrations-table
(delete-file (migrations-file) true)
(doseq [n [:foo :bar :baz]]
(generate-migration* *db* :lobos (symbol (name n)) nil
`[(~'create (~'table ~n (~'integer ~n)))]))
(delete-migrations *db* :lobos 'foo 'bar 'baz)
(is (= (pending-migrations *db* :lobos) (list "foo" "bar" "baz"))
"Should return a list containing 'foo' 'bar' and 'baz'")
(do-migrations *db* :lobos :up '[foo] true)
(is (= (pending-migrations *db* :lobos) (list "bar" "baz"))
"Should return a list containing 'bar' and 'baz'")
(do-migrations *db* :lobos :up '[bar baz] true)
(is (empty? (pending-migrations *db* :lobos))
"Should return an empty list")
(do-migrations *db* :lobos :down '[bar] true)
(is (= (pending-migrations *db* :lobos) (list "bar"))
"Should return a list containing 'bar'")
(do-migrations *db* :lobos :down '[foo baz] true)
(is (= (pending-migrations *db* :lobos) (list "foo" "bar" "baz"))
"Should return a list containing 'foo' 'bar' and 'baz'")))
(deftest test-generate-migration*
(with-migrations-table
(delete-file (migrations-file) true)
(generate-migration* *db* :lobos 'foo nil [])
(is (empty? (query-migrations-table *db* :lobos))
"Should return an empty list")
(generate-migration* *db* :lobos 'bar nil '[(println "up")])
(is (= (query-migrations-table *db* :lobos)
(list "bar"))
"Should return a list containing 'bar'")))