This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
migration_test.go
405 lines (377 loc) · 16.5 KB
/
migration_test.go
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package reiner
import (
"testing"
_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
)
var migration *Migration
func TestMigrationMain(t *testing.T) {
assert := assert.New(t)
builder, err := New("root:root@/test?charset=utf8")
migration = builder.Migration()
assert.NoError(err)
}
func TestMigrationBasic(t *testing.T) {
assert := assert.New(t)
err := migration.Table("test_table").Column("test").Varchar(32).Primary().Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table` (`test` VARCHAR(32) NOT NULL PRIMARY KEY) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationDrop(t *testing.T) {
assert := assert.New(t)
err := migration.Drop("test_table")
assert.NoError(err)
assert.Equal("DROP TABLE `test_table`", migration.LastQuery)
}
func TestMigrationDropIfExists(t *testing.T) {
assert := assert.New(t)
err := migration.DropIfExists("test_table")
assert.NoError(err)
assert.Equal("DROP TABLE IF EXISTS `test_table`", migration.LastQuery)
}
func TestMigrationDataTypes(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table1").
Column("test").TinyInt(1).
Column("test2").SmallInt(1).
Column("test3").MediumInt(1).
Column("test4").Int(1).
Column("test5").BigInt(1).
Column("test6").Char(1).
Column("test7").Varchar(1).
Column("test8").Binary(1).
Column("test9").VarBinary(1).
Column("test10").Bit(1).
Column("test11").TinyText().
Column("test12").Text().
Column("test13").MediumText().
Column("test14").LongText().
Column("test15").TinyBlob().
Column("test16").Blob().
Column("test17").MediumBlob().
Column("test18").LongBlob().
Column("test19").Date().
Column("test20").DateTime().
Column("test21").Time().
Column("test22").Timestamp().
Column("test23").Year().
Column("test24").Double(2, 1).
Column("test25").Decimal(2, 1).
Column("test26").Float(2, 1).
Column("test27").Float(1).
Column("test28").Enum("1", "2", "3", "A", "B", "C").
Column("test29").Set("1", "2", "3", "A", "B", "C").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table1` (`test` TINYINT(1) NOT NULL , `test2` SMALLINT(1) NOT NULL , `test3` MEDIUMINT(1) NOT NULL , `test4` INT(1) NOT NULL , `test5` BIGINT(1) NOT NULL , `test6` CHAR(1) NOT NULL , `test7` VARCHAR(1) NOT NULL , `test8` BINARY(1) NOT NULL , `test9` VARBINARY(1) NOT NULL , `test10` BIT(1) NOT NULL , `test11` TINYTEXT NOT NULL , `test12` TEXT NOT NULL , `test13` MEDIUMTEXT NOT NULL , `test14` LONGTEXT NOT NULL , `test15` TINYBLOB NOT NULL , `test16` BLOB NOT NULL , `test17` MEDIUMBLOB NOT NULL , `test18` LONGBLOB NOT NULL , `test19` DATE NOT NULL , `test20` DATETIME NOT NULL , `test21` TIME NOT NULL , `test22` TIMESTAMP NOT NULL , `test23` YEAR NOT NULL , `test24` DOUBLE(2, 1) NOT NULL , `test25` DECIMAL(2, 1) NOT NULL , `test26` FLOAT(2, 1) NOT NULL , `test27` FLOAT(1) NOT NULL , `test28` ENUM('1', '2', '3', 'A', 'B', 'C') NOT NULL , `test29` SET('1', '2', '3', 'A', 'B', 'C') NOT NULL) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationTableType(t *testing.T) {
assert := assert.New(t)
err := migration.Table("test_myisam_table").Column("test").Varchar(32).Engine(EngineMyISAM).Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_myisam_table` (`test` VARCHAR(32) NOT NULL) ENGINE=MYISAM", migration.LastQuery)
err = migration.Table("test_innodb_table").Column("test").Varchar(32).Engine(EngineInnoDB).Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_innodb_table` (`test` VARCHAR(32) NOT NULL) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationDefault(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_default_null_table").
Column("test").
Varchar(32).
Nullable().
Default(nil).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_default_null_table` (`test` VARCHAR(32) DEFAULT NULL) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_default_string_table").
Column("test").
Varchar(32).
Default("string").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_default_string_table` (`test` VARCHAR(32) NOT NULL DEFAULT 'string') ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_default_int_table").
Column("test").
Int(32).
Default(12).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_default_int_table` (`test` INT(32) NOT NULL DEFAULT 12) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_default_timestamp_table").
Column("test").
Timestamp().
Default("CURRENT_TIMESTAMP").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_default_timestamp_table` (`test` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_on_update_timestamp_table").
Column("test").
Timestamp().
Default("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_on_update_timestamp_table` (`test` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_default_timestamp_table").
Column("test").
Timestamp().
Default("CURRENT_TIMESTAMP").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_default_timestamp_table` (`test` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_default_datetime_table").
Column("test").
DateTime().
Default("NOW()").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_default_datetime_table` (`test` DATETIME NOT NULL DEFAULT NOW()) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationNullable(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_nullable_table").
Column("test").
Varchar(32).
Nullable().
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_nullable_table` (`test` VARCHAR(32) DEFAULT NULL) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationUnsigned(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_unsigned_table").
Column("test").
Int(10).
Unsigned().
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_unsigned_table` (`test` INT(10) UNSIGNED NOT NULL) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationAutoIncrement(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_auto_increment_table").
Column("test").
Int(10).
AutoIncrement().
Primary().
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_auto_increment_table` (`test` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationComment(t *testing.T) {
assert := assert.New(t)
err := migration.Table("test_column_comment_table").Column("test").Int(10).Comment("月月,搭拉安!").Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_column_comment_table` (`test` INT(10) NOT NULL COMMENT '月月,搭拉安!') ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationTableComment(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_comment_table", "月月,搭拉安!").
Column("test").
Int(10).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_comment_table` (`test` INT(10) NOT NULL) ENGINE=INNODB, COMMENT='月月,搭拉安!'", migration.LastQuery)
}
func TestMigrationPrimaryKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table2").
Column("test").Varchar(32).Primary().
Column("test2").Varchar(32).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table2` (`test` VARCHAR(32) NOT NULL PRIMARY KEY , `test2` VARCHAR(32) NOT NULL) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationNamingPrimaryKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table3").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Primary("pk_test", []string{"test", "test2"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table3` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, PRIMARY KEY `pk_test` (`test`,`test2`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationMultiPrimaryKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table4").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Primary([]string{"test", "test2"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table4` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, PRIMARY KEY (`test`,`test2`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationUniqueKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table5").
Column("test").Varchar(32).Unique().
Column("test2").Varchar(32).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table5` (`test` VARCHAR(32) NOT NULL UNIQUE , `test2` VARCHAR(32) NOT NULL) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationNamingUniqueKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table6").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Unique("uk_test", []string{"test", "test2"}).
Unique("uk_test2", []string{"test3", "test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table6` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL, UNIQUE KEY `uk_test` (`test`,`test2`), UNIQUE KEY `uk_test2` (`test3`,`test4`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationMultiUniqueKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table7").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Unique([]string{"test", "test2"}).
Unique([]string{"test3", "test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table7` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL, UNIQUE KEY (`test`,`test2`), UNIQUE KEY (`test3`,`test4`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationAnonymousIndexKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table14").
Column("test").Varchar(32).Index().
Column("test2").Varchar(32).Index().
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table14` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, INDEX (`test`), INDEX (`test2`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationNamingIndexKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table9").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Index("ik_test", []string{"test", "test2"}).
Index("ik_test2", []string{"test3", "test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table9` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL, INDEX `ik_test` (`test`,`test2`), INDEX `ik_test2` (`test3`,`test4`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationMixedKeys(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table10").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Primary([]string{"test", "test2"}).
Unique([]string{"test3", "test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table10` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL, PRIMARY KEY (`test`,`test2`), UNIQUE KEY (`test3`,`test4`)) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_table11").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Index("ik_test", []string{"test", "test2"}).
Unique([]string{"test3", "test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table11` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL, UNIQUE KEY (`test3`,`test4`), INDEX `ik_test` (`test`,`test2`)) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_table12").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Primary([]string{"test", "test2"}).
Index("ik_test", []string{"test3", "test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table12` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL, PRIMARY KEY (`test`,`test2`), INDEX `ik_test` (`test3`,`test4`)) ENGINE=INNODB", migration.LastQuery)
err = migration.
Table("test_table13").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Column("test3").Varchar(32).
Column("test4").Varchar(32).
Column("test5").Varchar(32).
Column("test6").Varchar(32).
Index("ik_test", []string{"test", "test2"}).
Unique([]string{"test3", "test4"}).
Primary([]string{"test5", "test6"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table13` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL , `test3` VARCHAR(32) NOT NULL , `test4` VARCHAR(32) NOT NULL , `test5` VARCHAR(32) NOT NULL , `test6` VARCHAR(32) NOT NULL, PRIMARY KEY (`test5`,`test6`), UNIQUE KEY (`test3`,`test4`), INDEX `ik_test` (`test`,`test2`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationForeignKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table15").
Column("test").Varchar(32).Foreign("test_table13.test5").
Column("test2").Varchar(32).Foreign("test_table13.test6").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table15` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, FOREIGN KEY (`test`,`test2`) REFERENCES `test_table13` (`test5`, `test6`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationMultipleForeignKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table16").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Foreign([]string{"test"}, []string{"test_table13.test5"}).
Foreign([]string{"test2"}, []string{"test_table12.test3"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table16` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, FOREIGN KEY (`test`) REFERENCES `test_table13` (`test5`), FOREIGN KEY (`test2`) REFERENCES `test_table12` (`test3`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationNamingForeignKey(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table17").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Foreign("fk_test", []string{"test", "test2"}, []string{"test_table13.test5", "test_table13.test6"}).
Foreign("fk_test2", []string{"test", "test2"}, []string{"test_table12.test3", "test_table12.test4"}).
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table17` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, FOREIGN KEY fk_test (`test`,`test2`) REFERENCES `test_table13` (`test5`, `test6`), FOREIGN KEY fk_test2 (`test`,`test2`) REFERENCES `test_table12` (`test3`, `test4`)) ENGINE=INNODB", migration.LastQuery)
}
func TestMigrationForeignKeysOnActions(t *testing.T) {
assert := assert.New(t)
err := migration.
Table("test_table18").
Column("test").Varchar(32).
Column("test2").Varchar(32).
Foreign([]string{"test"}, []string{"test_table13.test5"}).OnUpdate("NO ACTION").OnDelete("NO ACTION").
Foreign([]string{"test2"}, []string{"test_table12.test3"}).OnUpdate("CASCADE").OnDelete("RESTRICT").
Create()
assert.NoError(err)
assert.Equal("CREATE TABLE IF NOT EXISTS `test_table18` (`test` VARCHAR(32) NOT NULL , `test2` VARCHAR(32) NOT NULL, FOREIGN KEY (`test`) REFERENCES `test_table13` (`test5`) ON UPDATE NO ACTION ON DELETE NO ACTION, FOREIGN KEY (`test2`) REFERENCES `test_table12` (`test3`) ON UPDATE CASCADE ON DELETE RESTRICT) ENGINE=INNODB", migration.LastQuery)
}