Skip to content

Commit

Permalink
ddl: check default value again in updateColumnDefaultValue (#40230)
Browse files Browse the repository at this point in the history
close #40164
  • Loading branch information
wjhuang2016 authored Dec 30, 2022
1 parent 1b8a233 commit 6131e3f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,15 @@ func updateColumnDefaultValue(d *ddlCtx, t *meta.Meta, job *model.Job, newCol *m
job.State = model.JobStateCancelled
return ver, infoschema.ErrColumnNotExists.GenWithStackByArgs(newCol.Name, tblInfo.Name)
}

if hasDefaultValue, _, err := checkColumnDefaultValue(newContext(d.store), table.ToColumn(oldCol.Clone()), newCol.DefaultValue); err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
} else if !hasDefaultValue {
job.State = model.JobStateCancelled
return ver, dbterror.ErrInvalidDefaultValue.GenWithStackByArgs(newCol.Name)
}

// The newCol's offset may be the value of the old schema version, so we can't use newCol directly.
oldCol.DefaultValue = newCol.DefaultValue
oldCol.DefaultValueBit = newCol.DefaultValueBit
Expand Down
35 changes: 35 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -1789,3 +1790,37 @@ func TestHashPartitionAddColumn(t *testing.T) {
dom.DDL().SetHook(hook)
tk.MustExec("alter table t add column c int")
}

func TestSetInvalidDefaultValueAfterModifyColumn(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int)")

var wg sync.WaitGroup
var checkErr error
one := false
hook := &ddl.TestDDLCallback{Do: dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.SchemaState != model.StateDeleteOnly {
return
}
if !one {
one = true
} else {
return
}
wg.Add(1)
go func() {
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")
_, checkErr = tk2.Exec("alter table t alter column a set default 1")
wg.Done()
}()
}
dom.DDL().SetHook(hook)
tk.MustExec("alter table t modify column a text(100)")
wg.Wait()
require.EqualError(t, checkErr, "[ddl:1101]BLOB/TEXT/JSON column 'a' can't have a default value")
}

0 comments on commit 6131e3f

Please sign in to comment.