-
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
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -277,6 +277,7 @@ func columnDefToCol(ctx context.Context, offset int, colDef *ast.ColumnDef) (*ta | |
hasDefaultValue = true | ||
removeOnUpdateNowFlag(col) | ||
case ast.ColumnOptionOnUpdate: | ||
// TODO: Support other time functions. | ||
if !expression.IsCurrentTimeExpr(v.Expr) { | ||
return nil, nil, ErrInvalidOnUpdate.Gen("invalid ON UPDATE for - %s", col.Name) | ||
} | ||
|
@@ -289,7 +290,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 +847,10 @@ func modifiable(origin *types.FieldType, to *types.FieldType) bool { | |
return false | ||
} | ||
default: | ||
if origin.Tp == to.Tp { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
@@ -873,24 +878,53 @@ 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: | ||
// TODO: Support other time functions. | ||
if !expression.IsCurrentTimeExpr(opt.Expr) { | ||
return ErrInvalidOnUpdate.Gen("invalid ON UPDATE for - %s", col.Name) | ||
} | ||
|
||
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 +949,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 +960,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, | ||
|
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
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.
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.