ddl: remove temporary testing methods (#32946)

ref pingcap/tidb#27180
This commit is contained in:
tison
2022-03-09 15:59:49 +08:00
committed by GitHub
parent 4ab66b8ac2
commit b7279df426
8 changed files with 139 additions and 344 deletions

View File

@ -3670,40 +3670,6 @@ func TestAddPartitionReplicaBiggerThanTiFlashStores(t *testing.T) {
require.Equal(t, "[ddl:-1]DDL job rollback, error msg: [ddl] add partition wait for tiflash replica to complete", err.Error())
}
func TestDropAndTruncatePartition(t *testing.T) {
// Useless, but is required to initialize the global infoSync
// Otherwise this test throw a "infoSyncer is not initialized" error
_, clean := testkit.CreateMockStore(t)
defer clean()
ddl.ExportTestDropAndTruncatePartition(t)
}
func TestTable(t *testing.T) {
// Useless, but is required to initialize the global infoSync
// Otherwise this test throw a "infoSyncer is not initialized" error
_, clean := testkit.CreateMockStore(t)
defer clean()
ddl.ExportTestTable(t)
}
func TestRenameTables(t *testing.T) {
// Useless, but is required to initialize the global infoSync
// Otherwise this test throw a "infoSyncer is not initialized" error
_, clean := testkit.CreateMockStore(t)
defer clean()
ddl.ExportTestRenameTables(t)
}
func TestCreateTables(t *testing.T) {
_, clean := testkit.CreateMockStore(t)
defer clean()
ddl.ExportTestRenameTables(t)
}
func TestDuplicatePartitionNames(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

View File

@ -105,16 +105,6 @@ func checkEqualTable(t *testing.T, t1, t2 *model.TableInfo) {
require.Equal(t, t1.AutoIncID, t2.AutoIncID)
}
func checkEqualTableT(t *testing.T, t1, t2 *model.TableInfo) {
require.Equal(t, t1.ID, t2.ID)
require.Equal(t, t1.Name, t2.Name)
require.Equal(t, t1.Charset, t2.Charset)
require.Equal(t, t1.Collate, t2.Collate)
require.EqualValues(t, t1.PKIsHandle, t2.PKIsHandle)
require.EqualValues(t, t1.Comment, t2.Comment)
require.EqualValues(t, t1.AutoIncID, t2.AutoIncID)
}
func checkHistoryJob(t *testing.T, job *model.Job) {
require.Equal(t, job.State, model.JobStateSynced)
}
@ -142,29 +132,6 @@ func checkHistoryJobArgs(t *testing.T, ctx sessionctx.Context, id int64, args *h
}
}
func checkHistoryJobArgsT(t *testing.T, ctx sessionctx.Context, id int64, args *historyJobArgs) {
txn, err := ctx.Txn(true)
require.NoError(t, err)
tt := meta.NewMeta(txn)
historyJob, err := tt.GetHistoryDDLJob(id)
require.NoError(t, err)
require.Greater(t, historyJob.BinlogInfo.FinishedTS, uint64(0))
if args.tbl != nil {
require.Equal(t, args.ver, historyJob.BinlogInfo.SchemaVersion)
checkEqualTableT(t, historyJob.BinlogInfo.TableInfo, args.tbl)
return
}
// for handling schema job
require.Equal(t, args.ver, historyJob.BinlogInfo.SchemaVersion)
require.EqualValues(t, args.db, historyJob.BinlogInfo.DBInfo)
// only for creating schema job
if args.db != nil && len(args.tblIDs) == 0 {
return
}
}
func buildCreateIdxJob(dbInfo *model.DBInfo, tblInfo *model.TableInfo, unique bool, indexName string, colName string) *model.Job {
return &model.Job{
SchemaID: dbInfo.ID,

View File

@ -500,66 +500,33 @@ func testCheckOwner(t *testing.T, d *ddl, expectedVal bool) {
require.Equal(t, d.isOwner(), expectedVal)
}
func testCheckJobDone(test *testing.T, d *ddl, job *model.Job, isAdd bool) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
historyJob, err := t.GetHistoryDDLJob(job.ID)
require.NoError(test, err)
checkHistoryJob(test, historyJob)
if isAdd {
require.Equal(test, historyJob.SchemaState, model.StatePublic)
} else {
require.Equal(test, historyJob.SchemaState, model.StateNone)
}
return nil
})
require.NoError(test, err)
}
func testCheckJobDoneT(t *testing.T, d *ddl, job *model.Job, isAdd bool) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
historyJob, err := tt.GetHistoryDDLJob(job.ID)
func testCheckJobDone(t *testing.T, d *ddl, job *model.Job, isAdd bool) {
require.NoError(t, kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
historyJob, err := m.GetHistoryDDLJob(job.ID)
require.NoError(t, err)
require.Equal(t, model.JobStateSynced, historyJob.State)
checkHistoryJob(t, historyJob)
if isAdd {
require.Equal(t, model.StatePublic, historyJob.SchemaState)
require.Equal(t, historyJob.SchemaState, model.StatePublic)
} else {
require.Equal(t, model.StateNone, historyJob.SchemaState)
require.Equal(t, historyJob.SchemaState, model.StateNone)
}
return nil
})
require.NoError(t, err)
}))
}
func testCheckJobCancelledT(t *testing.T, d *ddl, job *model.Job, state *model.SchemaState) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
historyJob, err := tt.GetHistoryDDLJob(job.ID)
func testCheckJobCancelled(t *testing.T, d *ddl, job *model.Job, state *model.SchemaState) {
require.NoError(t, kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
historyJob, err := m.GetHistoryDDLJob(job.ID)
require.NoError(t, err)
require.True(t, historyJob.IsCancelled() || historyJob.IsRollbackDone(), "history job %s", historyJob)
if state != nil {
require.Equal(t, *state, historyJob.SchemaState)
require.Equal(t, historyJob.SchemaState, *state)
}
return nil
})
require.NoError(t, err)
}
func testCheckJobCancelled(test *testing.T, d *ddl, job *model.Job, state *model.SchemaState) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
historyJob, err := t.GetHistoryDDLJob(job.ID)
require.NoError(test, err)
require.True(test, historyJob.IsCancelled() || historyJob.IsRollbackDone(), "history job %s", historyJob)
if state != nil {
require.Equal(test, historyJob.SchemaState, *state)
}
return nil
})
require.NoError(test, err)
}))
}
func doDDLJobErrWithSchemaState(ctx sessionctx.Context, d *ddl, t *testing.T, schemaID, tableID int64, tp model.ActionType,
@ -571,31 +538,13 @@ func doDDLJobErrWithSchemaState(ctx sessionctx.Context, d *ddl, t *testing.T, sc
Args: args,
BinlogInfo: &model.HistoryInfo{},
}
err := d.doDDLJob(ctx, job)
// TODO: Add the detail error check.
require.Error(t, err, "err:%v", err)
// TODO: check error detail
require.Error(t, d.doDDLJob(ctx, job))
testCheckJobCancelled(t, d, job, state)
return job
}
func doDDLJobErrWithSchemaStateT(ctx sessionctx.Context, d *ddl, t *testing.T, schemaID, tableID int64, tp model.ActionType,
args []interface{}, state *model.SchemaState) *model.Job {
job := &model.Job{
SchemaID: schemaID,
TableID: tableID,
Type: tp,
Args: args,
BinlogInfo: &model.HistoryInfo{},
}
err := d.doDDLJob(ctx, job)
// TODO: Add the detail error check.
require.Error(t, err, "err:%v", err)
testCheckJobCancelledT(t, d, job, state)
return job
}
func doDDLJobSuccess(ctx sessionctx.Context, d *ddl, t *testing.T, schemaID, tableID int64, tp model.ActionType,
args []interface{}) {
job := &model.Job{
@ -609,16 +558,10 @@ func doDDLJobSuccess(ctx sessionctx.Context, d *ddl, t *testing.T, schemaID, tab
require.NoError(t, err)
}
func doDDLJobErr(t *testing.T, schemaID, tableID int64, tp model.ActionType, args []interface{},
ctx sessionctx.Context, d *ddl) *model.Job {
func doDDLJobErr(t *testing.T, schemaID, tableID int64, tp model.ActionType, args []interface{}, ctx sessionctx.Context, d *ddl) *model.Job {
return doDDLJobErrWithSchemaState(ctx, d, t, schemaID, tableID, tp, args, nil)
}
func doDDLJobErrT(t *testing.T, schemaID, tableID int64, tp model.ActionType, args []interface{},
ctx sessionctx.Context, d *ddl) *model.Job {
return doDDLJobErrWithSchemaStateT(ctx, d, t, schemaID, tableID, tp, args, nil)
}
func checkCancelState(txn kv.Transaction, job *model.Job, test *testCancelJob) error {
var checkErr error
addIndexFirstReorg := (test.act == model.ActionAddIndex || test.act == model.ActionAddPrimaryKey) &&

View File

@ -76,7 +76,7 @@ func testDropForeignKey(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *mo
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}
@ -114,14 +114,14 @@ func TestForeignKey(t *testing.T) {
dbInfo, err := testSchemaInfo(d, "test_foreign")
require.NoError(t, err)
ctx := testNewContext(d)
testCreateSchemaT(t, ctx, d, dbInfo)
testCreateSchema(t, ctx, d, dbInfo)
tblInfo, err := testTableInfo(d, "t", 3)
require.NoError(t, err)
err = ctx.NewTxn(context.Background())
require.NoError(t, err)
testCreateTableT(t, ctx, d, dbInfo, tblInfo)
testCreateTable(t, ctx, d, dbInfo, tblInfo)
txn, err := ctx.Txn(true)
require.NoError(t, err)
@ -157,7 +157,7 @@ func TestForeignKey(t *testing.T) {
d.SetHook(tc)
job := testCreateForeignKey(t, d, ctx, dbInfo, tblInfo, "c1_fk", []string{"c1"}, "t2", []string{"c1"}, ast.ReferOptionCascade, ast.ReferOptionSetNull)
testCheckJobDoneT(t, d, job, true)
testCheckJobDone(t, d, job, true)
txn, err = ctx.Txn(true)
require.NoError(t, err)
err = txn.Commit(context.Background())
@ -169,7 +169,7 @@ func TestForeignKey(t *testing.T) {
require.NoError(t, hErr)
require.True(t, ok)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
mu.Lock()
checkOK = false
@ -198,7 +198,7 @@ func TestForeignKey(t *testing.T) {
d.SetHook(tc2)
job = testDropForeignKey(t, ctx, d, dbInfo, tblInfo, "c1_fk")
testCheckJobDoneT(t, d, job, false)
testCheckJobDone(t, d, job, false)
mu.Lock()
hErr = hookErr
ok = checkOK
@ -209,8 +209,8 @@ func TestForeignKey(t *testing.T) {
err = ctx.NewTxn(context.Background())
require.NoError(t, err)
job = testDropTableT(t, ctx, d, dbInfo, tblInfo)
testCheckJobDoneT(t, d, job, false)
job = testDropTable(t, ctx, d, dbInfo, tblInfo)
testCheckJobDone(t, d, job, false)
txn, err = ctx.Txn(true)
require.NoError(t, err)

View File

@ -21,15 +21,16 @@ import (
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)
func ExportTestDropAndTruncatePartition(t *testing.T) {
store := createMockStore(t)
func TestDropAndTruncatePartition(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
defer func() {
err := store.Close()
require.NoError(t, err)
require.NoError(t, store.Close())
}()
d, err := testNewDDLAndStart(
context.Background(),
@ -38,19 +39,16 @@ func ExportTestDropAndTruncatePartition(t *testing.T) {
)
require.NoError(t, err)
defer func() {
err := d.Stop()
require.NoError(t, err)
require.NoError(t, d.Stop())
}()
dbInfo, err := testSchemaInfo(d, "test_partition")
require.NoError(t, err)
testCreateSchemaT(t, testNewContext(d), d, dbInfo)
testCreateSchema(t, testNewContext(d), d, dbInfo)
// generate 5 partition in tableInfo.
tblInfo, partIDs := buildTableInfoWithPartition(t, d)
ctx := testNewContext(d)
testCreateTableT(t, ctx, d, dbInfo, tblInfo)
testCreateTable(t, ctx, d, dbInfo, tblInfo)
testDropPartition(t, ctx, d, dbInfo, tblInfo, []string{"p0", "p1"})
testTruncatePartition(t, ctx, d, dbInfo, tblInfo, []int64{partIDs[3], partIDs[4]})
}
@ -125,7 +123,7 @@ func testDropPartition(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *mod
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}
@ -144,6 +142,6 @@ func testTruncatePartition(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}

View File

@ -49,8 +49,7 @@ func testCreateSchema(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *mode
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{dbInfo},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
require.NoError(t, d.doDDLJob(ctx, job))
v := getSchemaVer(t, ctx)
dbInfo.State = model.StatePublic
@ -59,23 +58,6 @@ func testCreateSchema(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *mode
return job
}
func testCreateSchemaT(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo) *model.Job {
job := &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionCreateSchema,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{dbInfo},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
v := getSchemaVer(t, ctx)
dbInfo.State = model.StatePublic
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, db: dbInfo})
dbInfo.State = model.StateNone
return job
}
func buildDropSchemaJob(dbInfo *model.DBInfo) *model.Job {
return &model.Job{
SchemaID: dbInfo.ID,

View File

@ -31,25 +31,14 @@ import (
"github.com/stretchr/testify/require"
)
func testCreateTableT(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo) *model.Job {
job := &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionCreateTable,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{tblInfo},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
v := getSchemaVer(t, ctx)
tblInfo.State = model.StatePublic
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
tblInfo.State = model.StateNone
return job
}
func testRenameTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSchemaID, oldSchemaID int64, oldSchemaName model.CIStr, tblInfo *model.TableInfo) *model.Job {
func testRenameTable(
t *testing.T,
ctx sessionctx.Context,
d *ddl,
newSchemaID, oldSchemaID int64,
oldSchemaName model.CIStr,
tblInfo *model.TableInfo,
) *model.Job {
job := &model.Job{
SchemaID: newSchemaID,
TableID: tblInfo.ID,
@ -57,12 +46,11 @@ func testRenameTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSchemaID,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{oldSchemaID, tblInfo.Name, oldSchemaName},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
require.NoError(t, d.doDDLJob(ctx, job))
v := getSchemaVer(t, ctx)
tblInfo.State = model.StatePublic
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
tblInfo.State = model.StateNone
return job
}
@ -77,11 +65,10 @@ func testRenameTables(
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{oldSchemaIDs, newSchemaIDs, newTableNames, oldTableIDs, oldSchemaNames},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
require.NoError(t, d.doDDLJob(ctx, job))
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: nil})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: nil})
return job
}
@ -104,7 +91,7 @@ func testLockTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSchemaID int
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v})
return job
}
@ -127,21 +114,6 @@ func checkTableLockedTest(t *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *m
require.NoError(t, err)
}
func testDropTableT(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo) *model.Job {
job := &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionDropTable,
BinlogInfo: &model.HistoryInfo{},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}
func testTruncateTable(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo) *model.Job {
genIDs, err := d.genGlobalIDs(1)
require.NoError(t, err)
@ -158,34 +130,10 @@ func testTruncateTable(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *mod
v := getSchemaVer(t, ctx)
tblInfo.ID = newTableID
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}
func testCheckTableStateT(t *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, state model.SchemaState) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
info, err := tt.GetTable(dbInfo.ID, tblInfo.ID)
require.NoError(t, err)
if state == model.StateNone {
require.Nil(t, info)
return nil
}
require.EqualValues(t, tblInfo.Name, info.Name)
require.Equal(t, state, info.State)
return nil
})
require.NoError(t, err)
}
func testGetTableT(t *testing.T, d *ddl, schemaID int64, tableID int64) table.Table {
tbl, err := testGetTableWithError(d, schemaID, tableID)
require.NoError(t, err)
return tbl
}
func testGetTableWithError(d *ddl, schemaID, tableID int64) (table.Table, error) {
var tblInfo *model.TableInfo
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
@ -211,85 +159,87 @@ func testGetTableWithError(d *ddl, schemaID, tableID int64) (table.Table, error)
return tbl, nil
}
func ExportTestTable(t *testing.T) {
func TestTable(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
ddl, err := testNewDDLAndStart(
defer func() {
require.NoError(t, store.Close())
}()
d, err := testNewDDLAndStart(
context.Background(),
WithStore(store),
WithLease(testLease),
)
require.NoError(t, err)
defer func() {
require.NoError(t, d.Stop())
}()
dbInfo, err := testSchemaInfo(ddl, "test_table")
dbInfo, err := testSchemaInfo(d, "test_table")
require.NoError(t, err)
testCreateSchemaT(t, testNewContext(ddl), ddl, dbInfo)
testCreateSchema(t, testNewContext(d), d, dbInfo)
ctx := testNewContext(ddl)
ctx := testNewContext(d)
tblInfo, err := testTableInfo(ddl, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
require.NoError(t, err)
job := testCreateTableT(t, ctx, ddl, dbInfo, tblInfo)
testCheckTableStateT(t, ddl, dbInfo, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
job := testCreateTable(t, ctx, d, dbInfo, tblInfo)
testCheckTableState(t, d, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
// Create an existing table.
newTblInfo, err := testTableInfo(ddl, "t", 3)
newTblInfo, err := testTableInfo(d, "t", 3)
require.NoError(t, err)
doDDLJobErrT(t, dbInfo.ID, newTblInfo.ID, model.ActionCreateTable, []interface{}{newTblInfo}, ctx, ddl)
doDDLJobErr(t, dbInfo.ID, newTblInfo.ID, model.ActionCreateTable, []interface{}{newTblInfo}, ctx, d)
count := 2000
tbl := testGetTableT(t, ddl, dbInfo.ID, tblInfo.ID)
tbl := testGetTable(t, d, dbInfo.ID, tblInfo.ID)
for i := 1; i <= count; i++ {
_, err := tbl.AddRecord(ctx, types.MakeDatums(i, i, i))
require.NoError(t, err)
}
job = testDropTableT(t, ctx, ddl, dbInfo, tblInfo)
testCheckJobDoneT(t, ddl, job, false)
job = testDropTable(t, ctx, d, dbInfo, tblInfo)
testCheckJobDone(t, d, job, false)
// for truncate table
tblInfo, err = testTableInfo(ddl, "tt", 3)
tblInfo, err = testTableInfo(d, "tt", 3)
require.NoError(t, err)
job = testCreateTableT(t, ctx, ddl, dbInfo, tblInfo)
testCheckTableStateT(t, ddl, dbInfo, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
job = testTruncateTable(t, ctx, ddl, dbInfo, tblInfo)
testCheckTableStateT(t, ddl, dbInfo, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
job = testCreateTable(t, ctx, d, dbInfo, tblInfo)
testCheckTableState(t, d, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
job = testTruncateTable(t, ctx, d, dbInfo, tblInfo)
testCheckTableState(t, d, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
// for rename table
dbInfo1, err := testSchemaInfo(ddl, "test_rename_table")
dbInfo1, err := testSchemaInfo(d, "test_rename_table")
require.NoError(t, err)
testCreateSchemaT(t, testNewContext(ddl), ddl, dbInfo1)
job = testRenameTable(t, ctx, ddl, dbInfo1.ID, dbInfo.ID, dbInfo.Name, tblInfo)
testCheckTableStateT(t, ddl, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
testCreateSchema(t, testNewContext(d), d, dbInfo1)
job = testRenameTable(t, ctx, d, dbInfo1.ID, dbInfo.ID, dbInfo.Name, tblInfo)
testCheckTableState(t, d, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
job = testLockTable(t, ctx, ddl, dbInfo1.ID, tblInfo, model.TableLockWrite)
testCheckTableStateT(t, ddl, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
checkTableLockedTest(t, ddl, dbInfo1, tblInfo, ddl.GetID(), ctx.GetSessionVars().ConnectionID, model.TableLockWrite)
job = testLockTable(t, ctx, d, dbInfo1.ID, tblInfo, model.TableLockWrite)
testCheckTableState(t, d, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
checkTableLockedTest(t, d, dbInfo1, tblInfo, d.GetID(), ctx.GetSessionVars().ConnectionID, model.TableLockWrite)
// for alter cache table
job = testAlterCacheTable(t, ctx, ddl, dbInfo1.ID, tblInfo)
testCheckTableStateT(t, ddl, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
checkTableCacheTest(t, ddl, dbInfo1, tblInfo)
job = testAlterCacheTable(t, ctx, d, dbInfo1.ID, tblInfo)
testCheckTableState(t, d, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
checkTableCacheTest(t, d, dbInfo1, tblInfo)
// for alter no cache table
job = testAlterNoCacheTable(t, ctx, ddl, dbInfo1.ID, tblInfo)
testCheckTableStateT(t, ddl, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
checkTableNoCacheTest(t, ddl, dbInfo1, tblInfo)
job = testAlterNoCacheTable(t, ctx, d, dbInfo1.ID, tblInfo)
testCheckTableState(t, d, dbInfo1, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
checkTableNoCacheTest(t, d, dbInfo1, tblInfo)
testDropSchema(t, testNewContext(ddl), ddl, dbInfo)
err = ddl.Stop()
require.NoError(t, err)
err = store.Close()
require.NoError(t, err)
testDropSchema(t, testNewContext(d), d, dbInfo)
}
func checkTableCacheTest(t *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
require.NoError(t, kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
info, err := tt.GetTable(dbInfo.ID, tblInfo.ID)
require.NoError(t, err)
@ -297,20 +247,18 @@ func checkTableCacheTest(t *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *mo
require.NotNil(t, info.TableCacheStatusType)
require.Equal(t, model.TableCacheStatusEnable, info.TableCacheStatusType)
return nil
})
require.NoError(t, err)
}))
}
func checkTableNoCacheTest(t *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
require.NoError(t, kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
info, err := tt.GetTable(dbInfo.ID, tblInfo.ID)
require.NoError(t, err)
require.NotNil(t, info)
require.Equal(t, model.TableCacheStatusDisable, info.TableCacheStatusType)
return nil
})
require.NoError(t, err)
}))
}
func testAlterCacheTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSchemaID int64, tblInfo *model.TableInfo) *model.Job {
@ -325,12 +273,11 @@ func testAlterCacheTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSchema
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v})
return job
}
func testAlterNoCacheTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSchemaID int64, tblInfo *model.TableInfo) *model.Job {
job := &model.Job{
SchemaID: newSchemaID,
TableID: tblInfo.ID,
@ -338,56 +285,53 @@ func testAlterNoCacheTable(t *testing.T, ctx sessionctx.Context, d *ddl, newSche
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
require.NoError(t, d.doDDLJob(ctx, job))
v := getSchemaVer(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v})
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v})
return job
}
func ExportTestRenameTables(t *testing.T) {
func TestRenameTables(t *testing.T) {
store, err := mockstore.NewMockStore()
defer func() {
err := store.Close()
require.NoError(t, err)
require.NoError(t, store.Close())
}()
require.NoError(t, err)
ddl, err := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
WithStore(store),
WithLease(testLease),
)
require.NoError(t, err)
defer func() {
err := ddl.Stop()
require.NoError(t, err)
require.NoError(t, d.Stop())
}()
dbInfo, err := testSchemaInfo(ddl, "test_table")
dbInfo, err := testSchemaInfo(d, "test_table")
require.NoError(t, err)
testCreateSchemaT(t, testNewContext(ddl), ddl, dbInfo)
testCreateSchema(t, testNewContext(d), d, dbInfo)
ctx := testNewContext(ddl)
ctx := testNewContext(d)
var tblInfos = make([]*model.TableInfo, 0, 2)
var newTblInfos = make([]*model.TableInfo, 0, 2)
for i := 1; i < 3; i++ {
tableName := fmt.Sprintf("t%d", i)
tblInfo, err := testTableInfo(ddl, tableName, 3)
tblInfo, err := testTableInfo(d, tableName, 3)
require.NoError(t, err)
job := testCreateTableT(t, ctx, ddl, dbInfo, tblInfo)
testCheckTableStateT(t, ddl, dbInfo, tblInfo, model.StatePublic)
testCheckJobDoneT(t, ddl, job, true)
job := testCreateTable(t, ctx, d, dbInfo, tblInfo)
testCheckTableState(t, d, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, d, job, true)
tblInfos = append(tblInfos, tblInfo)
newTableName := fmt.Sprintf("tt%d", i)
tblInfo, err = testTableInfo(ddl, newTableName, 3)
tblInfo, err = testTableInfo(d, newTableName, 3)
require.NoError(t, err)
newTblInfos = append(newTblInfos, tblInfo)
}
job := testRenameTables(
t, ctx, ddl,
t, ctx, d,
[]int64{dbInfo.ID, dbInfo.ID},
[]int64{dbInfo.ID, dbInfo.ID},
[]*model.CIStr{&newTblInfos[0].Name, &newTblInfos[1].Name},
@ -402,32 +346,30 @@ func ExportTestRenameTables(t *testing.T) {
require.Equal(t, wantTblInfos[1].Name.L, "tt2")
}
func ExportTestCreateTables(t *testing.T) {
func TestCreateTables(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
defer func() {
err := store.Close()
require.NoError(t, err)
require.NoError(t, store.Close())
}()
ddl, err := testNewDDLAndStart(
d, err := testNewDDLAndStart(
context.Background(),
WithStore(store),
WithLease(testLease),
)
require.NoError(t, err)
defer func() {
err := ddl.Stop()
require.NoError(t, err)
require.NoError(t, d.Stop())
}()
dbInfo, err := testSchemaInfo(ddl, "test_table")
dbInfo, err := testSchemaInfo(d, "test_table")
require.NoError(t, err)
testCreateSchemaT(t, testNewContext(ddl), ddl, dbInfo)
testCreateSchema(t, testNewContext(d), d, dbInfo)
ctx := testNewContext(ddl)
ctx := testNewContext(d)
infos := []*model.TableInfo{}
genIDs, err := ddl.genGlobalIDs(3)
var infos []*model.TableInfo
genIDs, err := d.genGlobalIDs(3)
require.NoError(t, err)
infos = append(infos, &model.TableInfo{
@ -449,10 +391,10 @@ func ExportTestCreateTables(t *testing.T) {
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{infos},
}
err = ddl.doDDLJob(ctx, job)
err = d.doDDLJob(ctx, job)
require.NoError(t, err)
testGetTableT(t, ddl, dbInfo.ID, genIDs[0])
testGetTableT(t, ddl, dbInfo.ID, genIDs[1])
testGetTableT(t, ddl, dbInfo.ID, genIDs[2])
testGetTable(t, d, dbInfo.ID, genIDs[0])
testGetTable(t, d, dbInfo.ID, genIDs[1])
testGetTable(t, d, dbInfo.ID, genIDs[2])
}

View File

@ -189,8 +189,7 @@ func testCreateView(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.
}
require.True(t, tblInfo.IsView())
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
require.NoError(t, d.doDDLJob(ctx, job))
v := getSchemaVer(t, ctx)
tblInfo.State = model.StatePublic
@ -206,30 +205,28 @@ func testDropTable(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.D
Type: model.ActionDropTable,
BinlogInfo: &model.HistoryInfo{},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
require.NoError(t, d.doDDLJob(ctx, job))
v := getSchemaVer(t, ctx)
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}
func testCheckTableState(test *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, state model.SchemaState) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
info, err := t.GetTable(dbInfo.ID, tblInfo.ID)
require.NoError(test, err)
func testCheckTableState(t *testing.T, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, state model.SchemaState) {
require.NoError(t, kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
m := meta.NewMeta(txn)
info, err := m.GetTable(dbInfo.ID, tblInfo.ID)
require.NoError(t, err)
if state == model.StateNone {
require.NoError(test, err)
require.NoError(t, err)
return nil
}
require.Equal(test, info.Name, tblInfo.Name)
require.Equal(test, info.State, state)
require.Equal(t, info.Name, tblInfo.Name)
require.Equal(t, info.State, state)
return nil
})
require.NoError(test, err)
}))
}
func testGetTable(t *testing.T, d *ddl, schemaID int64, tableID int64) table.Table {