-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#464) This PR introduces a new constraint `type` to `create_constraint` operation called `check`. Now it is possible to create check constraints on multiple columns. ### Example ```json { "name": "45_add_table_check_constraint", "operations": [ { "create_constraint": { "type": "check", "table": "tickets", "name": "check_zip_name", "columns": [ "sellers_name", "sellers_zip" ], "check": "sellers_name ~ 'Alice' AND sellers_zip IS NOT NULL", "up": { "sellers_name": "Alice", "sellers_zip": "(SELECT CASE WHEN sellers_zip IS NOT NULL THEN sellers_zip ELSE '00000' END)" }, "down": { "sellers_name": "sellers_name", "sellers_zip": "sellers_zip" } } } ] } ``` --------- Co-authored-by: Andrew Farries <[email protected]>
- Loading branch information
1 parent
5d63a7f
commit d231ee0
Showing
7 changed files
with
287 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "45_add_table_check_constraint", | ||
"operations": [ | ||
{ | ||
"create_constraint": { | ||
"type": "check", | ||
"table": "tickets", | ||
"name": "check_zip_name", | ||
"columns": [ | ||
"sellers_name", | ||
"sellers_zip" | ||
], | ||
"check": "sellers_name = 'alice' OR sellers_zip > 0", | ||
"up": { | ||
"sellers_name": "sellers_name", | ||
"sellers_zip": "(SELECT CASE WHEN sellers_name != 'alice' AND sellers_zip <= 0 THEN 123 WHEN sellers_name != 'alice' THEN sellers_zip ELSE sellers_zip END)" | ||
}, | ||
"down": { | ||
"sellers_name": "sellers_name", | ||
"sellers_zip": "sellers_zip" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/xataio/pgroll/internal/testutils" | ||
"github.com/xataio/pgroll/pkg/migrations" | ||
) | ||
|
@@ -97,6 +99,80 @@ func TestCreateConstraint(t *testing.T) { | |
}, testutils.UniqueViolationErrorCode) | ||
}, | ||
}, | ||
{ | ||
name: "create check constraint on single column", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
Pk: ptr(true), | ||
}, | ||
{ | ||
Name: "name", | ||
Type: "varchar(255)", | ||
Nullable: ptr(false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_create_constraint", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateConstraint{ | ||
Name: "name_letters", | ||
Table: "users", | ||
Type: "check", | ||
Check: ptr("name ~ '^[a-zA-Z]+$'"), | ||
Columns: []string{"name"}, | ||
Up: migrations.OpCreateConstraintUp(map[string]string{ | ||
"name": "regexp_replace(name, '\\d+', '', 'g')", | ||
}), | ||
Down: migrations.OpCreateConstraintDown(map[string]string{ | ||
"name": "name", | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB, schema string) { | ||
// The new (temporary) column should exist on the underlying table. | ||
ColumnMustExist(t, db, schema, "users", migrations.TemporaryName("name")) | ||
// The check constraint exists on the new table. | ||
CheckConstraintMustExist(t, db, schema, "users", "name_letters") | ||
// Inserting values into the old schema that violate the check constraint must succeed. | ||
MustInsert(t, db, schema, "01_add_table", "users", map[string]string{ | ||
"name": "alice11", | ||
}) | ||
|
||
// Inserting values into the new schema that violate the check constraint should fail. | ||
MustInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ | ||
"name": "bob", | ||
}) | ||
MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ | ||
"name": "bob2", | ||
}, testutils.CheckViolationErrorCode) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB, schema string) { | ||
// Functions, triggers and temporary columns are dropped. | ||
tableCleanedUp(t, db, schema, "users", "name") | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) { | ||
// Functions, triggers and temporary columns are dropped. | ||
tableCleanedUp(t, db, schema, "users", "name") | ||
|
||
// Inserting values into the new schema that violate the check constraint should fail. | ||
MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ | ||
"name": "carol0", | ||
}, testutils.CheckViolationErrorCode) | ||
}, | ||
}, | ||
{ | ||
name: "create unique constraint on multiple columns", | ||
migrations: []migrations.Migration{ | ||
|
@@ -181,6 +257,104 @@ func TestCreateConstraint(t *testing.T) { | |
// Complete is a no-op. | ||
}, | ||
}, | ||
{ | ||
name: "create check constraint on multiple columns", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
Pk: ptr(true), | ||
}, | ||
{ | ||
Name: "name", | ||
Type: "varchar(255)", | ||
Nullable: ptr(false), | ||
}, | ||
{ | ||
Name: "email", | ||
Type: "varchar(255)", | ||
Nullable: ptr(false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_create_constraint", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateConstraint{ | ||
Name: "check_name_email", | ||
Table: "users", | ||
Type: "check", | ||
Check: ptr("name != email"), | ||
Columns: []string{"name", "email"}, | ||
Up: migrations.OpCreateConstraintUp(map[string]string{ | ||
"name": "name", | ||
"email": "(SELECT CASE WHEN email ~ '@' THEN email ELSE email || '@example.com' END)", | ||
}), | ||
Down: migrations.OpCreateConstraintDown(map[string]string{ | ||
"name": "name", | ||
"email": "email", | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB, schema string) { | ||
// The new (temporary) column should exist on the underlying table. | ||
ColumnMustExist(t, db, schema, "users", migrations.TemporaryName("name")) | ||
// The new (temporary) column should exist on the underlying table. | ||
ColumnMustExist(t, db, schema, "users", migrations.TemporaryName("email")) | ||
// The check constraint exists on the new table. | ||
CheckConstraintMustExist(t, db, schema, "users", "check_name_email") | ||
|
||
// Inserting values into the old schema that the violate the check constraint must succeed. | ||
MustInsert(t, db, schema, "01_add_table", "users", map[string]string{ | ||
"name": "alice", | ||
"email": "alice", | ||
}) | ||
|
||
// Inserting values into the new schema that meet the check constraint should succeed. | ||
MustInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ | ||
"name": "bob", | ||
"email": "[email protected]", | ||
}) | ||
MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ | ||
"name": "bob", | ||
"email": "bob", | ||
}, testutils.CheckViolationErrorCode) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB, schema string) { | ||
// The check constraint must not exists on the table. | ||
CheckConstraintMustNotExist(t, db, schema, "users", "check_name_email") | ||
// Functions, triggers and temporary columns are dropped. | ||
tableCleanedUp(t, db, schema, "users", "name") | ||
tableCleanedUp(t, db, schema, "users", "email") | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) { | ||
// Functions, triggers and temporary columns are dropped. | ||
tableCleanedUp(t, db, schema, "users", "name") | ||
tableCleanedUp(t, db, schema, "users", "email") | ||
|
||
// Inserting values into the new schema that the violate the check constraint must fail. | ||
MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ | ||
"name": "carol", | ||
"email": "carol", | ||
}, testutils.CheckViolationErrorCode) | ||
|
||
rows := MustSelect(t, db, schema, "02_create_constraint", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice", "email": "[email protected]"}, | ||
{"id": 2, "name": "bob", "email": "[email protected]"}, | ||
}, rows) | ||
}, | ||
}, | ||
{ | ||
name: "invalid constraint name", | ||
migrations: []migrations.Migration{ | ||
|
@@ -270,6 +444,52 @@ func TestCreateConstraint(t *testing.T) { | |
afterRollback: func(t *testing.T, db *sql.DB, schema string) {}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) {}, | ||
}, | ||
{ | ||
name: "expression of check constraint is missing", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
Pk: ptr(true), | ||
}, | ||
{ | ||
Name: "name", | ||
Type: "varchar(255)", | ||
Nullable: ptr(false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_create_constraint_with_missing_migration", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateConstraint{ | ||
Name: "check_name", | ||
Table: "users", | ||
Columns: []string{"name"}, | ||
Type: "check", | ||
Up: migrations.OpCreateConstraintUp(map[string]string{ | ||
"name": "name", | ||
}), | ||
Down: migrations.OpCreateConstraintDown(map[string]string{ | ||
"name": "name", | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantStartErr: migrations.FieldRequiredError{Name: "check"}, | ||
afterStart: func(t *testing.T, db *sql.DB, schema string) {}, | ||
afterRollback: func(t *testing.T, db *sql.DB, schema string) {}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) {}, | ||
}, | ||
}) | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters