Skip to content

Commit

Permalink
deal comments
Browse files Browse the repository at this point in the history
Signed-off-by: joccau <[email protected]>
  • Loading branch information
joccau committed Sep 11, 2024
1 parent 9d356d4 commit 266da27
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 78 deletions.
14 changes: 6 additions & 8 deletions pkg/ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,12 @@ func TestBuildJobDependence(t *testing.T) {
model.JobVersion1,
model.JobVersion2,
}

for _, ver := range vers {
model.SetJobVerInUse(ver)
testBuildJobDependence(t)
testBuildJobDependence(t, ver)
}
}

func testBuildJobDependence(t *testing.T) {
func testBuildJobDependence(t *testing.T, jobVer model.JobVersion) {
store := createMockStore(t)
defer func() {
require.NoError(t, store.Close())
Expand All @@ -226,11 +224,11 @@ func testBuildJobDependence(t *testing.T) {
job6 := &model.Job{ID: 6, TableID: 1, Type: model.ActionDropTable}
job7 := &model.Job{ID: 7, TableID: 2, Type: model.ActionModifyColumn}
job9 := &model.Job{ID: 9, SchemaID: 111, Type: model.ActionDropSchema}
job11 := &model.Job{ID: 11, TableID: 2, Type: model.ActionRenameTable, Version: model.GetJobVerInUse()}
job11 := &model.Job{ID: 11, TableID: 2, Type: model.ActionRenameTable, Version: jobVer}
job11.FillArgs(&model.RenameTableArgs{
OldSchemaID: 111,
NewTableName: pmodel.NewCIStr("new_table_name"),
SchemaName: pmodel.NewCIStr("old_db_name"),
OldSchemaID: 111,
NewTableName: pmodel.NewCIStr("new_table_name"),
OldSchemaName: pmodel.NewCIStr("old_db_name"),
})

err := kv.RunInNewTxn(ctx, store, false, func(ctx context.Context, txn kv.Transaction) error {
Expand Down
20 changes: 19 additions & 1 deletion pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4283,7 +4283,11 @@ func (e *executor) renameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Id
SQLMode: ctx.GetSessionVars().SQLMode,
}

args := model.NewRenameTableArgs(schemas[0].ID, schemas[1].ID, schemas[0].Name, tableID, newIdent.Name)
args := &model.RenameTableArgs{
OldSchemaID: schemas[0].ID,
OldSchemaName: schemas[0].Name,
NewTableName: newIdent.Name,
}
job.FillArgs(args)

err = e.DoDDLJob(ctx, job)
Expand Down Expand Up @@ -6445,6 +6449,20 @@ func getTruncateTableNewTableID(job *model.Job) int64 {
return job.Args[0].(*model.TruncateTableArgs).NewTableID
}

func getRenameTableUniqueIDs(job *model.Job, schema bool) []int64 {
if !schema {
return []int64{job.TableID}
}

args := &model.RenameTableArgs{}
if job.Version == model.JobVersion1 {
args.OldSchemaID = job.Args[0].(int64)
} else {
args = job.Args[0].(*model.RenameTableArgs)
}
return []int64{args.OldSchemaID, job.SchemaID}
}

// HandleLockTablesOnSuccessSubmit handles the table lock for the job which is submitted
// successfully. exported for testing purpose.
func HandleLockTablesOnSuccessSubmit(ctx sessionctx.Context, jobW *JobWrapper) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/job_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func job2UniqueIDs(job *model.Job, schema bool) string {
case model.ActionExchangeTablePartition, model.ActionRenameTables, model.ActionRenameTable:
var ids []int64
if job.Type == model.ActionRenameTable {
ids = model.GetRenameTableUniqueIDs(job, schema)
ids = getRenameTableUniqueIDs(job, schema)
} else {
if schema {
ids = job.CtxVars[0].([]int64)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,13 +764,13 @@ func onRenameTable(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver int64,
return ver, errors.Trace(err)
}
oldTableName := tblInfo.Name
ver, err = checkAndRenameTables(t, job, tblInfo, args.OldSchemaID, job.SchemaID, &args.SchemaName, &args.NewTableName)
ver, err = checkAndRenameTables(t, job, tblInfo, args.OldSchemaID, job.SchemaID, &args.OldSchemaName, &args.NewTableName)
if err != nil {
return ver, errors.Trace(err)
}
fkh := newForeignKeyHelper()
err = adjustForeignKeyChildTableInfoAfterRenameTable(jobCtx.infoCache, t,
job, &fkh, tblInfo, args.SchemaName, oldTableName, args.NewTableName, newSchemaID)
job, &fkh, tblInfo, args.OldSchemaName, oldTableName, args.NewTableName, newSchemaID)
if err != nil {
return ver, errors.Trace(err)
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ func testRenameTable(
},
}
args := model.RenameTableArgs{
OldSchemaID: oldSchemaID,
SchemaName: oldSchemaName,
NewTableName: tblInfo.Name,
SchemaIDs: []int64{oldSchemaID, newSchemaID},
TableIDs: []int64{tblInfo.ID},
OldSchemaID: oldSchemaID,
OldSchemaName: oldSchemaName,
NewTableName: tblInfo.Name,
}
job.FillArgs(&args)

Expand Down
80 changes: 19 additions & 61 deletions pkg/meta/model/job_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,73 +259,48 @@ func getTruncateTableArgs(job *Job, argsOfFinished bool) (*TruncateTableArgs, er
return getOrDecodeArgsV2[*TruncateTableArgs](job)
}

// RenameTableArgs is the arguements for rename table DDL job.
// RenameTableArgs is the arguments for rename table DDL job.
type RenameTableArgs struct {
// for Args
OldSchemaID int64 `json:"old_schema_id,omitempty"`
SchemaName pmodel.CIStr `json:"schema_name,omitempty"`
NewTableName pmodel.CIStr `json:"new_table_name,omitempty"`

// for CtxVars
// SchemaIDs contains {oldSchemaID, newSchemaID}
SchemaIDs []int64 `json:"-"`
// TableIDs contains {tableID}
TableIDs []int64 `json:"-"`
OldSchemaID int64 `json:"old_schema_id,omitempty"`
OldSchemaName pmodel.CIStr `json:"schema_name,omitempty"`
NewTableName pmodel.CIStr `json:"new_table_name,omitempty"`
}

func (rt *RenameTableArgs) fillJob(job *Job) {
if job.Version == JobVersion1 {
job.Args = []any{rt.OldSchemaID, rt.NewTableName, rt.SchemaName}
job.CtxVars = []any{rt.SchemaIDs, rt.TableIDs}
job.Args = []any{rt.OldSchemaID, rt.NewTableName, rt.OldSchemaName}
} else {
job.Args = []any{rt}
}
}

// NewRenameTableArgs creates a struct with given parameters.
func NewRenameTableArgs(
oldSchemaID int64, newSchemaID int64, schemaName pmodel.CIStr,
tableID int64, newTableName pmodel.CIStr,
) *RenameTableArgs {
return &RenameTableArgs{
OldSchemaID: oldSchemaID,
SchemaName: schemaName,
NewTableName: newTableName,
SchemaIDs: []int64{oldSchemaID, newSchemaID},
TableIDs: []int64{tableID},
}
}

// GetRenameTableArgs get the arguements from job.
// GetRenameTableArgs get the arguments from job.
func GetRenameTableArgs(job *Job) (*RenameTableArgs, error) {
if job.Version == JobVersion1 {
var (
oldSchemaID int64
schemaName pmodel.CIStr
newTableName pmodel.CIStr
)
var (
oldSchemaID int64
oldSchemaName pmodel.CIStr
newTableName pmodel.CIStr
)

if job.Version == JobVersion1 {
// decode args and cache in args.
if len(job.Args) == 0 {
err := job.DecodeArgs(&oldSchemaID, &newTableName, &schemaName)
err := job.DecodeArgs(&oldSchemaID, &newTableName, &oldSchemaName)
if err != nil {
return nil, errors.Trace(err)
}
job.Args = []any{oldSchemaID, newTableName, schemaName}
job.Args = []any{oldSchemaID, newTableName, oldSchemaName}
} else {
oldSchemaID = job.Args[0].(int64)
schemaName = job.Args[1].(pmodel.CIStr)
newTableName = job.Args[2].(pmodel.CIStr)
newTableName = job.Args[1].(pmodel.CIStr)
oldSchemaName = job.Args[2].(pmodel.CIStr)
}

args := RenameTableArgs{
OldSchemaID: oldSchemaID,
SchemaName: schemaName,
NewTableName: newTableName,
}
if len(job.CtxVars) > 0 {
args.SchemaIDs = job.CtxVars[0].([]int64)
args.TableIDs = job.CtxVars[1].([]int64)
OldSchemaID: oldSchemaID,
OldSchemaName: oldSchemaName,
NewTableName: newTableName,
}
return &args, nil
}
Expand All @@ -336,20 +311,3 @@ func GetRenameTableArgs(job *Job) (*RenameTableArgs, error) {
}
return argsV2, err
}

// GetRenameTableUniqueIDs gets unique IDs from job for rename table type.
func GetRenameTableUniqueIDs(job *Job, schema bool) []int64 {
args := &RenameTableArgs{}

if job.Version == JobVersion1 {
args.SchemaIDs = job.CtxVars[0].([]int64)
args.TableIDs = job.CtxVars[1].([]int64)
} else {
args = job.Args[0].(*RenameTableArgs)
}

if schema {
return args.SchemaIDs
}
return args.TableIDs
}

0 comments on commit 266da27

Please sign in to comment.