-
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: implement ALTER DATABASE
to alter charset/collation
#10393
Conversation
PTAL @crazycs520 @lonng @winkyao |
/run-all-tests |
e4cd9d0
to
f31cc2a
Compare
Codecov Report
@@ Coverage Diff @@
## master #10393 +/- ##
================================================
- Coverage 77.3263% 77.2982% -0.0281%
================================================
Files 412 412
Lines 86629 86720 +91
================================================
+ Hits 66987 67033 +46
- Misses 14498 14528 +30
- Partials 5144 5159 +15 |
/rebuild |
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.
LGTM
@bb7133 I got some problem, but maybe we can fix this in other PR: mysql root@localhost:(none)> create database test charset utf8;
Query OK, 0 rows affected
Time: 0.005s
mysql root@localhost:(none)> alter database test charset="utf8"
(1105, u'unsupported modify collate from to utf8_bin')
mysql root@localhost:(none)> drop database if exists test
Query OK, 0 rows affected
Time: 0.010s
mysql root@localhost:(none)> create database test charset UTF8MB4;
Query OK, 0 rows affected
Time: 0.005s
mysql root@localhost:(none)> alter database test charset="utf8mb4"
(1105, u'unsupported modify collate from to utf8mb4_bin')
mysql root@localhost:(none)> There is some bug in create a schema. |
Line 2170 in d241d74
Isn't this an expected behavior? For now, we only support changing charset from |
16ef132
to
82966c6
Compare
@jackysp @crazycs520 addressed, PTAL, thank you! |
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.
rest LGTM
ddl/schema.go
Outdated
|
||
dbInfo, err := checkSchemaExistAndCancelNotExistJob(t, job) | ||
if err != nil { | ||
if infoschema.ErrDatabaseDropExists.Equal(err) { |
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.
This error is weird, I think to call checkSchemaExistAndCancelNotExistJob
is not appropriate. Maybe you should call t.GetDatabase(job.SchemaID)
directly.
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.
addressed
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.
LGTM
infoschema/builder.go
Outdated
if !ok { | ||
return ErrDatabaseNotExists.GenWithStackByArgs(di.Name.O) | ||
} | ||
if oldInfo.Charset == di.Charset && oldInfo.Collate == di.Collate { |
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.
If this happens, it is already processed when the DDL job is processed. Doesn't this happen here?
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.
I suppose another DDL job may have changed the charset already, is it possible?
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.
I don't think so. If another DDL job changed, this DDL job needn't to be handled.
infoschema/builder.go
Outdated
return errors.Trace(err) | ||
} | ||
if di == nil { | ||
// When we apply an old schema diff, the database may has been dropped already |
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.
Ditto. Maybe we can update this comment.
} | ||
return ver, errors.Trace(err) | ||
} | ||
|
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.
I think we can check "if dbInfo.Charset == toCharset && dbInfo.Charset == toCollate {return nil}" here。
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.
OK, then
ddl/ddl_api.go
Outdated
return errors.Trace(err) | ||
} | ||
|
||
// Do DDL job |
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.
// Do DDL job | |
// Do the DDL job. |
ddl/ddl_api.go
Outdated
return nil | ||
} | ||
|
||
// check current TiDB limitations |
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.
// check current TiDB limitations | |
// Check the current TiDB limitations. |
ddl/ddl_api.go
Outdated
@@ -90,6 +90,63 @@ func (d *ddl) CreateSchema(ctx sessionctx.Context, schema model.CIStr, charsetIn | |||
err = d.callHookOnChanged(err) | |||
return errors.Trace(err) | |||
} | |||
func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) (err error) { | |||
// Resolve target charset and collation from options |
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.
Please add the .
at the end of a line. And please update other comments in this PR.
1d8cf8d
to
b3ec525
Compare
} | ||
|
||
dbInfo, err := t.GetDatabase(job.SchemaID) | ||
if err != nil { |
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.
Here we need to add job.State = model.JobStateCancelled
to make sure the job will be done.
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.
yes, if we don't cancel the job here, the job will never be finished.
b3ec525
to
2f14d76
Compare
ddl/schema.go
Outdated
} | ||
|
||
if dbInfo.Charset == toCharset && dbInfo.Collate == toCollate { | ||
job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) |
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.
I think replace this line with job.State = model.JobStateCancelled
is better.
We don't do this schema change, becase other job is done.
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.
But this will cause an error message, right?
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.
No. Here we return error is nil.
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.
addressed, thanks @zimulala
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.
LGTM
/run-all-tests |
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.
LGTM
/run-all-tests |
What problem does this PR solve?
Add support for
ALTER DATABASE
syntaxRelated issue: #4641
Related parser PR: pingcap/parser#318
What is changed and how it works?
A new DDL job is added to change charset/collate for a database.
Check List
Tests
Code changes
Side effects
Related changes