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
34 changes: 34 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@ var ScriptTests = []ScriptTest{
},
},
},
{
Name: "GMS issue 2369",
SetUpScript: []string{
`CREATE TABLE table1 (
id int NOT NULL AUTO_INCREMENT,
name text,
parentId int DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT myConstraint FOREIGN KEY (parentId) REFERENCES table1 (id) ON DELETE CASCADE
)`,
},
Assertions: []ScriptTestAssertion{
{
Query: "INSERT INTO table1 (name, parentId) VALUES ('tbl1 row 1', NULL);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}},
},
{
Query: "INSERT INTO table1 (name, parentId) VALUES ('tbl1 row 2', 1);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 2}}},
},
{
Query: "INSERT INTO table1 (name, parentId) VALUES ('tbl1 row 3', NULL);",
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 3}}},
},
{
Query: "select * from table1",
Expected: []sql.Row{
{1, "tbl1 row 1", nil},
{2, "tbl1 row 2", 1},
{3, "tbl1 row 3", nil},
},
},
},
},
{
Name: "GMS issue 2349",
SetUpScript: []string{
Expand Down
1 change: 1 addition & 0 deletions memory/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ func (t *Table) tableEditorForRewrite(ctx *sql.Context, oldSchema, newSchema sql
tableData := tableUnderEdit.data.truncate(normalizeSchemaForRewrite(newSchema))
tableUnderEdit.data = tableData

// TODO: |editedTableAnd| and |ea| should have the same tableData reference
uniqIdxCols, prefixLengths := tableData.indexColsForTableEditor()
var editor sql.TableEditor = &tableEditor{
editedTable: tableUnderEdit,
Expand Down
24 changes: 18 additions & 6 deletions memory/table_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ func (t *tableEditor) Insert(ctx *sql.Context, row sql.Row) error {
return err
}

idx := t.editedTable.data.autoColIdx
// todo could peek into internals, not use editedTable t.ea.(*pkTableEditAccumulator).tableData
idx := t.ea.TableData().autoColIdx
if idx >= 0 {
autoCol := t.editedTable.data.schema.Schema[idx]
cmp, err := autoCol.Type.Compare(row[idx], t.editedTable.data.autoIncVal)
autoCol := t.ea.TableData().schema.Schema[idx]
cmp, err := autoCol.Type.Compare(row[idx], t.ea.TableData().autoIncVal)
if err != nil {
return err
}
Expand All @@ -191,11 +192,11 @@ func (t *tableEditor) Insert(ctx *sql.Context, row sql.Row) error {
if err != nil {
return err
}
t.editedTable.data.autoIncVal = v.(uint64)
t.editedTable.data.autoIncVal++ // Move onto next autoIncVal
t.ea.TableData().autoIncVal = v.(uint64)
t.ea.TableData().autoIncVal++ // Move onto next autoIncVal
} else if cmp == 0 {
// Provided value equal to autoIncVal
t.editedTable.data.autoIncVal++ // Move onto next autoIncVal
t.ea.TableData().autoIncVal++ // Move onto next autoIncVal
}
}

Expand Down Expand Up @@ -372,6 +373,9 @@ type tableEditAccumulator interface {
GetByCols(value sql.Row, cols []int, prefixLengths []uint16) (sql.Row, bool, error)
// Clear wipes all of the stored inserts and deletes that may or may not have been applied.
Clear()
// TableData exposes the session table data for this accumulator. Currently used for mutating
// autoincrement in a way that will track row changes.
TableData() *TableData
}

// newTableEditAccumulator returns a tableEditAccumulator based on the schema.
Expand Down Expand Up @@ -400,6 +404,10 @@ type pkTableEditAccumulator struct {

var _ tableEditAccumulator = (*pkTableEditAccumulator)(nil)

func (pke *pkTableEditAccumulator) TableData() *TableData {
return pke.tableData
}

// Insert implements the tableEditAccumulator interface.
func (pke *pkTableEditAccumulator) Insert(value sql.Row) error {
rowKey := pke.getRowKey(value)
Expand Down Expand Up @@ -650,6 +658,10 @@ type keylessTableEditAccumulator struct {

var _ tableEditAccumulator = (*keylessTableEditAccumulator)(nil)

func (k *keylessTableEditAccumulator) TableData() *TableData {
return k.tableData
}

// Insert implements the tableEditAccumulator interface.
func (k *keylessTableEditAccumulator) Insert(value sql.Row) error {
for i, row := range k.deletes {
Expand Down