Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ CREATE INDEX Test_Name_CreatedAt ON Test(Name, CreatedAt DESC);
$ gcloud spanner databases ddl update test --ddl-file=tmp.sql
Schema updating...done.
```

## Known Issues & Limitations

- View DDL generation may be incorrect or out of order due to unresolved column names in the view query.
32 changes: 29 additions & 3 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,11 +863,37 @@ func (v *view) alter(tgt definition, m *migration) {
}

func (v *view) dependsOn() []identifier {
// TODO: process query to find dependencies
return nil
var ids []identifier
paths, idents := tablesOrViewsInQueryExpr(v.node.Query)
// Can't distinguish between tables and views, so add both.
for _, ident := range idents {
ids = append(ids,
newTableIDFromIdent(ident),
newViewIDFromIdent(ident),
)
}
for _, path := range paths {
ids = append(ids,
newTableIDFromPath(path),
newViewIDFromPath(path),
)
}
// TODO: Add dependencies on columns.
// But it's difficult to extract column names from the query!
return ids
}

func (v *view) onDependencyChange(me, dependency migrationState, m *migration) {}
func (v *view) onDependencyChange(me, dependency migrationState, m *migration) {
switch dep := dependency.definition().(type) {
case *column, *table, *view:
switch dependency.kind {
case migrationKindDropAndAdd:
m.updateState(me.updateKind(migrationKindDropAndAdd))
}
default:
panic(fmt.Sprintf("unexpected dependOn type on view: %T", dep))
}
}

type changeStream struct {
node *ast.CreateChangeStream
Expand Down
30 changes: 30 additions & 0 deletions spannerdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,26 @@ func TestDiff(t *testing.T) {
CREATE OR REPLACE VIEW V1 SQL SECURITY DEFINER AS SELECT * FROM T1 WHERE T1_I1 > 0;`,
false,
},
"drop and create view": {
`
CREATE TABLE T1 (
T1_I1 INT64 NOT NULL,
) PRIMARY KEY(T1_I1);
CREATE VIEW V1 SQL SECURITY DEFINER AS SELECT * FROM T1;`,
`
CREATE TABLE T1 (
T1_S1 STRING(MAX) NOT NULL,
) PRIMARY KEY(T1_S1);
CREATE VIEW V1 SQL SECURITY DEFINER AS SELECT * FROM T1;`,
`
DROP VIEW V1;
DROP TABLE T1;
CREATE TABLE T1 (
T1_S1 STRING(MAX) NOT NULL,
) PRIMARY KEY(T1_S1);
CREATE VIEW V1 SQL SECURITY DEFINER AS SELECT * FROM T1;`,
false,
},
"add change stream": {
``,
`
Expand Down Expand Up @@ -772,6 +792,16 @@ func TestDiff(t *testing.T) {
ALTER DATABASE D1 SET OPTIONS (version_retention_period = '2d');`,
false,
},
"issue #35": {
``,
`
CREATE OR REPLACE VIEW V2 SQL SECURITY INVOKER AS SELECT * FROM T1;
CREATE OR REPLACE VIEW V1 SQL SECURITY INVOKER AS SELECT * FROM V2;`,
`
CREATE OR REPLACE VIEW V2 SQL SECURITY INVOKER AS SELECT * FROM T1;
CREATE OR REPLACE VIEW V1 SQL SECURITY INVOKER AS SELECT * FROM V2;`,
false,
},
} {
t.Run(name, func(t *testing.T) {
var buf bytes.Buffer
Expand Down
16 changes: 16 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,19 @@ func uniqueIdent(is []*ast.Ident) []*ast.Ident {
return i.Name
})
}

func tablesOrViewsInQueryExpr(expr ast.QueryExpr) ([]*ast.Path, []*ast.Ident) {
var idents []*ast.Ident
var paths []*ast.Path

ast.Inspect(expr, func(n ast.Node) bool {
switch t := n.(type) {
case *ast.TableName:
idents = append(idents, t.Table)
case *ast.PathTableExpr:
paths = append(paths, t.Path)
}
return true
})
return paths, idents
}