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

Fix the returned schema object for broken views #12713

Merged
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small equivalent simplification here.

}

// 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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment was wrong here as well.

// 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the logic and the comment.

}
}
}
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