Skip to content

Commit

Permalink
Fix the returned schema object for broken views (#12713)
Browse files Browse the repository at this point in the history
The logic added in #12675 has a
small issue. When a view is found that has an error, it's not present
anymore when a subsequent `.ToSQL()` call happens, so it looks like the
view disappears.

We should never have a view disappear since that could potentially break
things and lead to unexpected results.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink authored Mar 23, 2023
1 parent d061b2b commit 647a19e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 6 additions & 6 deletions go/vt/schemadiff/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ func NewSchemaFromEntities(entities []Entity) (*Schema, error) {
return nil, &UnsupportedEntityError{Entity: c.Name(), Statement: c.Create().CanonicalStatementString()}
}
}
if err := schema.normalize(); err != nil {
return schema, err
}
return schema, nil
err := schema.normalize()
return schema, err
}

// NewSchemaFromStatements creates a valid and normalized schema based on list of valid statements
Expand Down Expand Up @@ -309,16 +307,18 @@ func (s *Schema) normalize() error {
// - two or more views have a circular dependency
for _, t := range s.tables {
if _, ok := dependencyLevels[t.Name()]; !ok {
// We _know_ that in this iteration, at least one view is found unassigned a dependency level.
// We _know_ that in this iteration, at least one foreign key is not found.
// We return the first one.
return &ForeignKeyDependencyUnresolvedError{Table: t.Name()}
}
}
for _, v := range s.views {
if _, ok := dependencyLevels[v.Name()]; !ok {
// We _know_ that in this iteration, at least one view is found unassigned a dependency level.
// We return the first one.
// We gather all the errors.
errs = errors.Join(errs, &ViewDependencyUnresolvedError{View: v.ViewName.Name.String()})
// We still add it so it shows up in the output if that is used for anything.
s.sorted = append(s.sorted, v)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion go/vt/schemadiff/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ func TestNewSchemaFromQueriesUnresolved(t *testing.T) {
queries := append(createQueries,
"create view v7 as select * from v8, t2",
)
_, err := NewSchemaFromQueries(queries)
schema, err := NewSchemaFromQueries(queries)
assert.Error(t, err)
assert.EqualError(t, err, (&ViewDependencyUnresolvedError{View: "v7"}).Error())
v := schema.sorted[len(schema.sorted)-1]
assert.IsType(t, &CreateViewEntity{}, v)
assert.Equal(t, "CREATE VIEW `v7` AS SELECT * FROM `v8`, `t2`", v.Create().CanonicalStatementString())
}

func TestNewSchemaFromQueriesUnresolvedAlias(t *testing.T) {
Expand Down

0 comments on commit 647a19e

Please sign in to comment.