Skip to content

Commit

Permalink
Online DDL: edit CONSTRAINT names in CREATE TABLE
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach committed Nov 13, 2023
1 parent 0fd0b36 commit 1ef698b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 16 additions & 1 deletion go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,21 @@ func testScheduler(t *testing.T) {
})
})

// Constraints
t.Run("CREATE TABLE with CHECK constraint", func(t *testing.T) {
query := `create table with_constraint (id int primary key, check ((id >= 0)))`
uuid := testOnlineDDLStatement(t, createParams(query, ddlStrategy, "vtgate", "chk_", "", false))
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
t.Run("ensure constraint name is rewritten", func(t *testing.T) {
// Since we did not provide a name for the CHECK constraint, MySQL will
// name it `with_constraint_chk_1`. But we expect Online DDL to explicitly
// modify the constraint name, specifically to get rid of the <table-name> prefix,
// so that we don't get into https://bugs.mysql.com/bug.php?id=107772 situation.
createStatement := getCreateTableStatement(t, shards[0].Vttablets[0], "with_constraint")
assert.NotContains(t, createStatement, "with_constraint_chk")
})
})

// INSTANT DDL
instantDDLCapable, err := capableOf(mysql.InstantAddLastColumnFlavorCapability)
require.NoError(t, err)
Expand Down Expand Up @@ -2328,7 +2343,7 @@ func testRevertMigration(t *testing.T, params *testRevertMigrationParams) (uuid
return uuid
}

// checkTable checks the number of tables in the first two shards.
// checkTable checks the number of tables in all shards
func checkTable(t *testing.T, showTableName string, expectExists bool) bool {
expectCount := 0
if expectExists {
Expand Down
11 changes: 11 additions & 0 deletions go/vt/vttablet/onlineddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2863,6 +2863,17 @@ func (e *Executor) executeCreateDDLActionMigration(ctx context.Context, onlineDD
}
}
}
if originalCreateTable, ok := ddlStmt.(*sqlparser.CreateTable); ok {
newCreateTable := sqlparser.CloneRefOfCreateTable(originalCreateTable)
// Rewrite this CREATE TABLE statement such that CONSTRAINT names are edited,
// specifically removing any <tablename> prefix.
if _, err := e.validateAndEditCreateTableStatement(ctx, onlineDDL, newCreateTable); err != nil {
return failMigration(err)
}
ddlStmt = newCreateTable
onlineDDL.SQL = sqlparser.String(newCreateTable)
}

// from now on, whether a VIEW or a TABLE, they get the same treatment

sentryArtifactTableName, err := schema.GenerateGCTableName(schema.HoldTableGCState, newGCTableRetainTime())
Expand Down

0 comments on commit 1ef698b

Please sign in to comment.