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(spanddl): support dropping interleaved tables #501

Merged
merged 3 commits into from
Jan 8, 2024
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
26 changes: 26 additions & 0 deletions spanddl/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spanddl

import (
"fmt"
"strings"

"cloud.google.com/go/spanner/spansql"
)
Expand Down Expand Up @@ -118,7 +119,15 @@ func (d *Database) applyDropTable(stmt *spansql.DropTable) (err error) {
if i == -1 {
return fmt.Errorf("table %s does not exist", stmt.Name)
}
if table, ok := d.Table(stmt.Name); ok && len(table.InterleavedTables) > 0 {
names := make([]string, 0, len(table.InterleavedTables))
for _, t := range table.InterleavedTables {
names = append(names, string(t.Name))
}
return fmt.Errorf("table %s has interleaved tables %s", stmt.Name, strings.Join(names, ", "))
}
d.Tables = append(d.Tables[:i], d.Tables[i+1:]...)
d.removeInterleavedReferenceFromParentTable(stmt.Name)
return nil
}

Expand Down Expand Up @@ -177,3 +186,20 @@ func (d *Database) indexOfIndex(name spansql.ID) int {
}
return -1
}

func (d *Database) removeInterleavedReferenceFromParentTable(name spansql.ID) {
for _, table := range d.Tables {
index := -1
for i, it := range table.InterleavedTables {
if it.Name == name {
index = i
break
}
}
if index >= 0 {
// Parent table found
table.InterleavedTables = append(table.InterleavedTables[:index], table.InterleavedTables[index+1:]...)
return
}
}
}
95 changes: 92 additions & 3 deletions spanddl/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestDatabase_ApplyDDL(t *testing.T) {
name string
ddls []string
expected *Database
errorDdlIndex int
errorContains string
}{
{
Expand Down Expand Up @@ -45,6 +46,23 @@ func TestDatabase_ApplyDDL(t *testing.T) {
},
},

{
name: "drop missing table",
ddls: []string{
`CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
BirthDate DATE,
) PRIMARY KEY(SingerId);`,

`DROP TABLE Albums`,
},
errorDdlIndex: 1,
errorContains: "DROP TABLE: table Albums does not exist",
Copy link
Member Author

Choose a reason for hiding this comment

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

errorContains was unused before this pr.

},

{
name: "create table with row deletion policy",
ddls: []string{
Expand Down Expand Up @@ -274,6 +292,77 @@ func TestDatabase_ApplyDDL(t *testing.T) {
},
},

{
name: "drop interleaved",
ddls: []string{
`CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
BirthDate DATE,
) PRIMARY KEY(SingerId);`,

`CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX),
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE;`,

`DROP TABLE Albums;`,
},
expected: &Database{
Tables: []*Table{
{
Name: "Singers",
Columns: []*Column{
{Name: "SingerId", Type: spansql.Type{Base: spansql.Int64}, NotNull: true},
{Name: "FirstName", Type: spansql.Type{Base: spansql.String, Len: 1024}},
{Name: "LastName", Type: spansql.Type{Base: spansql.String, Len: 1024}},
{Name: "SingerInfo", Type: spansql.Type{Base: spansql.Bytes, Len: spansql.MaxLen}},
{Name: "BirthDate", Type: spansql.Type{Base: spansql.Date}},
},
PrimaryKey: []spansql.KeyPart{
{Column: "SingerId"},
},
InterleavedTables: []*Table{},
},
},
},
},

{
name: "drop table with interleaved tables",
ddls: []string{
`CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
BirthDate DATE,
) PRIMARY KEY(SingerId);`,

`CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX),
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE;`,

`CREATE TABLE Genres (
SingerId INT64 NOT NULL,
GenreId INT64 NOT NULL,
GenreTitle STRING(MAX),
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE;`,

`DROP TABLE Singers;`,
},
errorDdlIndex: 3,
errorContains: "DROP TABLE: table Singers has interleaved tables Albums, Genres",
},

{
name: "create index",
ddls: []string{
Expand Down Expand Up @@ -356,13 +445,13 @@ func TestDatabase_ApplyDDL(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var db Database
for _, ddl := range tt.ddls {
for i, ddl := range tt.ddls {
ddl, err := spansql.ParseDDL(tt.name, ddl)
assert.NilError(t, err)
err = db.ApplyDDL(ddl)
if tt.errorContains != "" {
if tt.errorDdlIndex == i && tt.errorContains != "" {
assert.ErrorContains(t, err, tt.errorContains)
break
return
Copy link
Member Author

Choose a reason for hiding this comment

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

exit early if any ddl statement thrown an error

}
assert.NilError(t, err)
}
Expand Down
8 changes: 8 additions & 0 deletions testdata/migrations/music/000002_create_drop_genres.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- This migration should result in no schema changes.
CREATE TABLE Genres (
SingerId INT64 NOT NULL,
GenreId INT64 NOT NULL,
) PRIMARY KEY (SingerId, GenreId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE;

DROP TABLE Genres;