@@ -746,7 +746,7 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
746
746
747
747
ExecuteTests (t , TestCases {
748
748
{
749
- name : "rename table, create constraint" ,
749
+ name : "rename table, create check constraint" ,
750
750
migrations : []migrations.Migration {
751
751
{
752
752
Name : "01_create_table" ,
@@ -854,7 +854,7 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
854
854
},
855
855
},
856
856
{
857
- name : "rename table, rename column, create constraint" ,
857
+ name : "rename table, rename column, create check constraint" ,
858
858
migrations : []migrations.Migration {
859
859
{
860
860
Name : "01_create_table" ,
@@ -962,6 +962,118 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
962
962
{"id" : 3 , "item_name" : "banana" },
963
963
}, rows )
964
964
965
+ // The table has been cleaned up
966
+ TableMustBeCleanedUp (t , db , schema , "products" , "name" )
967
+ },
968
+ },
969
+ {
970
+ name : "rename table, rename column, create unique constraint" ,
971
+ migrations : []migrations.Migration {
972
+ {
973
+ Name : "01_create_table" ,
974
+ Operations : migrations.Operations {
975
+ & migrations.OpCreateTable {
976
+ Name : "items" ,
977
+ Columns : []migrations.Column {
978
+ {
979
+ Name : "id" ,
980
+ Type : "int" ,
981
+ Pk : true ,
982
+ },
983
+ {
984
+ Name : "name" ,
985
+ Type : "varchar(255)" ,
986
+ Nullable : true ,
987
+ },
988
+ },
989
+ },
990
+ },
991
+ },
992
+ {
993
+ Name : "02_multi_operation" ,
994
+ Operations : migrations.Operations {
995
+ & migrations.OpRenameTable {
996
+ From : "items" ,
997
+ To : "products" ,
998
+ },
999
+ & migrations.OpRenameColumn {
1000
+ Table : "products" ,
1001
+ From : "name" ,
1002
+ To : "item_name" ,
1003
+ },
1004
+ & migrations.OpCreateConstraint {
1005
+ Table : "products" ,
1006
+ Type : migrations .OpCreateConstraintTypeUnique ,
1007
+ Name : "unique_item_name" ,
1008
+ Columns : []string {"item_name" },
1009
+ Up : map [string ]string {
1010
+ "item_name" : "item_name" ,
1011
+ },
1012
+ Down : map [string ]string {
1013
+ "item_name" : "item_name" ,
1014
+ },
1015
+ },
1016
+ },
1017
+ },
1018
+ },
1019
+ afterStart : func (t * testing.T , db * sql.DB , schema string ) {
1020
+ // Can insert a row into the new schema
1021
+ MustInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1022
+ "id" : "1" ,
1023
+ "item_name" : "apple" ,
1024
+ })
1025
+
1026
+ // Can insert a row into the new schema that meets the constraint
1027
+ MustInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1028
+ "id" : "2" ,
1029
+ "item_name" : "banana" ,
1030
+ })
1031
+
1032
+ // Can't insert a row into the new schema that violates the constraint
1033
+ MustNotInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1034
+ "id" : "3" ,
1035
+ "item_name" : "apple" ,
1036
+ }, testutils .UniqueViolationErrorCode )
1037
+
1038
+ // The new view has the expected rows
1039
+ rows := MustSelect (t , db , schema , "02_multi_operation" , "products" )
1040
+ assert .Equal (t , []map [string ]any {
1041
+ {"id" : 1 , "item_name" : "apple" },
1042
+ {"id" : 2 , "item_name" : "banana" },
1043
+ }, rows )
1044
+
1045
+ // The old view has the expected rows
1046
+ rows = MustSelect (t , db , schema , "01_create_table" , "items" )
1047
+ assert .Equal (t , []map [string ]any {
1048
+ {"id" : 1 , "name" : "apple" },
1049
+ {"id" : 2 , "name" : "banana" },
1050
+ }, rows )
1051
+ },
1052
+ afterRollback : func (t * testing.T , db * sql.DB , schema string ) {
1053
+ // The table has been cleaned up
1054
+ TableMustBeCleanedUp (t , db , schema , "items" , "name" )
1055
+ },
1056
+ afterComplete : func (t * testing.T , db * sql.DB , schema string ) {
1057
+ // Can insert a row into the new schema that meets the constraint
1058
+ MustInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1059
+ "id" : "3" ,
1060
+ "item_name" : "carrot" ,
1061
+ })
1062
+
1063
+ // Can't insert a row into the new schema that violates the constraint
1064
+ MustNotInsert (t , db , schema , "02_multi_operation" , "products" , map [string ]string {
1065
+ "id" : "4" ,
1066
+ "item_name" : "carrot" ,
1067
+ }, testutils .UniqueViolationErrorCode )
1068
+
1069
+ // The new view has the expected rows
1070
+ rows := MustSelect (t , db , schema , "02_multi_operation" , "products" )
1071
+ assert .Equal (t , []map [string ]any {
1072
+ {"id" : 1 , "item_name" : "apple" },
1073
+ {"id" : 2 , "item_name" : "banana" },
1074
+ {"id" : 3 , "item_name" : "carrot" },
1075
+ }, rows )
1076
+
965
1077
// The table has been cleaned up
966
1078
TableMustBeCleanedUp (t , db , schema , "products" , "name" )
967
1079
},
0 commit comments