Skip to content

Commit

Permalink
ddl: fix the parallel problem of "set default value" and other… (#11346)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and winkyao committed Jul 23, 2019
1 parent e8d254a commit f8662a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func onSetDefaultValue(t *meta.Meta, job *model.Job) (ver int64, _ error) {
return ver, errors.Trace(err)
}

return updateColumn(t, job, newCol, &newCol.Name)
return updateColumnDefaultValue(t, job, newCol, &newCol.Name)
}

func (w *worker) onModifyColumn(t *meta.Meta, job *model.Job) (ver int64, _ error) {
Expand Down Expand Up @@ -481,7 +481,7 @@ func checkForNullValue(ctx sessionctx.Context, isDataTruncated bool, schema, tab
return nil
}

func updateColumn(t *meta.Meta, job *model.Job, newCol *model.ColumnInfo, oldColName *model.CIStr) (ver int64, _ error) {
func updateColumnDefaultValue(t *meta.Meta, job *model.Job, newCol *model.ColumnInfo, oldColName *model.CIStr) (ver int64, _ error) {
tblInfo, err := getTableInfoAndCancelFaultJob(t, job, job.SchemaID)
if err != nil {
return ver, errors.Trace(err)
Expand All @@ -491,7 +491,10 @@ func updateColumn(t *meta.Meta, job *model.Job, newCol *model.ColumnInfo, oldCol
job.State = model.JobStateCancelled
return ver, infoschema.ErrColumnNotExists.GenWithStackByArgs(newCol.Name, tblInfo.Name)
}
*oldCol = *newCol
// 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
oldCol.Flag = newCol.Flag

ver, err = updateVersionAndTableInfo(t, job, tblInfo, true)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,30 @@ func (s *testStateChangeSuite) TestParallelAlterModifyColumn(c *C) {
// s.testControlParallelExecSQL(c, sql1, sql2, f)
// }

func (s *testStateChangeSuite) TestParallelAddColumAndSetDefaultValue(c *C) {
_, err := s.se.Execute(context.Background(), "use test_db_state")
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), `create table tx (
c1 varchar(64),
c2 enum('N','Y') not null default 'N',
primary key idx2 (c2, c1))`)
c.Assert(err, IsNil)
_, err = s.se.Execute(context.Background(), "insert into tx values('a', 'N')")
c.Assert(err, IsNil)
defer s.se.Execute(context.Background(), "drop table tx")

sql1 := "alter table tx add column cx int after c1"
sql2 := "alter table tx alter c2 set default 'N'"

f := func(c *C, err1, err2 error) {
c.Assert(err1, IsNil)
c.Assert(err2, IsNil)
_, err := s.se.Execute(context.Background(), "delete from tx where c1='a'")
c.Assert(err, IsNil)
}
s.testControlParallelExecSQL(c, sql1, sql2, f)
}

func (s *testStateChangeSuite) TestParallelChangeColumnName(c *C) {
sql1 := "ALTER TABLE t CHANGE a aa int;"
sql2 := "ALTER TABLE t CHANGE b aa int;"
Expand Down

0 comments on commit f8662a2

Please sign in to comment.