-
Notifications
You must be signed in to change notification settings - Fork 496
/
MigrationTest.kt
310 lines (272 loc) · 11.3 KB
/
MigrationTest.kt
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
304
305
306
307
308
309
310
package app.cash.sqldelight.tests
import app.cash.sqldelight.withCommonConfiguration
import com.google.common.truth.Truth.assertThat
import org.gradle.testkit.runner.GradleRunner
import org.junit.Test
import java.io.File
class MigrationTest {
@Test fun `verification enabled fails when database file is missing`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/missing-database-file-verification-enabled"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains(
"Verifying a migration requires a database file to be present. To generate one, use the generate Gradle task.",
)
}
@Test fun `verification disabled succeeds when database file is missing`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/missing-database-file-verification-disabled"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
}
@Test fun `verification disabled succeeds with hsql dialect`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/verify-migration-disabled-no-errors"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
}
@Test fun `failing migration errors properly`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-failure"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains(
"""
|Error migrating from 1.db, fresh database looks different from migration database:
|/tables[testView] - ADDED
|/tables[test]/columns[test."value"]/ordinalPosition - CHANGED
| BEFORE:
| 2
| AFTER:
| 1
|/tables[test]/columns[test."value"]/partOfIndex - ADDED
|/tables[test]/columns[test.value2]/attributes{IS_NULLABLE} - CHANGED
| BEFORE:
| NO
| AFTER:
| YES
|/tables[test]/columns[test.value2]/nullable - REMOVED
|/tables[test]/columns[test.value2]/ordinalPosition - CHANGED
| BEFORE:
| 1
| AFTER:
| 2
|/tables[test]/definition - CHANGED
| BEFORE:
| CREATE TABLE test (
| value2 TEXT NOT NULL,
| value TEXT NOT NULL
| )
| AFTER:
| CREATE TABLE test (
| value TEXT NOT NULL
| , value2 TEXT)
|/tables[test]/indexes[test.testIndex] - ADDED
|/tables[test]/triggers[test.testTrigger] - ADDED
|
""".trimMargin(),
)
}
@Test fun `schema definition changes fail migration properly`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-definition-failure"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains(
"""
|Error migrating from 1.db, fresh database looks different from migration database:
|/tables[test]/columns[test."value"]/partOfIndex - REMOVED
|/tables[test]/columns[test.value2]/partOfIndex - ADDED
|/tables[test]/indexes[test.testIndex]/columns[test.value2] - ADDED
|/tables[test]/indexes[test.testIndex]/columns[test."value"] - REMOVED
|/tables[test]/triggers[test.testTrigger]/actionStatement - CHANGED
| BEFORE:
| CREATE TRIGGER testTrigger
| AFTER DELETE ON test
| BEGIN
| INSERT INTO test VALUES ("3", "4");
| END
| AFTER:
| CREATE TRIGGER testTrigger
| AFTER DELETE ON test
| BEGIN
| INSERT INTO test VALUES ("1", "2");
| END
|/tables[testView]/definition - CHANGED
| BEFORE:
| CREATE VIEW testView AS
| SELECT *
| FROM test
| WHERE value = 'sup'
| AFTER:
| CREATE VIEW testView AS
| SELECT *
| FROM test
|
""".trimMargin(),
)
}
@Test fun `migration file with errors reports file errors`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-syntax-failure"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains(
"""
|1.sqm: (1, 5): TABLE expected, got 'TABE'
|1 ALTER TABE test ADD COLUMN value2 TEXT
""".trimMargin(),
)
}
@Test fun `deriving schema from migration introduces failures in migration files`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-schema-failure"))
.withArguments("clean", "build", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains(
"""
|1.sqm: (5, 22): No column found with name new_column
|5 INSERT INTO test (id, new_column)
| ^^^^^^^^^^
|6 VALUES ("hello", "world")
""".trimMargin(),
)
}
@Test fun `successful migration works properly`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-success"))
.withArguments("clean", "check", "verifyMainDatabaseMigration", "--stacktrace")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
}
@Test fun `successful migration when folder contains db extension`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-success-db-directory-name"))
.withArguments("clean", "check", "verifyMainDatabaseMigration", "--stacktrace")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
}
@Test fun `multiple databases can have separate migrations`() {
val fixtureRoot = File("src/test/multiple-project-migration-success")
var output = GradleRunner.create()
.withCommonConfiguration(fixtureRoot)
.withArguments("clean", "check", "verifyMainDatabaseAMigration", "--stacktrace")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
output = GradleRunner.create()
.withCommonConfiguration(fixtureRoot)
.withArguments("clean", "check", "verifyMainDatabaseBMigration", "--stacktrace")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
}
@Test fun `don't print java-object-diff warnings on default log level`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-success"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.build()
assertThat(output.output).doesNotContain("Detected circular reference in node at path")
}
@Test fun `print java-object-diff warnings on debug log level`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-success"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace", "--debug")
.build()
assertThat(output.output).contains("""Detected circular reference in node at path /tables[test]/indexes[test.testIndex]/columns[test."value"]/index Going deeper would cause an infinite loop, so I'll stop looking at this instance along the current path.""")
}
@Test fun `migrations with a gap errors properly`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-gap-failure"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains("""Gap in migrations detected. Expected migration 2, got 3.""")
}
@Test fun `migrations with bad name errors properly`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-failure-name"))
.withArguments("clean", "verifyMainDatabaseMigration", "--stacktrace")
.buildAndFail()
assertThat(output.output).contains("Migration files must have an integer value somewhere in their filename but nope.sqm does not.")
}
@Test fun `compilation fails when verifyMigrations is set to true but the migrations are incomplete`() {
val output = GradleRunner.create()
.withCommonConfiguration(File("src/test/migration-incomplete"))
.withArguments("clean", "generateSqlDelightInterface", "--stacktrace", "--debug")
.buildAndFail()
assertThat(output.output).contains("1.sqm: (1, 12): No table found with name test")
}
@Test fun `compilation succeeds when verifyMigrations is set to false but the migrations are incomplete`() {
val fixtureRoot = File("src/test/migration-incomplete-verification-disabled")
val output = GradleRunner.create()
.withCommonConfiguration(fixtureRoot)
.withArguments("clean", "generateSqlDelightInterface", "--stacktrace", "--debug")
.build()
assertThat(output.output).contains("BUILD SUCCESSFUL")
val generatedDatabase = File(fixtureRoot, "build/generated/sqldelight/code/Database/com/example/sqldelightmigrations/DatabaseImpl.kt")
assertThat(generatedDatabase.exists()).isTrue()
assertThat(generatedDatabase.readText()).contains(
"""
|private class DatabaseImpl(
| driver: SqlDriver,
|) : TransacterImpl(driver), Database {
| public override val testQueries: TestQueries = TestQueries(driver)
|
| public object Schema : SqlSchema {
| public override val version: Int
| get() = 2
|
| public override fun create(driver: SqlDriver): QueryResult<Unit> {
| driver.execute(null, ""${'"'}
| |CREATE TABLE test (
| | value TEXT NOT NULL,
| | value2 TEXT
| |)
| ""${'"'}.trimMargin(), 0)
| driver.execute(null, ""${'"'}
| |CREATE VIEW testView AS
| |SELECT *
| |FROM test
| ""${'"'}.trimMargin(), 0)
| driver.execute(null, "CREATE INDEX testIndex ON test(value)", 0)
| driver.execute(null, ""${'"'}
| |CREATE TRIGGER testTrigger
| |AFTER DELETE ON test
| |BEGIN
| |INSERT INTO test VALUES ("1", "2");
| |END
| ""${'"'}.trimMargin(), 0)
| return QueryResult.Unit
| }
|
| public override fun migrate(
| driver: SqlDriver,
| oldVersion: Int,
| newVersion: Int,
| ): QueryResult<Unit> {
| if (oldVersion <= 1 && newVersion > 1) {
| driver.execute(null, "ALTER TABLE test ADD COLUMN value2 TEXT", 0)
| driver.execute(null, "CREATE INDEX testIndex ON test(value)", 0)
| driver.execute(null, ""${'"'}
| |CREATE TRIGGER testTrigger
| |AFTER DELETE ON test
| |BEGIN
| |INSERT INTO test VALUES ("1", "2");
| |END
| ""${'"'}.trimMargin(), 0)
| driver.execute(null, ""${'"'}
| |CREATE VIEW testView AS
| |SELECT *
| |FROM test
| ""${'"'}.trimMargin(), 0)
| }
| return QueryResult.Unit
| }
| }
|}
|
""".trimMargin(),
)
}
}