Skip to content

Commit

Permalink
cherry pick pingcap#30164 to release-5.3
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
sylzd authored and ti-srebot committed Dec 20, 2021
1 parent 83b273a commit 60db6c5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
15 changes: 14 additions & 1 deletion dumpling/export/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,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 @@ -112,13 +112,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 @@ -127,6 +132,8 @@ func (t TableType) String() string {
return TableTypeBaseStr
case TableTypeView:
return TableTypeViewStr
case TableTypeSequence:
return TableTypeSequenceStr
default:
return "UNKNOWN"
}
Expand All @@ -139,6 +146,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 @@ -691,6 +691,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
54 changes: 54 additions & 0 deletions dumpling/export/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,60 @@ func TestPickupPossibleField(t *testing.T) {
}
}

<<<<<<< HEAD
=======
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())
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()
require.NoError(t, err)
defer func() {
require.NoError(t, db.Close())
}()
ctx := context.Background()
conn, err := db.Conn(ctx)
require.NoError(t, err)

mock.ExpectQuery("SHOW CHARACTER SET").
WillReturnRows(sqlmock.NewRows([]string{"Charset", "Description", "Default collation", "Maxlen"}).
AddRow("utf8mb4", "UTF-8 Unicode", "utf8mb4_0900_ai_ci", 4).
AddRow("latin1", "cp1252 West European", "latin1_swedish_ci", 1))

charsetAndDefaultCollation, err := GetCharsetAndDefaultCollation(ctx, conn)
require.NoError(t, err)
require.Equal(t, "utf8mb4_0900_ai_ci", charsetAndDefaultCollation["utf8mb4"])
require.Equal(t, "latin1_swedish_ci", charsetAndDefaultCollation["latin1"])
}

>>>>>>> 9d307b00a... dumpling: fix dump failed when sequence exists (#30164)
func makeVersion(major, minor, patch int64, preRelease string) *semver.Version {
return &semver.Version{
Major: major,
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 > ${DUMPLING_OUTPUT_DIR}/dumpling.log
Expand Down

0 comments on commit 60db6c5

Please sign in to comment.