Skip to content
Open
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
32 changes: 32 additions & 0 deletions dm/pkg/parser/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,38 @@ func TestParser(t *testing.T) {
}
}

func TestParserSpecificCreateTableOptions(t *testing.T) {
t.Parallel()

cases := []string{
"create table t2 (id int primary key, c1 varchar(255)) transactional=0",
"CREATE TABLE `help_topic` (`help_topic_id` int unsigned NOT NULL) ENGINE=Aria DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci PAGE_CHECKSUM=1 TRANSACTIONAL=0 COMMENT='help topics'",
"create table t_auto (id int primary key) AUTOEXTEND_SIZE=4M",
}
for _, sql := range cases {
t.Run(sql, func(t *testing.T) {
Comment on lines +391 to +392

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In Go versions prior to 1.22, referencing the loop variable sql directly inside a parallel subtest (t.Parallel()) causes all subtests to run using the value of the loop variable from the last iteration. To prevent this race condition and ensure all test cases are executed correctly, capture the loop variable sql locally within the loop.

Suggested change
for _, sql := range cases {
t.Run(sql, func(t *testing.T) {
for _, sql := range cases {
sql := sql
t.Run(sql, func(t *testing.T) {

t.Parallel()
p := parser.New()

stmts, err := Parse(p, sql, "", "")
require.NoError(t, err)
require.Len(t, stmts, 1)

splitDDLs, err := SplitDDL(stmts[0], "test")
require.NoError(t, err)
require.Len(t, splitDDLs, 1)

stmts, err = Parse(p, splitDDLs[0], "", "")
require.NoError(t, err)
require.Len(t, stmts, 1)

tableNames, err := FetchDDLTables("test", stmts[0], conn.LCTableNamesSensitive)
require.NoError(t, err)
require.Len(t, tableNames, 1)
})
}
}

func TestError(t *testing.T) {
t.Parallel()
p := parser.New()
Expand Down
45 changes: 45 additions & 0 deletions dm/pkg/schema/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,51 @@ func TestDDL(t *testing.T) {
require.NoError(t, err)
}

func TestDDLWithSpecificCreateTableOptions(t *testing.T) {
t.Parallel()

ctx := context.Background()
p := parser.New()

tracker, err := NewTestTracker(ctx, "test-tracker", nil, dlog.L())
require.NoError(t, err)
defer tracker.Close()

err = tracker.Exec(ctx, "", parseSQL(t, p, "create database testdb;"))
require.NoError(t, err)

cases := []struct {
sql string
table string
expectedColumns int
}{
{
sql: "create table t2 (id int primary key, c1 varchar(255)) transactional=0",
table: "t2",
expectedColumns: 2,
},
{
sql: "CREATE TABLE `help_topic` (`help_topic_id` int unsigned NOT NULL) ENGINE=Aria DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci PAGE_CHECKSUM=1 TRANSACTIONAL=0 COMMENT='help topics'",
table: "help_topic",
expectedColumns: 1,
},
{
sql: "create table t_auto (id int primary key) AUTOEXTEND_SIZE=4M",
table: "t_auto",
expectedColumns: 1,
},
}
for _, tc := range cases {
stmt := parseSQL(t, p, tc.sql)
err = tracker.Exec(ctx, "testdb", stmt)
require.NoError(t, err)

ti, err := tracker.GetTableInfo(&filter.Table{Schema: "testdb", Name: tc.table})
require.NoError(t, err)
require.Len(t, ti.Columns, tc.expectedColumns)
}
}

func TestGetSingleColumnIndices(t *testing.T) {
ctx := context.Background()
p := parser.New()
Expand Down
Loading