-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
rename_constraint
operation (#293)
- Loading branch information
Showing
8 changed files
with
233 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "33_rename_check_constraint", | ||
"operations": [ | ||
{ | ||
"rename_constraint": { | ||
"table": "people", | ||
"from": "name_length", | ||
"to": "name_length_check" | ||
} | ||
} | ||
] | ||
} |
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,47 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
"github.com/xataio/pgroll/pkg/schema" | ||
) | ||
|
||
var _ Operation = (*OpRenameConstraint)(nil) | ||
|
||
func (o *OpRenameConstraint) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) (*schema.Table, error) { | ||
// no-op | ||
return nil, nil | ||
} | ||
|
||
func (o *OpRenameConstraint) Complete(ctx context.Context, conn *sql.DB, s *schema.Schema) error { | ||
// rename the constraint in the underlying table | ||
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME CONSTRAINT %s TO %s", | ||
pq.QuoteIdentifier(o.Table), | ||
pq.QuoteIdentifier(o.From), | ||
pq.QuoteIdentifier(o.To))) | ||
return err | ||
} | ||
|
||
func (o *OpRenameConstraint) Rollback(ctx context.Context, conn *sql.DB) error { | ||
// no-op | ||
return nil | ||
} | ||
|
||
func (o *OpRenameConstraint) Validate(ctx context.Context, s *schema.Schema) error { | ||
table := s.GetTable(o.Table) | ||
|
||
if !table.ConstraintExists(o.From) { | ||
return ConstraintDoesNotExistError{Table: o.Table, Constraint: o.From} | ||
} | ||
|
||
if table.ConstraintExists(o.To) { | ||
return ConstraintAlreadyExistsError{Table: o.Table, Constraint: o.To} | ||
} | ||
|
||
return nil | ||
} |
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,102 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations_test | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xataio/pgroll/pkg/migrations" | ||
) | ||
|
||
func TestRenameConstraint(t *testing.T) { | ||
t.Parallel() | ||
|
||
addTableMigration := migrations.Migration{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
Pk: ptr(true), | ||
}, | ||
{ | ||
Name: "username", | ||
Type: "text", | ||
Nullable: ptr(false), | ||
Check: &migrations.CheckConstraint{Constraint: `LENGTH("username") <= 2048`, Name: "users_text_length_username"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ExecuteTests(t, TestCases{{ | ||
name: "rename constraint", | ||
migrations: []migrations.Migration{ | ||
addTableMigration, | ||
{ | ||
Name: "02_rename_constraint", | ||
Operations: migrations.Operations{ | ||
&migrations.OpAlterColumn{ | ||
Table: "users", | ||
Column: "username", | ||
Name: ptr("name"), | ||
}, | ||
&migrations.OpRenameConstraint{ | ||
Table: "users", | ||
From: "users_text_length_username", | ||
To: "users_text_length_name", | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB, schema string) { | ||
// The column in the underlying table has not been renamed. | ||
ColumnMustExist(t, db, schema, "users", "username") | ||
|
||
// Insertions to the new column name in the new version schema should work. | ||
MustInsert(t, db, schema, "02_rename_constraint", "users", map[string]string{"name": "alice"}) | ||
|
||
// Insertions to the old column name in the old version schema should work. | ||
MustInsert(t, db, schema, "01_add_table", "users", map[string]string{"username": "bob"}) | ||
|
||
// The check constraint in the underlying table has not been renamed. | ||
CheckConstraintMustExist(t, db, schema, "users", "users_text_length_username") | ||
|
||
// The new check constraint in the underlying table has not been created. | ||
CheckConstraintMustNotExist(t, db, schema, "users", "users_text_length_name") | ||
|
||
// Data can be read from the view in the new version schema. | ||
rows := MustSelect(t, db, schema, "02_rename_constraint", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB, schema string) { | ||
// no-op | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) { | ||
// The column in the underlying table has been renamed. | ||
ColumnMustExist(t, db, schema, "users", "name") | ||
|
||
// The check constraint in the underlying table has been renamed. | ||
CheckConstraintMustExist(t, db, schema, "users", "users_text_length_name") | ||
|
||
// The old check constraint in the underlying table has been dropped. | ||
CheckConstraintMustNotExist(t, db, schema, "users", "users_text_length_username") | ||
|
||
// Data can be read from the view in the new version schema. | ||
rows := MustSelect(t, db, schema, "02_rename_constraint", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
}, | ||
}}) | ||
} |
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