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

dumpling: fix dump failed when sequence exists #30164

Merged
merged 11 commits into from
Dec 6, 2021
15 changes: 14 additions & 1 deletion dumpling/export/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,20 @@ func prepareTableListToDump(tctx *tcontext.Context, conf *Config, db *sql.Conn)
if !conf.NoViews {
tableTypes = append(tableTypes, TableTypeView)
}
conf.Tables, err = ListAllDatabasesTables(tctx, db, databases, getListTableTypeByConf(conf), tableTypes...)

ifSeqExists, err := CheckIfSeqExists(db)
if err != nil {
return err
}
var listType listTableType
if ifSeqExists {
tctx.L().Warn("dumpling tableType `sequence` is unsupported for now")
listType = listTableByShowFullTables
} else {
listType = getListTableTypeByConf(conf)
}

conf.Tables, err = ListAllDatabasesTables(tctx, db, databases, listType, tableTypes...)
if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions dumpling/export/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,18 @@ const (
TableTypeBase TableType = iota
// TableTypeView represents the view table
TableTypeView
// TableTypeSequence represents the view table
// TODO: need to be supported
TableTypeSequence
)

const (
// TableTypeBaseStr represents the basic table string
TableTypeBaseStr = "BASE TABLE"
// TableTypeViewStr represents the view table string
TableTypeViewStr = "VIEW"
// TableTypeSequenceStr represents the view table string
TableTypeSequenceStr = "SEQUENCE"
)

func (t TableType) String() string {
Expand All @@ -131,6 +136,8 @@ func (t TableType) String() string {
return TableTypeBaseStr
case TableTypeView:
return TableTypeViewStr
case TableTypeSequence:
return TableTypeSequenceStr
default:
return "UNKNOWN"
}
Expand All @@ -143,6 +150,8 @@ func ParseTableType(s string) (TableType, error) {
return TableTypeBase, nil
case TableTypeViewStr:
return TableTypeView, nil
case TableTypeSequenceStr:
return TableTypeSequence, nil
default:
return TableTypeBase, errors.Errorf("unknown table type %s", s)
}
Expand Down
13 changes: 13 additions & 0 deletions dumpling/export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,19 @@ func CheckTiDBWithTiKV(db *sql.DB) (bool, error) {
return count > 0, nil
}

// CheckIfSeqExists use sql to check whether sequence exists
func CheckIfSeqExists(db *sql.Conn) (bool, error) {
var count int
const query = "SELECT COUNT(1) as c FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='SEQUENCE'"
row := db.QueryRowContext(context.Background(), query)
err := row.Scan(&count)
if err != nil {
return false, errors.Annotatef(err, "sql: %s", query)
}

return count > 0, nil
}

// CheckTiDBEnableTableLock use sql variable to check whether current TiDB has TiKV
func CheckTiDBEnableTableLock(db *sql.Conn) (bool, error) {
tidbConfig, err := getTiDBConfig(db)
Expand Down
29 changes: 29 additions & 0 deletions dumpling/export/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,35 @@ func TestPickupPossibleField(t *testing.T) {
}
}

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

db, mock, err := sqlmock.New()
require.NoError(t, err)
defer func() {
require.NoError(t, db.Close())
}()

conn, err := db.Conn(context.Background())
sylzd marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

mock.ExpectQuery("SELECT COUNT").
WillReturnRows(sqlmock.NewRows([]string{"c"}).
AddRow("1"))

exists, err := CheckIfSeqExists(conn)
require.NoError(t, err)
require.Equal(t, true, exists)

mock.ExpectQuery("SELECT COUNT").
WillReturnRows(sqlmock.NewRows([]string{"c"}).
AddRow("0"))

exists, err = CheckIfSeqExists(conn)
require.NoError(t, err)
require.Equal(t, false, exists)
}

func TestGetCharsetAndDefaultCollation(t *testing.T) {
t.Parallel()
db, mock, err := sqlmock.New()
Expand Down
6 changes: 6 additions & 0 deletions dumpling/tests/basic/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ actual=$(sed -n '2p' ${DUMPLING_OUTPUT_DIR}/result.000000000.csv)
echo "expected 2, actual ${actual}"
[ "$actual" = 2 ]

# Test for dump with sequence
run_dumpling | tee ${DUMPLING_OUTPUT_DIR}/dumpling.log
actual=$(grep -w "dump failed" ${DUMPLING_OUTPUT_DIR}/dumpling.log|wc -l)
echo "expected 0, actual ${actual}"
[ "$actual" = 0 ]

# Test for tidb_mem_quota_query configuration
export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/PrintTiDBMemQuotaQuery=1*return"
run_dumpling | tee ${DUMPLING_OUTPUT_DIR}/dumpling.log
Expand Down