Skip to content

Commit

Permalink
schemadiff: granular foreign key reference errors
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
  • Loading branch information
shlomi-noach committed Dec 5, 2023
1 parent fe01a76 commit c214f7a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
20 changes: 20 additions & 0 deletions go/vt/schemadiff/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,26 @@ func (e *ForeignKeyDependencyUnresolvedError) Error() string {
sqlescape.EscapeID(e.Table))
}

type ForeignKeyNonexistentReferencedTableError struct {
Table string
ReferencedTable string
}

func (e *ForeignKeyNonexistentReferencedTableError) Error() string {
return fmt.Sprintf("table %s foreign key references nonexistent table %s",
sqlescape.EscapeID(e.Table), sqlescape.EscapeID(e.ReferencedTable))
}

type ForeignKeyReferencesViewError struct {
Table string
ReferencedView string
}

func (e *ForeignKeyReferencesViewError) Error() string {
return fmt.Sprintf("table %s foreign key references view %s",
sqlescape.EscapeID(e.Table), sqlescape.EscapeID(e.ReferencedView))
}

type InvalidColumnInForeignKeyConstraintError struct {
Table string
Constraint string
Expand Down
8 changes: 8 additions & 0 deletions go/vt/schemadiff/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ func (s *Schema) normalize() error {
if referencedTableName != name {
nonSelfReferenceNames = append(nonSelfReferenceNames, referencedTableName)
}
referencedEntity, ok := s.named[referencedTableName]
if !ok {
return &ForeignKeyNonexistentReferencedTableError{Table: name, ReferencedTable: referencedTableName}
}
if _, ok := referencedEntity.(*CreateViewEntity); ok {
return &ForeignKeyReferencesViewError{Table: name, ReferencedView: referencedTableName}
}

fkParents[referencedTableName] = true
}
if allNamesFoundInLowerLevel(nonSelfReferenceNames, iterationLevel) {
Expand Down
10 changes: 9 additions & 1 deletion go/vt/schemadiff/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,15 @@ func TestInvalidSchema(t *testing.T) {
expectErr: &ForeignKeyColumnCountMismatchError{Table: "t11", Constraint: "f11", ColumnCount: 2, ReferencedTable: "t11", ReferencedColumnCount: 1},
},
{
schema: "create table t11 (id int primary key, i int, constraint f12 foreign key (i) references t12(id) on delete restrict)",
schema: "create table t11 (id int primary key, i int, constraint f12 foreign key (i) references t12 (id) on delete restrict)",
expectErr: &ForeignKeyNonexistentReferencedTableError{Table: "t11", ReferencedTable: "t12"},
},
{
schema: "create view v as select 1 as id from dual; create table t11 (id int primary key, i int, constraint fv foreign key (i) references v (id) on delete restrict)",
expectErr: &ForeignKeyReferencesViewError{Table: "t11", ReferencedView: "v"},
},
{
schema: "create table t11 (id int primary key, i int, constraint f11 foreign key (i) references t12 (id) on delete restrict); create table t12 (id int primary key, i int, constraint f12 foreign key (i) references t11 (id) on delete restrict)",
expectErr: &ForeignKeyDependencyUnresolvedError{Table: "t11"},
},
{
Expand Down

0 comments on commit c214f7a

Please sign in to comment.