Skip to content

Commit

Permalink
fix(spanddl): support dropping interleaved tables
Browse files Browse the repository at this point in the history
Before the parent would have an reference to the dropped table, which
caused an type error since the type could not be resolved.

This PR removes the child reference in the parent table.
  • Loading branch information
thall committed Jan 8, 2024
1 parent 93988ca commit 54c6334
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
18 changes: 18 additions & 0 deletions spanddl/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (d *Database) applyDropTable(stmt *spansql.DropTable) (err error) {
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 @@ -185,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
}
}
}
40 changes: 40 additions & 0 deletions spanddl/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,46 @@ 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{
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;

0 comments on commit 54c6334

Please sign in to comment.