-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: Support some definitions and fix a bug to modify|change column
#2629
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,7 +289,7 @@ func columnDefToCol(ctx context.Context, offset int, colDef *ast.ColumnDef) (*ta | |
return nil, nil, errors.Trace(err) | ||
} | ||
case ast.ColumnOptionFulltext: | ||
// Do nothing. | ||
// TODO: Support this type. | ||
} | ||
} | ||
} | ||
|
@@ -846,6 +846,10 @@ func modifiable(origin *types.FieldType, to *types.FieldType) bool { | |
return false | ||
} | ||
default: | ||
if origin.Tp == to.Tp { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
@@ -873,24 +877,52 @@ func setDefaultAndComment(ctx context.Context, col *table.Column, options []*ast | |
return nil | ||
} | ||
|
||
for _, v := range options { | ||
switch v.Tp { | ||
var ( | ||
hasDefaultValue bool | ||
hasNotNull bool | ||
setOnUpdateNow bool | ||
) | ||
for _, opt := range options { | ||
switch opt.Tp { | ||
case ast.ColumnOptionDefaultValue: | ||
err := setDefaultValue(ctx, col, v) | ||
value, err := getDefaultValue(ctx, opt, col.Tp, col.Decimal) | ||
if err != nil { | ||
return errors.Trace(err) | ||
return ErrColumnBadNull.Gen("invalid default value - %s", err) | ||
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. Add test case for this case. |
||
} | ||
col.DefaultValue = value | ||
hasDefaultValue = true | ||
case ast.ColumnOptionComment: | ||
err := setColumnComment(ctx, col, v) | ||
err := setColumnComment(ctx, col, opt) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
case ast.ColumnOptionNotNull: | ||
col.Flag |= mysql.NotNullFlag | ||
hasNotNull = true | ||
case ast.ColumnOptionNull: | ||
col.Flag &= ^uint(mysql.NotNullFlag) | ||
case ast.ColumnOptionOnUpdate: | ||
if !expression.IsCurrentTimeExpr(opt.Expr) { | ||
return ErrInvalidOnUpdate.Gen("invalid ON UPDATE for - %s", col.Name) | ||
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. Add test case for this case. 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. This check is done at the parsing layer In MySQL. But we didn't do that. I will fix it in the next PR. 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. And now we only support the type of |
||
} | ||
|
||
col.Flag |= mysql.OnUpdateNowFlag | ||
setOnUpdateNow = true | ||
default: | ||
// TODO: Support other types. | ||
return errors.Trace(errUnsupportedModifyColumn) | ||
} | ||
} | ||
|
||
setTimestampDefaultValue(col, hasDefaultValue, setOnUpdateNow) | ||
if mysql.HasTimestampFlag(col.Flag) && hasNotNull { | ||
return errors.Trace(errInvalidUseOfNull) | ||
} | ||
|
||
if hasDefaultValue { | ||
return errors.Trace(checkDefaultValue(ctx, col, true)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -915,6 +947,7 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, ident ast.Ident, origi | |
// Make sure the column definition is simple field type. | ||
return nil, errors.Trace(errUnsupportedModifyColumn) | ||
} | ||
|
||
if err := setDefaultAndComment(ctx, col, spec.NewColumn.Options); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
@@ -925,6 +958,8 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, ident ast.Ident, origi | |
|
||
newCol := *col | ||
newCol.FieldType = *spec.NewColumn.Tp | ||
// TODO: Remove this line after merged PR 2627. | ||
newCol.Flag = col.Flag | ||
newCol.Name = spec.NewColumn.Name.Name | ||
job := &model.Job{ | ||
SchemaID: schema.ID, | ||
|
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.
Need to check
Flen
,Decimal
andFlag
?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.
There are checked in line819-830.