Skip to content

Commit

Permalink
Add test for preservation of FK constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Jan 16, 2024
1 parent a329f89 commit edc378c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions pkg/migrations/op_set_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,84 @@ func TestSetColumnUnique(t *testing.T) {
}, rows)
},
},
{
name: "foreign keys defined on the column are preserved when adding a unique constraint",
migrations: []migrations.Migration{
{
Name: "01_add_departments_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "departments",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Type: "text",
Nullable: false,
},
},
},
},
},
{
Name: "02_add_employees_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "employees",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Type: "text",
Nullable: false,
},
{
Name: "department_id",
Type: "integer",
Nullable: true,
References: &migrations.ForeignKeyReference{
Name: "fk_employee_department",
Table: "departments",
Column: "id",
},
},
},
},
},
},
{
Name: "03_set_unique",
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "employees",
Column: "department_id",
Unique: &migrations.UniqueConstraint{
Name: "employees_department_id_unique",
},
Up: "department_id",
Down: "department_id",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB) {
// A temporary FK constraint has been created on the temporary column
ConstraintMustExist(t, db, "public", "employees", migrations.TemporaryName("fk_employee_department"))
},
afterRollback: func(t *testing.T, db *sql.DB) {
},
afterComplete: func(t *testing.T, db *sql.DB) {
// The foreign key constraint still exists on the column
ConstraintMustExist(t, db, "public", "employees", "fk_employee_department")
},
},
})
}

0 comments on commit edc378c

Please sign in to comment.