Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ForeignKeyReferenceOnDelete to ForeignKeyAction #639

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/migrations/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ type InvalidOnDeleteSettingError struct {
func (e InvalidOnDeleteSettingError) Error() string {
return fmt.Sprintf("foreign key %q on_delete setting must be one of: %q, %q, %q, %q or %q, not %q",
e.Name,
ForeignKeyReferenceOnDeleteNOACTION,
ForeignKeyReferenceOnDeleteRESTRICT,
ForeignKeyReferenceOnDeleteSETDEFAULT,
ForeignKeyReferenceOnDeleteSETNULL,
ForeignKeyReferenceOnDeleteCASCADE,
ForeignKeyActionNOACTION,
ForeignKeyActionRESTRICT,
ForeignKeyActionSETDEFAULT,
ForeignKeyActionSETNULL,
ForeignKeyActionCASCADE,
e.Setting,
)
}
Expand All @@ -207,8 +207,8 @@ type UnexpectedOnDeleteSetColumnError struct {
func (e UnexpectedOnDeleteSetColumnError) Error() string {
return fmt.Sprintf("if on_delete_set_columns is set in foreign key %q, on_delete setting must be one of: %q, %q",
e.Name,
ForeignKeyReferenceOnDeleteSETDEFAULT,
ForeignKeyReferenceOnDeleteSETNULL,
ForeignKeyActionSETDEFAULT,
ForeignKeyActionSETNULL,
)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/migrations/fk_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func (f *ForeignKeyReference) Validate(s *schema.Schema) error {
}

switch strings.ToUpper(string(f.OnDelete)) {
case string(ForeignKeyReferenceOnDeleteNOACTION):
case string(ForeignKeyReferenceOnDeleteRESTRICT):
case string(ForeignKeyReferenceOnDeleteSETDEFAULT):
case string(ForeignKeyReferenceOnDeleteSETNULL):
case string(ForeignKeyReferenceOnDeleteCASCADE):
case string(ForeignKeyActionNOACTION):
case string(ForeignKeyActionRESTRICT):
case string(ForeignKeyActionSETDEFAULT):
case string(ForeignKeyActionSETNULL):
case string(ForeignKeyActionCASCADE):
case "":
break
default:
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (w ColumnSQLWriter) Write(col Column) (string, error) {
}

if col.References != nil {
onDelete := string(ForeignKeyReferenceOnDeleteNOACTION)
onDelete := string(ForeignKeyActionNOACTION)
if col.References.OnDelete != "" {
onDelete = strings.ToUpper(string(col.References.OnDelete))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_add_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
Name: "fk_users_id",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestChangeColumnType(t *testing.T) {
Name: "fk_employee_department",
Table: "departments",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/migrations/op_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (o *OpCreateTable) Validate(ctx context.Context, s *schema.Schema) error {
return FieldRequiredError{Name: "references"}
}
if len(c.References.OnDeleteSetColumns) != 0 {
if c.References.OnDelete != ForeignKeyReferenceOnDeleteSETDEFAULT && c.References.OnDelete != ForeignKeyReferenceOnDeleteSETNULL {
if c.References.OnDelete != ForeignKeyActionSETDEFAULT && c.References.OnDelete != ForeignKeyActionSETNULL {
return UnexpectedOnDeleteSetColumnError{
Name: o.Name,
}
Expand Down Expand Up @@ -381,15 +381,15 @@ func (w *ConstraintSQLWriter) WritePrimaryKey() string {
return constraint
}

func (w *ConstraintSQLWriter) WriteForeignKey(referencedTable string, referencedColumns []string, onDelete, onUpdate ForeignKeyReferenceOnDelete, setColumns []string) string {
onDeleteAction := string(ForeignKeyReferenceOnDeleteNOACTION)
func (w *ConstraintSQLWriter) WriteForeignKey(referencedTable string, referencedColumns []string, onDelete, onUpdate ForeignKeyAction, setColumns []string) string {
onDeleteAction := string(ForeignKeyActionNOACTION)
if onDelete != "" {
onDeleteAction = strings.ToUpper(string(onDelete))
if len(setColumns) != 0 {
onDeleteAction += " (" + strings.Join(quoteColumnNames(setColumns), ", ") + ")"
}
}
onUpdateAction := string(ForeignKeyReferenceOnDeleteNOACTION)
onUpdateAction := string(ForeignKeyActionNOACTION)
if onUpdate != "" {
onUpdateAction = strings.ToUpper(string(onUpdate))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/migrations/op_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestCreateTable(t *testing.T) {
Column: "id",
Name: "fk_users_id",
Table: "users",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
},
{
Expand Down Expand Up @@ -820,7 +820,7 @@ func TestCreateTable(t *testing.T) {
References: &migrations.ConstraintReferences{
Table: "owners",
Columns: []string{"id"},
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
},
},
Expand Down Expand Up @@ -950,7 +950,7 @@ func TestCreateTable(t *testing.T) {
References: &migrations.ConstraintReferences{
Table: "owners",
Columns: []string{"id", "city"},
OnDelete: migrations.ForeignKeyReferenceOnDeleteSETDEFAULT,
OnDelete: migrations.ForeignKeyActionSETDEFAULT,
OnDeleteSetColumns: []string{"owner_id", "owner_city_id"},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_set_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "fk_employee_department",
Table: "departments",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/migrations/op_set_fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestSetForeignKey(t *testing.T) {
Name: "fk_users_id",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
Up: "SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END",
Down: "user_id",
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestSetForeignKey(t *testing.T) {
Name: "fk_users_id",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteSETNULL,
OnDelete: migrations.ForeignKeyActionSETNULL,
},
Up: "SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END",
Down: "user_id",
Expand Down Expand Up @@ -520,7 +520,7 @@ func TestSetForeignKey(t *testing.T) {
Name: "fk_users_id",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteSETDEFAULT,
OnDelete: migrations.ForeignKeyActionSETDEFAULT,
},
Up: "SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END",
Down: "user_id",
Expand Down Expand Up @@ -725,7 +725,7 @@ func TestSetForeignKey(t *testing.T) {
Name: "fk_users_id_1",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
Up: "SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END",
Down: "user_id",
Expand Down Expand Up @@ -1894,7 +1894,7 @@ func TestSetForeignKeyValidation(t *testing.T) {
Name: "fk_users_doesntexist",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteNOACTION,
OnDelete: migrations.ForeignKeyActionNOACTION,
},
Up: "SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END",
Down: "user_id",
Expand All @@ -1918,7 +1918,7 @@ func TestSetForeignKeyValidation(t *testing.T) {
Name: "fk_users_doesntexist",
Table: "users",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteSETNULL,
OnDelete: migrations.ForeignKeyActionSETNULL,
},
Up: "SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END",
Down: "user_id",
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_set_notnull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestSetNotNull(t *testing.T) {
Name: "fk_employee_department",
Table: "departments",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE,
OnDelete: migrations.ForeignKeyActionCASCADE,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_set_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestSetColumnUnique(t *testing.T) {
Name: "fk_employee_department",
Table: "departments",
Column: "id",
OnDelete: migrations.ForeignKeyReferenceOnDeleteSETNULL,
OnDelete: migrations.ForeignKeyActionSETNULL,
},
},
},
Expand Down
24 changes: 12 additions & 12 deletions pkg/migrations/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,20 @@ func convertAlterTableAddForeignKeyConstraint(stmt *pgq.AlterTableStmt, constrai
}, nil
}

func parseOnDeleteAction(action string) (migrations.ForeignKeyReferenceOnDelete, error) {
func parseOnDeleteAction(action string) (migrations.ForeignKeyAction, error) {
switch action {
case "a":
return migrations.ForeignKeyReferenceOnDeleteNOACTION, nil
return migrations.ForeignKeyActionNOACTION, nil
case "c":
return migrations.ForeignKeyReferenceOnDeleteCASCADE, nil
return migrations.ForeignKeyActionCASCADE, nil
case "r":
return migrations.ForeignKeyReferenceOnDeleteRESTRICT, nil
return migrations.ForeignKeyActionRESTRICT, nil
case "d":
return migrations.ForeignKeyReferenceOnDeleteSETDEFAULT, nil
return migrations.ForeignKeyActionSETDEFAULT, nil
case "n":
return migrations.ForeignKeyReferenceOnDeleteSETNULL, nil
return migrations.ForeignKeyActionSETNULL, nil
default:
return migrations.ForeignKeyReferenceOnDeleteNOACTION, fmt.Errorf("unknown delete action: %q", action)
return migrations.ForeignKeyActionNOACTION, fmt.Errorf("unknown delete action: %q", action)
}
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ func TestConvertAlterTableStatements(t *testing.T) {
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d);",
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyReferenceOnDeleteNOACTION),
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyActionNOACTION),
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE NO ACTION;",
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyReferenceOnDeleteNOACTION),
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyActionNOACTION),
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE RESTRICT;",
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyReferenceOnDeleteRESTRICT),
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyActionRESTRICT),
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET DEFAULT ;",
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyReferenceOnDeleteSETDEFAULT),
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyActionSETDEFAULT),
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET NULL;",
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyReferenceOnDeleteSETNULL),
expectedOp: expect.AddForeignKeyOp1WithOnDelete(migrations.ForeignKeyActionSETNULL),
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT fk_bar_c FOREIGN KEY (a) REFERENCES bar (c);",
Expand Down Expand Up @@ -196,31 +196,31 @@ func TestConvertAlterTableStatements(t *testing.T) {
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar)",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteNOACTION),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionNOACTION),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON UPDATE NO ACTION",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteNOACTION),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionNOACTION),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE NO ACTION",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteNOACTION),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionNOACTION),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE RESTRICT",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteRESTRICT),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionRESTRICT),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE SET NULL ",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteSETNULL),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionSETNULL),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE SET DEFAULT",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteSETDEFAULT),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionSETDEFAULT),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE CASCADE",
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyReferenceOnDeleteCASCADE),
expectedOp: expect.AddColumnOp8WithOnDeleteAction(migrations.ForeignKeyActionCASCADE),
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int GENERATED BY DEFAULT AS IDENTITY ",
Expand Down
Loading