Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema(ticdc): add unit test for handle key column #8547

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
59 changes: 59 additions & 0 deletions cdc/entry/schema_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,3 +921,62 @@ func getAllHistoryDDLJob(storage tidbkv.Storage) ([]*timodel.Job, error) {
}
return jobs, nil
}

// This test is used to show how the schemaStorage choose a handleKey of a table.
// The handleKey is chosen by the following rules:
// 1. If the table has a primary key, the handleKey is the first column of the primary key.
// 2. If the table has not null unique key, the handleKey is the first column of the unique key.
// 3. If the table has no primary key and no not null unique key, it has no handleKey.
func TestHandleKey(t *testing.T) {
store, err := mockstore.NewMockStore()
require.Nil(t, err)
defer store.Close() //nolint:errcheck

session.SetSchemaLease(0)
session.DisableStats4Test()
domain, err := session.BootstrapSession(store)
require.Nil(t, err)
defer domain.Close()
domain.SetStatsUpdating(true)
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database test2")
tk.MustExec("create table test.simple_test1 (id bigint primary key)")
tk.MustExec("create table test.simple_test2 (id bigint, age int NOT NULL, " +
"name char NOT NULL, UNIQUE KEY(age, name))")
tk.MustExec("create table test.simple_test3 (id bigint, age int)")
ver, err := store.CurrentVersion(oracle.GlobalTxnScope)
require.Nil(t, err)
meta, err := kv.GetSnapshotMeta(store, ver.Ver)
require.Nil(t, err)
snap, err := schema.NewSnapshotFromMeta(meta, ver.Ver, false)
require.Nil(t, err)
tb1, ok := snap.TableByName("test", "simple_test1")
require.True(t, ok)
require.Equal(t, int64(-1), tb1.HandleIndexID) // pk is handleKey
columnID := int64(1)
flag := tb1.ColumnsFlag[columnID]
require.True(t, flag.IsHandleKey())
for _, column := range tb1.Columns {
if column.ID == columnID {
require.True(t, column.Name.O == "id")
}
}

// unique key is handleKey
tb2, ok := snap.TableByName("test", "simple_test2")
require.True(t, ok)
require.Equal(t, int64(1), tb2.HandleIndexID)
columnID = int64(2)
flag = tb2.ColumnsFlag[columnID]
require.True(t, flag.IsHandleKey())
for _, column := range tb2.Columns {
if column.ID == columnID {
require.True(t, column.Name.O == "age")
}
}

// has no handleKey
tb3, ok := snap.TableByName("test", "simple_test3")
require.True(t, ok)
require.Equal(t, int64(-2), tb3.HandleIndexID)
}
4 changes: 4 additions & 0 deletions cdc/model/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const (
// BinaryFlag means the column charset is binary
BinaryFlag ColumnFlagType = 1 << ColumnFlagType(iota)
// HandleKeyFlag means the column is selected as the handle key
// The handleKey is chosen by the following rules:
// 1. If the table has a primary key, the handleKey is the first column of the primary key.
// 2. If the table has not null unique key, the handleKey is the first column of the unique key.
// 3. If the table has no primary key and no not null unique key, it has no handleKey.
HandleKeyFlag
// GeneratedColumnFlag means the column is a generated column
GeneratedColumnFlag
Expand Down