Skip to content

Commit

Permalink
ddl, tests: add expression default values feature relevant tests for …
Browse files Browse the repository at this point in the history
…some DDLs and fix a related bug (#51571)

close #51554, close #51570
  • Loading branch information
zimulala authored Mar 11, 2024
1 parent 5f3fc33 commit 1e5c179
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 39 deletions.
17 changes: 16 additions & 1 deletion pkg/ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ func TestDefaultValueAsExpressions(t *testing.T) {
store := testkit.CreateMockStoreWithSchemaLease(t, testLease)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1")
tk.MustExec("drop table if exists t, t1, t2")

// date_format
tk.MustExec("create table t6 (c int(10), c1 int default (date_format(now(),'%Y-%m-%d %H:%i:%s')))")
Expand All @@ -1630,6 +1630,16 @@ func TestDefaultValueAsExpressions(t *testing.T) {
tk.Session().GetSessionVars().User = &auth.UserIdentity{Username: "xyz", Hostname: "localhost"}
tk.MustExec("insert into t(c) values (4),(5),(6)")
tk.MustExec("insert into t values (7, default)")
rows := tk.MustQuery("SELECT c1 from t order by c").Rows()
for i, row := range rows {
d, ok := row[0].(string)
require.True(t, ok)
if i < 3 {
require.Equal(t, "ROOT", d)
} else {
require.Equal(t, "XYZ", d)
}
}

// replace
tk.MustExec("create table t1 (c int(10), c1 int default (REPLACE(UPPER(UUID()), '-', '')))")
Expand All @@ -1642,6 +1652,11 @@ func TestDefaultValueAsExpressions(t *testing.T) {
if int(sqlErr.Code) != errno.ErrTruncatedWrongValue {
require.Equal(t, errno.ErrDataOutOfRange, int(sqlErr.Code))
}
// test modify column
// The error message has UUID, so put this test here.
tk.MustExec("create table t2(c int(10), c1 varchar(256) default (REPLACE(UPPER(UUID()), '-', '')), index idx(c1));")
tk.MustExec("insert into t2(c) values (1),(2),(3);")
tk.MustGetErrCode("alter table t2 modify column c1 varchar(30) default 'xx';", errno.WarnDataTruncated)
}

func TestChangingDBCharset(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6280,8 +6280,8 @@ func (d *ddl) AlterColumn(ctx sessionctx.Context, ident ast.Ident, spec *ast.Alt

// Clean the NoDefaultValueFlag value.
col.DelFlag(mysql.NoDefaultValueFlag)
col.DefaultIsExpr = false
if len(specNewColumn.Options) == 0 {
col.DefaultIsExpr = false
err = col.SetDefaultValue(nil)
if err != nil {
return errors.Trace(err)
Expand Down
Loading

0 comments on commit 1e5c179

Please sign in to comment.