Skip to content

Commit

Permalink
ddl: cancel create view job , when failed to drop old view (#40353) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Aug 3, 2023
1 parent 87b7923 commit 23654df
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func onCreateView(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error)
if oldTbInfoID > 0 && orReplace {
err = t.DropTableOrView(schemaID, oldTbInfoID)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
err = t.GetAutoIDAccessors(schemaID, oldTbInfoID).Del()
Expand Down
73 changes: 73 additions & 0 deletions ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,79 @@ func TestTable(t *testing.T) {
testDropSchema(t, testkit.NewTestKit(t, store).Session(), d, dbInfo)
}

func TestCreateView(t *testing.T) {
store, domain := testkit.CreateMockStoreAndDomainWithSchemaLease(t, testLease)

d := domain.DDL()
dbInfo, err := testSchemaInfo(store, "test_table")
require.NoError(t, err)
testCreateSchema(t, testkit.NewTestKit(t, store).Session(), domain.DDL(), dbInfo)

ctx := testkit.NewTestKit(t, store).Session()

tblInfo, err := testTableInfo(store, "t", 3)
require.NoError(t, err)
job := testCreateTable(t, ctx, d, dbInfo, tblInfo)
testCheckTableState(t, store, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, store, job.ID, true)

// Create a view
newTblInfo0, err := testTableInfo(store, "v", 3)
require.NoError(t, err)
job = &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionCreateView,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{newTblInfo0},
}
ctx.SetValue(sessionctx.QueryString, "skip")
err = d.DoDDLJob(ctx, job)
require.NoError(t, err)

v := getSchemaVer(t, ctx)
tblInfo.State = model.StatePublic
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: newTblInfo0})
tblInfo.State = model.StateNone
testCheckTableState(t, store, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, store, job.ID, true)

// Replace a view
newTblInfo1, err := testTableInfo(store, "v", 3)
require.NoError(t, err)
job = &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionCreateView,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{newTblInfo1, true, newTblInfo0.ID},
}
ctx.SetValue(sessionctx.QueryString, "skip")
err = d.DoDDLJob(ctx, job)
require.NoError(t, err)

v = getSchemaVer(t, ctx)
tblInfo.State = model.StatePublic
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: newTblInfo1})
tblInfo.State = model.StateNone
testCheckTableState(t, store, dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(t, store, job.ID, true)

// Replace a view with a non-existing table id
newTblInfo2, err := testTableInfo(store, "v", 3)
require.NoError(t, err)
job = &model.Job{
SchemaID: dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionCreateView,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{newTblInfo2, true, newTblInfo0.ID},
}
ctx.SetValue(sessionctx.QueryString, "skip")
err = d.DoDDLJob(ctx, job)
require.Error(t, err)
}

func checkTableCacheTest(t *testing.T, store kv.Storage, dbInfo *model.DBInfo, tblInfo *model.TableInfo) {
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL)
require.NoError(t, kv.RunInNewTxn(ctx, store, false, func(ctx context.Context, txn kv.Transaction) error {
Expand Down

0 comments on commit 23654df

Please sign in to comment.