Skip to content

Commit

Permalink
refactor v2 job args for ActionModifyTableComment ddl
Browse files Browse the repository at this point in the history
Signed-off-by: joccau <zak.zhao@pingcap.cn>
  • Loading branch information
joccau committed Sep 19, 2024
1 parent 91681e7 commit 9602c87
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3505,17 +3505,20 @@ func (e *executor) AlterTableComment(ctx sessionctx.Context, ident ast.Ident, sp
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: tb.Meta().ID,
SchemaName: schema.Name.L,
TableName: tb.Meta().Name.L,
Type: model.ActionModifyTableComment,
BinlogInfo: &model.HistoryInfo{},
Args: []any{spec.Comment},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}

job.FillArgs(&model.ModifyTableCommentArgs{
Comment: spec.Comment,
})
err = e.DoDDLJob(ctx, job)
return errors.Trace(err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,8 @@ func finishJobRenameTables(jobCtx *jobContext, t *meta.Meta, job *model.Job,
}

func onModifyTableComment(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver int64, _ error) {
var comment string
if err := job.DecodeArgs(&comment); err != nil {
args, err := model.GetModifyTableCommentArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
Expand All @@ -994,7 +994,7 @@ func onModifyTableComment(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver
return ver, nil
}

tblInfo.Comment = comment
tblInfo.Comment = args.Comment
ver, err = updateVersionAndTableInfo(jobCtx, t, job, tblInfo, true)
if err != nil {
return ver, errors.Trace(err)
Expand Down
30 changes: 30 additions & 0 deletions pkg/meta/model/job_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,33 @@ func GetRebaseAutoIDArgs(job *Job) (*RebaseAutoIDArgs, error) {
// for version V2
return getOrDecodeArgsV2[*RebaseAutoIDArgs](job)
}

// ModifyTableCommentArgs is the arguments for ActionModifyTableComment ddl.
type ModifyTableCommentArgs struct {
Comment string
}

func (a *ModifyTableCommentArgs) fillJob(job *Job) {
intest.Assert(job.Version == JobVersion1 || job.Version == JobVersion2, "job version is invalid")

if job.Version == JobVersion1 {
job.Args = []any{a.Comment}
} else {
job.Args = []any{a}
}
}

// GetModifyTableCommentArgs gets the args for ActionModifyTableComment.
func GetModifyTableCommentArgs(job *Job) (*ModifyTableCommentArgs, error) {
if job.Version == JobVersion1 {
var comment string
if err := job.DecodeArgs(&comment); err != nil {
return nil, errors.Trace(err)
}
return &ModifyTableCommentArgs{
Comment: comment,
}, nil
}

return getOrDecodeArgsV2[*ModifyTableCommentArgs](job)
}
14 changes: 14 additions & 0 deletions pkg/meta/model/job_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,17 @@ func TestGetRebaseAutoIDArgs(t *testing.T) {
}
}
}

func TestGetModifyTableCommentArgs(t *testing.T) {
inArgs := &ModifyTableCommentArgs{
Comment: "TiDb is great",
}

for _, v := range []JobVersion{JobVersion1, JobVersion2} {
j2 := &Job{}
require.NoError(t, j2.Decode(getJobBytes(t, inArgs, v, ActionModifyTableComment)))
args, err := GetModifyTableCommentArgs(j2)
require.NoError(t, err)
require.Equal(t, inArgs, args)
}
}

0 comments on commit 9602c87

Please sign in to comment.