Skip to content

Commit

Permalink
Add a testcase for dropping unique constraints (#117)
Browse files Browse the repository at this point in the history
Now that adding `UNIQUE` constraints is restricted to single columns, it
is possible to drop them using the same single-column duplication +
triggers method that is used to drop check constraints and foreign keys.

Add an example migration and a testcase to check that this works as
expected; the new version of the schema contains a column that does not
have a `UNIQUE` constraint while the old version does.
  • Loading branch information
andrew-farries authored Sep 21, 2023
1 parent df004b6 commit eca293a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/27_drop_unique_constraint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "27_drop_unique_constraint",
"operations": [
{
"drop_constraint": {
"table": "reviews",
"column": "review",
"name": "reviews_review_unique",
"up": "review",
"down": "review || '-' || (random()*1000000)::integer"
}
}
]
}
79 changes: 79 additions & 0 deletions pkg/migrations/op_drop_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,85 @@ func TestDropConstraint(t *testing.T) {
TriggerMustNotExist(t, db, "public", "posts", migrations.TriggerName("posts", migrations.TemporaryName("user_id")))
},
},
{
name: "drop unique constraint",
migrations: []migrations.Migration{
{
Name: "01_add_tables",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
},
{
Name: "name",
Type: "text",
Unique: true,
},
},
},
},
},
{
Name: "02_drop_unique_constraint",
Operations: migrations.Operations{
&migrations.OpDropConstraint{
Table: "users",
Column: "name",
Name: "_pgroll_new_users_name_key",
Up: "name",
Down: "name || '-' || (random()*1000000)::integer",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB) {
// The new (temporary) `name` column should exist on the underlying table.
ColumnMustExist(t, db, "public", "users", migrations.TemporaryName("name"))

// Inserting a row that meets the unique constraint into the old view works.
MustInsert(t, db, "public", "01_add_tables", "users", map[string]string{
"name": "alice",
})

// Inserting a row that does not meet the unique constraint into the old view fails.
MustNotInsert(t, db, "public", "01_add_tables", "users", map[string]string{
"name": "alice",
})

// Inserting a row that does not meet the unique constraint into the new view works.
MustInsert(t, db, "public", "02_drop_unique_constraint", "users", map[string]string{
"name": "alice",
})
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The new (temporary) `name` column should not exist on the underlying table.
ColumnMustNotExist(t, db, "public", "users", migrations.TemporaryName("name"))

// The up function no longer exists.
FunctionMustNotExist(t, db, "public", migrations.TriggerFunctionName("users", "name"))
// The down function no longer exists.
FunctionMustNotExist(t, db, "public", migrations.TriggerFunctionName("users", migrations.TemporaryName("name")))

// The up trigger no longer exists.
TriggerMustNotExist(t, db, "public", "users", migrations.TriggerName("users", "name"))
// The down trigger no longer exists.
TriggerMustNotExist(t, db, "public", "users", migrations.TriggerName("users", migrations.TemporaryName("name")))
},
afterComplete: func(t *testing.T, db *sql.DB) {
// The new (temporary) `name` column should not exist on the underlying table.
ColumnMustNotExist(t, db, "public", "users", migrations.TemporaryName("name"))

// Inserting a row that does not meet the unique constraint into the new view works.
MustInsert(t, db, "public", "02_drop_unique_constraint", "users", map[string]string{
"name": "alice",
})
},
},
})
}

Expand Down

0 comments on commit eca293a

Please sign in to comment.