@@ -1225,6 +1225,105 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
1225
1225
TableMustBeCleanedUp (t , db , schema , "products" , "name" )
1226
1226
},
1227
1227
},
1228
+ {
1229
+ name : "create table with comment and default on column, add unique constraint" ,
1230
+ migrations : []migrations.Migration {
1231
+ {
1232
+ Name : "01_create_table" ,
1233
+ Operations : migrations.Operations {
1234
+ & migrations.OpCreateTable {
1235
+ Name : "items" ,
1236
+ Columns : []migrations.Column {
1237
+ {
1238
+ Name : "id" ,
1239
+ Type : "int" ,
1240
+ Pk : true ,
1241
+ },
1242
+ {
1243
+ Name : "name" ,
1244
+ Type : "text" ,
1245
+ Default : ptr ("'Pixel'" ),
1246
+ Comment : ptr ("my important comment" ),
1247
+ },
1248
+ },
1249
+ },
1250
+ & migrations.OpCreateConstraint {
1251
+ Table : "items" ,
1252
+ Type : migrations .OpCreateConstraintTypeUnique ,
1253
+ Name : "unique_item_name" ,
1254
+ Columns : []string {"name" },
1255
+ Up : map [string ]string {
1256
+ "name" : "name" ,
1257
+ },
1258
+ Down : map [string ]string {
1259
+ "name" : "name" ,
1260
+ },
1261
+ },
1262
+ },
1263
+ },
1264
+ },
1265
+ afterStart : func (t * testing.T , db * sql.DB , schema string ) {
1266
+ // Column must have comment set
1267
+ ColumnMustHaveComment (t , db , schema , "items" , "name" , "my important comment" )
1268
+ // Column must have default value
1269
+ ColumnMustHaveDefault (t , db , schema , "items" , "name" , "'Pixel'::text" )
1270
+ // Can insert a row into the new schema
1271
+ MustInsert (t , db , schema , "01_create_table" , "items" , map [string ]string {
1272
+ "id" : "1" ,
1273
+ "name" : "apple" ,
1274
+ })
1275
+
1276
+ // Can insert a row into the new schema that meets the constraint
1277
+ MustInsert (t , db , schema , "01_create_table" , "items" , map [string ]string {
1278
+ "id" : "2" ,
1279
+ "name" : "banana" ,
1280
+ })
1281
+
1282
+ // Can't insert a row into the new schema that violates the constraint
1283
+ MustNotInsert (t , db , schema , "01_create_table" , "items" , map [string ]string {
1284
+ "id" : "3" ,
1285
+ "name" : "apple" ,
1286
+ }, testutils .UniqueViolationErrorCode )
1287
+
1288
+ // The new view has the expected rows
1289
+ rows := MustSelect (t , db , schema , "01_create_table" , "items" )
1290
+ assert .Equal (t , []map [string ]any {
1291
+ {"id" : 1 , "name" : "apple" },
1292
+ {"id" : 2 , "name" : "banana" },
1293
+ }, rows )
1294
+
1295
+ // The old view has the expected rows
1296
+ rows = MustSelect (t , db , schema , "01_create_table" , "items" )
1297
+ assert .Equal (t , []map [string ]any {
1298
+ {"id" : 1 , "name" : "apple" },
1299
+ {"id" : 2 , "name" : "banana" },
1300
+ }, rows )
1301
+ },
1302
+ afterRollback : func (t * testing.T , db * sql.DB , schema string ) {},
1303
+ afterComplete : func (t * testing.T , db * sql.DB , schema string ) {
1304
+ // Column comment must be preserved
1305
+ ColumnMustHaveComment (t , db , schema , "items" , "name" , "my important comment" )
1306
+ // Column must have default value
1307
+ ColumnMustHaveDefault (t , db , schema , "items" , "name" , "'Pixel'::text" )
1308
+ // Can insert a row into the new schema that meets the constraint
1309
+ MustInsert (t , db , schema , "01_create_table" , "items" , map [string ]string {
1310
+ "id" : "3" ,
1311
+ "name" : "carrot" ,
1312
+ })
1313
+
1314
+ // Can't insert a row into the new schema that violates the constraint
1315
+ MustNotInsert (t , db , schema , "01_create_table" , "items" , map [string ]string {
1316
+ "id" : "4" ,
1317
+ "name" : "carrot" ,
1318
+ }, testutils .UniqueViolationErrorCode )
1319
+
1320
+ // The new view has the expected rows
1321
+ rows := MustSelect (t , db , schema , "01_create_table" , "items" )
1322
+ assert .Equal (t , []map [string ]any {
1323
+ {"id" : 3 , "name" : "carrot" },
1324
+ }, rows )
1325
+ },
1326
+ },
1228
1327
})
1229
1328
}
1230
1329
0 commit comments