Skip to content

Commit

Permalink
schemadiff: fix scenario where no tables exist in schema and with jus…
Browse files Browse the repository at this point in the history
…t views reading from DUAL (#12189)
  • Loading branch information
shlomi-noach authored Feb 3, 2023
1 parent 48d8877 commit b73da20
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions go/vt/schemadiff/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ func (s *Schema) normalize() error {
// - then we only want views that depend on 1st level views or on tables. These are 2nd level views.
// - etc.
// we stop when we have been unable to find a view in an iteration.

// It's possible that there's never been any tables in this schema. Which means
// iterationLevel remains zero.
// To deal with views, we must have iterationLevel at least 1. This is because any view reads
// from _something_: at the very least it reads from DUAL (inplicitly or explicitly). Which
// puts the view at a higher level.
if iterationLevel < 1 {
iterationLevel = 1
}
for {
handledAnyViewsInIteration := false
for _, v := range s.views {
Expand Down
18 changes: 18 additions & 0 deletions go/vt/schemadiff/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ func TestNewSchemaFromQueriesUnresolvedAlias(t *testing.T) {
assert.EqualError(t, err, (&ViewDependencyUnresolvedError{View: "v7"}).Error())
}

func TestNewSchemaFromQueriesViewFromDual(t *testing.T) {
// Schema will not contain any tables, just a view selecting from DUAL
queries := []string{
"create view v20 as select 1 from dual",
}
_, err := NewSchemaFromQueries(queries)
assert.NoError(t, err)
}

func TestNewSchemaFromQueriesViewFromDualImplicit(t *testing.T) {
// Schema will not contain any tables, just a view implicitly selecting from DUAL
queries := []string{
"create view v20 as select 1",
}
_, err := NewSchemaFromQueries(queries)
assert.NoError(t, err)
}

func TestNewSchemaFromQueriesLoop(t *testing.T) {
// v7 and v8 depend on each other
queries := append(createQueries,
Expand Down

0 comments on commit b73da20

Please sign in to comment.