Skip to content

Commit c82ba4e

Browse files
authored
ddl_notifier: create system table in test and escape the string literal (#56734)
ref #55722
1 parent dfd6cf2 commit c82ba4e

8 files changed

+33
-13
lines changed

pkg/ddl/constant.go

+11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
BackgroundSubtaskTableID = meta.MaxInt48 - 5
3939
// BackgroundSubtaskHistoryTableID is the table ID of `tidb_background_subtask_history`.
4040
BackgroundSubtaskHistoryTableID = meta.MaxInt48 - 6
41+
// NotifierTableID is the table ID of `tidb_ddl_notifier`.
42+
NotifierTableID = meta.MaxInt48 - 7
4143

4244
// JobTableSQL is the CREATE TABLE SQL of `tidb_ddl_job`.
4345
JobTableSQL = "create table " + JobTable + `(
@@ -117,4 +119,13 @@ const (
117119
summary json,
118120
key idx_task_key(task_key),
119121
key idx_state_update_time(state_update_time))`
122+
123+
// NotifierTableSQL is the CREATE TABLE SQL of `tidb_ddl_notifier`.
124+
// TODO(lance6716): update the column name multi_schema_change_seq
125+
NotifierTableSQL = `CREATE TABLE tidb_ddl_notifier (
126+
ddl_job_id BIGINT,
127+
multi_schema_change_seq BIGINT COMMENT '-1 if the schema change does not belong to a multi-schema change DDL. 0 or positive numbers representing the sub-job index of a multi-schema change DDL',
128+
schema_change LONGBLOB COMMENT 'SchemaChangeEvent at rest',
129+
processed_by_flag BIGINT UNSIGNED DEFAULT 0 COMMENT 'flag to mark which subscriber has processed the event',
130+
PRIMARY KEY(ddl_job_id, multi_schema_change_seq))`
120131
)

pkg/ddl/notifier/store.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ func (t *tableStore) Insert(ctx context.Context, s *sess.Session, change *schema
5656
multi_schema_change_seq,
5757
schema_change,
5858
processed_by_flag
59-
) VALUES (%d, %d, '%s', 0)`,
59+
) VALUES (%%?, %%?, %%?, 0)`,
6060
t.db, t.table,
61+
)
62+
_, err = s.Execute(
63+
ctx, sql, "ddl_notifier",
6164
change.ddlJobID, change.multiSchemaChangeSeq, event,
6265
)
63-
_, err = s.Execute(ctx, sql, "ddl_notifier")
6466
return err
6567
}
6668

pkg/ddl/notifier/testkit_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ CREATE TABLE ddl_notifier (
4646
func TestPublishToTableStore(t *testing.T) {
4747
store := testkit.CreateMockStore(t)
4848
tk := testkit.NewTestKit(t, store)
49-
tk.MustExec("USE test")
50-
tk.MustExec("DROP TABLE IF EXISTS ddl_notifier")
51-
tk.MustExec(tableStructure)
49+
t.Cleanup(func() {
50+
tk.MustExec("TRUNCATE mysql.tidb_ddl_notifier")
51+
})
5252

5353
ctx := context.Background()
54-
s := notifier.OpenTableStore("test", "ddl_notifier")
54+
s := notifier.OpenTableStore("mysql", "tidb_ddl_notifier")
5555
se := sess.NewSession(tk.Session())
5656
event1 := notifier.NewCreateTablesEvent([]*model.TableInfo{{ID: 1000, Name: pmodel.NewCIStr("t1")}})
5757
err := notifier.PubSchemeChangeToStore(ctx, se, 1, -1, event1, s)
@@ -114,7 +114,7 @@ func TestBasicPubSub(t *testing.T) {
114114
event1 := notifier.NewCreateTablesEvent([]*model.TableInfo{{ID: 1000, Name: pmodel.NewCIStr("t1")}})
115115
err := notifier.PubSchemeChangeToStore(ctx, se, 1, -1, event1, s)
116116
require.NoError(t, err)
117-
event2 := notifier.NewDropTableEvent(&model.TableInfo{ID: 1001, Name: pmodel.NewCIStr("t2")})
117+
event2 := notifier.NewDropTableEvent(&model.TableInfo{ID: 1001, Name: pmodel.NewCIStr("t2#special-char?in'name")})
118118
err = notifier.PubSchemeChangeToStore(ctx, se, 2, -1, event2, s)
119119
require.NoError(t, err)
120120
event3 := notifier.NewDropTableEvent(&model.TableInfo{ID: 1002, Name: pmodel.NewCIStr("t3")})

pkg/ddl/session/session.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *Session) Reset() {
7272
}
7373

7474
// Execute executes a query.
75-
func (s *Session) Execute(ctx context.Context, query string, label string) ([]chunk.Row, error) {
75+
func (s *Session) Execute(ctx context.Context, query string, label string, args ...any) ([]chunk.Row, error) {
7676
startTime := time.Now()
7777
var err error
7878
defer func() {
@@ -82,7 +82,7 @@ func (s *Session) Execute(ctx context.Context, query string, label string) ([]ch
8282
if ctx.Value(kv.RequestSourceKey) == nil {
8383
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnDDL)
8484
}
85-
rs, err := s.Context.GetSQLExecutor().ExecuteInternal(ctx, query)
85+
rs, err := s.Context.GetSQLExecutor().ExecuteInternal(ctx, query, args...)
8686
if err != nil {
8787
return nil, errors.Trace(err)
8888
}

pkg/executor/infoschema_cluster_table_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func TestTableStorageStats(t *testing.T) {
393393
"test 2",
394394
))
395395
rows := tk.MustQuery("select TABLE_NAME from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql';").Rows()
396-
result := 57
396+
result := 58
397397
require.Len(t, rows, result)
398398

399399
// More tests about the privileges.

pkg/executor/infoschema_reader_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ func TestColumnTable(t *testing.T) {
607607
testkit.RowsWithSep("|",
608608
"test|tbl1|col_2"))
609609
tk.MustQuery(`select count(*) from information_schema.columns;`).Check(
610-
testkit.RowsWithSep("|", "4979"))
610+
testkit.RowsWithSep("|", "4983"))
611611
}
612612

613613
func TestIndexUsageTable(t *testing.T) {
@@ -654,7 +654,7 @@ func TestIndexUsageTable(t *testing.T) {
654654
testkit.RowsWithSep("|",
655655
"test|idt2|idx_4"))
656656
tk.MustQuery(`select count(*) from information_schema.tidb_index_usage;`).Check(
657-
testkit.RowsWithSep("|", "77"))
657+
testkit.RowsWithSep("|", "78"))
658658

659659
tk.MustQuery(`select TABLE_SCHEMA, TABLE_NAME, INDEX_NAME from information_schema.tidb_index_usage
660660
where TABLE_SCHEMA = 'test1';`).Check(testkit.Rows())

pkg/session/bootstrap_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ func TestDDLTableCreateBackfillTable(t *testing.T) {
250250
m.SetDDLTables(meta.MDLTableVersion)
251251
MustExec(t, se, "drop table mysql.tidb_background_subtask")
252252
MustExec(t, se, "drop table mysql.tidb_background_subtask_history")
253+
// TODO(lance6716): remove it after tidb_ddl_notifier GA
254+
MustExec(t, se, "drop table mysql.tidb_ddl_notifier")
253255
err = txn.Commit(context.Background())
254256
require.NoError(t, err)
255257

pkg/session/session.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -3222,11 +3222,16 @@ func splitAndScatterTable(store kv.Storage, tableIDs []int64) {
32223222
}
32233223
}
32243224

3225-
// InitDDLJobTables is to create tidb_ddl_job, tidb_ddl_reorg and tidb_ddl_history, or tidb_background_subtask and tidb_background_subtask_history.
3225+
// InitDDLJobTables creates system tables that DDL uses. Because CREATE TABLE is
3226+
// also a DDL, we must directly modify KV data to create these tables.
32263227
func InitDDLJobTables(store kv.Storage, targetVer meta.DDLTableVersion) error {
32273228
targetTables := DDLJobTables
32283229
if targetVer == meta.BackfillTableVersion {
32293230
targetTables = BackfillTables
3231+
if intest.InTest {
3232+
// create the system tables to test ddl notifier
3233+
targetTables = append(targetTables, tableBasicInfo{ddl.NotifierTableSQL, ddl.NotifierTableID})
3234+
}
32303235
}
32313236
return kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL), store, true, func(_ context.Context, txn kv.Transaction) error {
32323237
t := meta.NewMutator(txn)

0 commit comments

Comments
 (0)