-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ddl: fix ddl modify column from null to not null bug: check null value again before change to no null #10948
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4d01fd1
ddl: fix ddl modify column from null to not null bug
crazycs520 88c64a8
Merge branch 'master' into fix-null-to-notnull
crazycs520 7580127
refine test
crazycs520 9044d51
refine test
crazycs520 3be21e4
fix test
crazycs520 bb72bbd
refine test
crazycs520 d205e3f
Merge branch 'master' into fix-null-to-notnull
crazycs520 fc93575
Merge branch 'master' into fix-null-to-notnull
crazycs520 bdfd5e0
Address comment
crazycs520 9ea5c8b
Merge branch 'master' into fix-null-to-notnull
crazycs520 d28b4ec
address comment
crazycs520 2cd1bdf
Merge branch 'master' into fix-null-to-notnull
lonng de42748
address comment
crazycs520 c89f33d
Merge branch 'master' of https://github.com/pingcap/tidb into fix-nul…
crazycs520 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2470,50 +2470,73 @@ LOOP: | |
|
||
func (s *testDBSuite1) TestModifyColumnNullToNotNull(c *C) { | ||
s.tk = testkit.NewTestKit(c, s.store) | ||
tk2 := testkit.NewTestKit(c, s.store) | ||
tk2.MustExec("use test_db") | ||
s.mustExec(c, "use test_db") | ||
s.mustExec(c, "drop table if exists t1") | ||
s.mustExec(c, "create table t1 (c1 int, c2 int);") | ||
|
||
tbl := s.testGetTable(c, "t1") | ||
var insertErr error | ||
hook := &ddl.TestDDLCallback{} | ||
hook.OnJobRunBeforeExported = func(job *model.Job) { | ||
if tbl.Meta().ID != job.TableID { | ||
return | ||
} | ||
var c2 *table.Column | ||
getModifyColumn := func() *table.Column { | ||
t := s.testGetTable(c, "t1") | ||
for _, col := range t.Cols() { | ||
if col.Name.L == "c2" { | ||
c2 = col | ||
return col | ||
} | ||
} | ||
if mysql.HasPreventNullInsertFlag(c2.Flag) { | ||
_, insertErr = s.tk.Exec("insert into t1 values ();") | ||
} | ||
return nil | ||
} | ||
|
||
originalHook := s.dom.DDL().GetHook() | ||
defer s.dom.DDL().(ddl.DDLForTest).SetHook(originalHook) | ||
|
||
// Check insert null before job first update. | ||
times := 0 | ||
hook := &ddl.TestDDLCallback{} | ||
s.tk.MustExec("delete from t1") | ||
hook.OnJobUpdatedExported = func(job *model.Job) { | ||
if tbl.Meta().ID != job.TableID { | ||
return | ||
} | ||
if job.State != model.JobStateRunning { | ||
return | ||
} | ||
if times == 0 { | ||
tk2.MustExec("insert into t1 values ();") | ||
} | ||
times++ | ||
} | ||
s.dom.DDL().(ddl.DDLForTest).SetHook(hook) | ||
done := make(chan error, 1) | ||
go backgroundExec(s.store, "alter table t1 change c2 c2 bigint not null;", done) | ||
err := <-done | ||
c.Assert(err, IsNil) | ||
_, err := s.tk.Exec("alter table t1 change c2 c2 int not null;") | ||
c.Assert(err, NotNil) | ||
c.Assert(err.Error(), Equals, "[ddl:1138]Invalid use of NULL value") | ||
s.tk.MustQuery("select * from t1").Check(testkit.Rows("<nil> <nil>")) | ||
|
||
var c2 *table.Column | ||
t := s.testGetTable(c, "t1") | ||
for _, col := range t.Cols() { | ||
if col.Name.L == "c2" { | ||
c2 = col | ||
// Check insert error when column has prevent null flag. | ||
s.tk.MustExec("delete from t1") | ||
hook.OnJobUpdatedExported = nil | ||
hook.OnJobRunBeforeExported = func(job *model.Job) { | ||
if tbl.Meta().ID != job.TableID { | ||
return | ||
} | ||
if job.State != model.JobStateRunning { | ||
return | ||
} | ||
c2 := getModifyColumn() | ||
if mysql.HasPreventNullInsertFlag(c2.Flag) { | ||
_, err := s.tk.Exec("insert into t1 values ();") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ye~, Thanks. |
||
c.Assert(err.Error(), Equals, "[table:1048]Column 'c2' cannot be null") | ||
} | ||
} | ||
|
||
s.dom.DDL().(ddl.DDLForTest).SetHook(hook) | ||
s.tk.MustExec("alter table t1 change c2 c2 bigint not null;") | ||
|
||
c2 := getModifyColumn() | ||
c.Assert(mysql.HasNotNullFlag(c2.Flag), IsTrue) | ||
c.Assert(mysql.HasPreventNullInsertFlag(c2.Flag), IsFalse) | ||
c.Assert(insertErr.Error(), Equals, "[table:1048]Column 'c2' cannot be null") | ||
_, insertErr = s.tk.Exec("insert into t1 values ();") | ||
c.Assert(insertErr.Error(), Equals, "[table:1364]Field 'c2' doesn't have a default value") | ||
s.dom.DDL().(ddl.DDLForTest).SetHook(originalHook) | ||
s.mustExec(c, "drop table t1") | ||
_, err = s.tk.Exec("insert into t1 values ();") | ||
c.Assert(err.Error(), Equals, "[table:1364]Field 'c2' doesn't have a default value") | ||
} | ||
|
||
func (s *testDBSuite2) TestTransactionOnAddDropColumn(c *C) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that if
noPreventNullFlag
is false, we only need to docheckForNullValue
. In other words, we needn't doupdateVersionAndTableInfoWithCheck
. Do we change this code?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, Thanks.