Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
Signed-off-by: joccau <[email protected]>
  • Loading branch information
joccau committed Sep 17, 2024
1 parent 5da35dc commit 5a61960
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
9 changes: 5 additions & 4 deletions pkg/ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,30 +742,31 @@ func onRenameTable(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver int64,
return ver, errors.Trace(err)
}

oldSchemaID, oldSchemaName, tableName := args.OldSchemaID, args.OldSchemaName, args.NewTableName
if job.SchemaState == model.StatePublic {
return finishJobRenameTable(jobCtx, t, job)
}
newSchemaID := job.SchemaID
err = checkTableNotExists(jobCtx.infoCache, newSchemaID, args.NewTableName.L)
err = checkTableNotExists(jobCtx.infoCache, newSchemaID, tableName.L)
if err != nil {
if infoschema.ErrDatabaseNotExists.Equal(err) || infoschema.ErrTableExists.Equal(err) {
job.State = model.JobStateCancelled
}
return ver, errors.Trace(err)
}

tblInfo, err := GetTableInfoAndCancelFaultJob(t, job, args.OldSchemaID)
tblInfo, err := GetTableInfoAndCancelFaultJob(t, job, oldSchemaID)
if err != nil {
return ver, errors.Trace(err)
}
oldTableName := tblInfo.Name
ver, err = checkAndRenameTables(t, job, tblInfo, args.OldSchemaID, job.SchemaID, &args.OldSchemaName, &args.NewTableName)
ver, err = checkAndRenameTables(t, job, tblInfo, oldSchemaID, job.SchemaID, &oldSchemaName, &tableName)
if err != nil {
return ver, errors.Trace(err)
}
fkh := newForeignKeyHelper()
err = adjustForeignKeyChildTableInfoAfterRenameTable(jobCtx.infoCache, t,
job, &fkh, tblInfo, args.OldSchemaName, oldTableName, args.NewTableName, newSchemaID)
job, &fkh, tblInfo, oldSchemaName, oldTableName, tableName, newSchemaID)
if err != nil {
return ver, errors.Trace(err)
}
Expand Down
14 changes: 3 additions & 11 deletions pkg/meta/model/job_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,10 @@ func GetRenameTableArgs(job *Job) (*RenameTableArgs, error) {

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

args := RenameTableArgs{
OldSchemaID: oldSchemaID,
OldSchemaName: oldSchemaName,
Expand Down
9 changes: 2 additions & 7 deletions pkg/meta/model/job_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,13 @@ func TestRenameTableArgs(t *testing.T) {
}
job.FillArgs(inArgs)

// get the args from args
args, err := GetRenameTableArgs(job)
require.NoError(t, err)
require.Equal(t, args, inArgs)

// encode job
_, err = job.Encode(true)
_, err := job.Encode(true)
require.NoError(t, err)
job.Args = nil

// get the args after decode
args, err = GetRenameTableArgs(job)
args, err := GetRenameTableArgs(job)
require.NoError(t, err)
require.Equal(t, args, inArgs)

Expand Down

0 comments on commit 5a61960

Please sign in to comment.