Skip to content

Commit

Permalink
mysql(ticdc): handle the case when @@sql_mode is null (pingcap#7992) (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jan 4, 2023
1 parent 48ecd66 commit 3626cbd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 7 additions & 3 deletions cdc/sink/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,17 @@ func needSwitchDB(ddl *model.DDLEvent) bool {
return true
}

func querySQLMode(ctx context.Context, db *sql.DB) (sqlMode string, err error) {
func querySQLMode(ctx context.Context, db *sql.DB) (string, error) {
row := db.QueryRowContext(ctx, "SELECT @@SESSION.sql_mode;")
err = row.Scan(&sqlMode)
var sqlMode sql.NullString
err := row.Scan(&sqlMode)
if err != nil {
err = cerror.WrapError(cerror.ErrMySQLQueryError, err)
}
return
if !sqlMode.Valid {
sqlMode.String = ""
}
return sqlMode.String, err
}

// check whether the target charset is supported
Expand Down
8 changes: 6 additions & 2 deletions cdc/sink/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,10 @@ func TestReduceReplace(t *testing.T) {
}

func mockTestDB(adjustSQLMode bool) (*sql.DB, error) {
return mockTestDBWithSQLMode(adjustSQLMode, "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE")
}

func mockTestDBWithSQLMode(adjustSQLMode bool, sqlMode interface{}) (*sql.DB, error) {
// mock for test db, which is used querying TiDB session variable
db, mock, err := sqlmock.New()
if err != nil {
Expand All @@ -955,7 +959,7 @@ func mockTestDB(adjustSQLMode bool) (*sql.DB, error) {
if adjustSQLMode {
mock.ExpectQuery("SELECT @@SESSION.sql_mode;").
WillReturnRows(sqlmock.NewRows([]string{"@@SESSION.sql_mode"}).
AddRow("ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"))
AddRow(sqlMode))
}

columns := []string{"Variable_name", "Value"}
Expand Down Expand Up @@ -998,7 +1002,7 @@ func TestAdjustSQLMode(t *testing.T) {
}()
if dbIndex == 0 {
// test db
db, err := mockTestDB(true)
db, err := mockTestDBWithSQLMode(true, nil)
require.Nil(t, err)
return db, nil
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/sink/mysql/db_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,17 @@ func generateDSNByConfig(
return dsnCfg.FormatDSN(), nil
}

func querySQLMode(ctx context.Context, db *sql.DB) (sqlMode string, err error) {
func querySQLMode(ctx context.Context, db *sql.DB) (string, error) {
row := db.QueryRowContext(ctx, "SELECT @@SESSION.sql_mode;")
err = row.Scan(&sqlMode)
var sqlMode sql.NullString
err := row.Scan(&sqlMode)
if err != nil {
err = cerror.WrapError(cerror.ErrMySQLQueryError, err)
}
return
if !sqlMode.Valid {
sqlMode.String = ""
}
return sqlMode.String, err
}

// check whether the target charset is supported
Expand Down

0 comments on commit 3626cbd

Please sign in to comment.