Skip to content

Commit

Permalink
refactor the V2 args for ActionRebaseAutoID, ActionRebaseAutoRandomBa…
Browse files Browse the repository at this point in the history
…se ddl

Signed-off-by: joccau <zak.zhao@pingcap.cn>
  • Loading branch information
joccau committed Sep 19, 2024
1 parent c13eb90 commit 82b25a1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,7 @@ func (e *executor) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase
newBase = newBaseTemp
}
job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: tbInfo.ID,
SchemaName: schema.Name.L,
Expand All @@ -2142,6 +2143,10 @@ func (e *executor) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}
job.FillArgs(&model.RebaseAutoIDArgs{
NewBase: newBase,
Force: force,
})
err = e.DoDDLJob(ctx, job)
return errors.Trace(err)
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,17 +588,15 @@ func onRebaseAutoRandomType(jobCtx *jobContext, t *meta.Meta, job *model.Job) (v
}

func onRebaseAutoID(jobCtx *jobContext, t *meta.Meta, job *model.Job, tp autoid.AllocatorType) (ver int64, _ error) {
schemaID := job.SchemaID
var (
newBase int64
force bool
)
err := job.DecodeArgs(&newBase, &force)
args, err := model.GetRebaseAutoIDArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}

schemaID := job.SchemaID
newBase, force := args.NewBase, args.Force

if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible {
job.MarkNonRevertible()
return ver, nil
Expand Down
35 changes: 35 additions & 0 deletions pkg/meta/model/job_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,38 @@ func GetResourceGroupArgs(job *Job) (*ResourceGroupArgs, error) {
}
return getOrDecodeArgsV2[*ResourceGroupArgs](job)
}

// RebaseAutoIDArgs is the arguments for ActionRebaseAutoID DDL.
// It is also for ActionRebaseAutoRandomBase.
type RebaseAutoIDArgs struct {
NewBase int64 `json:"new_base,omitempty"`
Force bool `json:"force,omitempty"`
}

func (a *RebaseAutoIDArgs) fillJob(job *Job) {
if job.Version == JobVersion1 {
job.Args = []any{a.NewBase, a.Force}
} else {
job.Args = []any{a}
}
}

func GetRebaseAutoIDArgs(job *Job) (*RebaseAutoIDArgs, error) {
var (
newBase int64
force bool
)

if job.Version == JobVersion1 {
if err := job.DecodeArgs(&newBase, &force); err != nil {
return nil, errors.Trace(err)
}
return &RebaseAutoIDArgs{
NewBase: newBase,
Force: force,
}, nil
}

// for version V2
return getOrDecodeArgsV2[*RebaseAutoIDArgs](job)
}
16 changes: 16 additions & 0 deletions pkg/meta/model/job_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,19 @@ func TestResourceGroupArgs(t *testing.T) {
}
}
}

func TestGetRebaseAutoIDArgs(t *testing.T) {
inArgs := &RebaseAutoIDArgs{
NewBase: 9527,
Force: true,
}
for _, tp := range []ActionType{ActionRebaseAutoID, ActionRebaseAutoRandomBase} {
for _, v := range []JobVersion{JobVersion1, JobVersion2} {
j2 := &Job{}
require.NoError(t, j2.Decode(getJobBytes(t, inArgs, v, tp)))
args, err := GetRebaseAutoIDArgs(j2)
require.NoError(t, err)
require.Equal(t, inArgs, args)
}
}
}

0 comments on commit 82b25a1

Please sign in to comment.