Skip to content
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

impl alter database rename #5286

Conversation

TCeason
Copy link
Collaborator

@TCeason TCeason commented May 10, 2022

I hereby agree to the terms of the CLA available at: https://databend.rs/dev/policies/cla/

Summary

Support alter database.

Changelog

  • New Feature

Related Issues

Fixes #4839

@vercel
Copy link

vercel bot commented May 10, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
databend ✅ Ready (Inspect) Visit Preview May 11, 2022 at 0:20AM (UTC)

@mergify
Copy link
Contributor

mergify bot commented May 10, 2022

Thanks for the contribution!
I have applied any labels matching special text in your PR Changelog.

Please review the labels and make any necessary changes.

@mergify mergify bot added pr-feature this PR introduces a new feature to the codebase pr-build this PR changes build/testing/ci steps pr-doc-fix labels May 10, 2022
@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from fe3d6bd to 20f2e73 Compare May 10, 2022 14:19
@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from 20f2e73 to 383f32c Compare May 10, 2022 14:30
@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from 383f32c to b83df89 Compare May 10, 2022 14:36
@BohuTANG BohuTANG requested review from drmingdrmer and removed request for BohuTANG May 10, 2022 15:27
@BohuTANG BohuTANG removed pr-build this PR changes build/testing/ci steps pr-doc-fix labels May 10, 2022
@mergify mergify bot added pr-build this PR changes build/testing/ci steps pr-doc-fix labels May 10, 2022
@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from b83df89 to 7edcaef Compare May 10, 2022 15:29
Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great PR!

Comment on lines 221 to 223
// TODO(like rename table): if database not exists but sql hint if_exists is true
// the sql must be Ok but should not return database_id
return Ok(RenameDatabaseReply { db_id: 0 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest removing db_id in the RenameDatabaseReply because renaming a db does not change its id.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will have a look.

let (db_id_seq, _db_id) = get_id_value(self, &tenant_newdbname).await?;
db_has_to_not_exist(db_id_seq, &tenant_newdbname, "rename_database")?;

let new_db_id = fetch_id(self, DatabaseIdGen {}).await?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use the old_db_id. We do not need to create a new id. So that the application(databend-query) will always be able to access this db with the same id.

// rename database
{
let txn_req = TxnRequest {
condition: vec![txn_cond_seq(&tenant_newdbname, Eq, 0)?],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It requires another txn_cond_seq to ensure the (tenant, old_db_name) -> (old_db_id_seq, old_db_id) is not removed before this transaction completes.

common/meta/api/src/schema_api_impl.rs Outdated Show resolved Hide resolved
common/meta/api/src/schema_api_impl.rs Show resolved Hide resolved
common/meta/api/src/schema_api_test_suite.rs Show resolved Hide resolved
common/meta/api/src/schema_api_test_suite.rs Outdated Show resolved Hide resolved
@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from 7edcaef to 142de4f Compare May 11, 2022 08:27
common/ast/src/ast/statement.rs Outdated Show resolved Hide resolved
common/ast/src/ast/statement.rs Outdated Show resolved Hide resolved
@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from 142de4f to f740c94 Compare May 11, 2022 09:22
@TCeason TCeason removed pr-build this PR changes build/testing/ci steps pr-doc-fix labels May 11, 2022
@BohuTANG
Copy link
Member

@mergify update

@mergify
Copy link
Contributor

mergify bot commented May 11, 2022

update

✅ Branch has been successfully updated

@TCeason TCeason force-pushed the ISSUE-4839/support_alter_database_with_new_api branch from 9ff69b9 to 9e33858 Compare May 11, 2022 12:18
Comment on lines +209 to +226
let res = get_db_or_err(
self,
tenant_dbname,
format!("rename_database: {}", &tenant_dbname),
)
.await;

let (old_db_id_seq, old_db_id, _, _) = match res {
Ok(x) => x,
Err(e) => {
if let MetaError::AppError(AppError::UnknownDatabase(_)) = e {
if req.if_exists {
return Ok(RenameDatabaseReply {});
}
}
return Err(e);
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since db_meta is never used, this part can be simplified to let res = get_id_value(self, tenant_db_name...) ..., which will save one RPC to metasrv.

But it's not a big deal and can be done in next PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-review pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support: ALTER DATABASE [ IF EXISTS ] <name> RENAME TO <new_db_name>
5 participants