Skip to content

Commit

Permalink
adding support for interleaving for sources without foreign key actio…
Browse files Browse the repository at this point in the history
…n support
  • Loading branch information
aasthabharill-google committed Jul 18, 2024
1 parent 7daf613 commit 1b49665
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 0 additions & 1 deletion sources/spanner/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ func (isi InfoSchemaImpl) GetIndexes(conv *internal.Conv, table common.SchemaAnd
return indexes, nil
}

// TODO: Extract & initialise ON DELETE action and return it (ON_DELETE_ACTION in information_schema.tables)
func (isi InfoSchemaImpl) GetInterleaveTables(spSchema ddl.Schema) (map[string]ddl.InterleavedParent, error) {
q := `SELECT table_name, parent_table_name, on_delete_action FROM information_schema.tables
WHERE interleave_type = 'IN PARENT' AND table_type = 'BASE TABLE' AND table_schema = ''`
Expand Down
7 changes: 5 additions & 2 deletions spanner/ddl/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,12 @@ func (ct CreateTable) PrintCreateTable(spSchema Schema, config Config) string {
if config.SpDialect == constants.DIALECT_POSTGRESQL {
// PG spanner only supports PRIMARY KEY() inside the CREATE TABLE()
// and thus INTERLEAVE follows immediately after closing brace.
interleave = " INTERLEAVE IN PARENT " + config.quote(parent) + " ON DELETE " + ct.ParentTable.OnDelete
interleave = " INTERLEAVE IN PARENT " + config.quote(parent)
} else {
interleave = ",\nINTERLEAVE IN PARENT " + config.quote(parent) + " ON DELETE " + ct.ParentTable.OnDelete
interleave = ",\nINTERLEAVE IN PARENT " + config.quote(parent)
}
if ct.ParentTable.OnDelete != "" {
interleave = interleave + " ON DELETE " + ct.ParentTable.OnDelete
}
}

Expand Down
23 changes: 23 additions & 0 deletions spanner/ddl/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func TestPrintCreateTable(t *testing.T) {
Comment: "",
Id: "1",
}
t3 := CreateTable{
Name: "mytable",
ColIds: []string{"col1", "col2", "col3"},
ShardIdColumn: "",
ColDefs: cds,
PrimaryKeys: []IndexKey{{ColId: "col1", Desc: true}},
ForeignKeys: nil,
Indexes: nil,
ParentTable: InterleavedParent{Id: "par1", OnDelete: ""},
Comment: "",
Id: "1",
}
tests := []struct {
name string
protectIds bool
Expand Down Expand Up @@ -195,6 +207,17 @@ func TestPrintCreateTable(t *testing.T) {
") PRIMARY KEY (col1 DESC),\n" +
"INTERLEAVE IN PARENT ON DELETE CASCADE",
},
{
"interleaved without on delete support",
false,
t3,
"CREATE TABLE mytable (\n" +
" col1 INT64 NOT NULL ,\n" +
" col2 STRING(MAX),\n" +
" col3 BYTES(42),\n" +
") PRIMARY KEY (col1 DESC),\n" +
"INTERLEAVE IN PARENT ",
},
}
for _, tc := range tests {
assert.Equal(t, tc.expected, tc.ct.PrintCreateTable(Schema{}, Config{ProtectIds: tc.protectIds}))
Expand Down

0 comments on commit 1b49665

Please sign in to comment.