feat(opertor): check if a database is in use before dropping it (#4035)

feat(opertor): check if database is in use before dropping it
This commit is contained in:
Eugene Tolbakov
2024-05-27 04:31:58 +01:00
committed by GitHub
parent 25d64255a3
commit af486ec0d0
6 changed files with 37 additions and 7 deletions

View File

@@ -256,6 +256,13 @@ pub enum Error {
location: Location,
},
#[snafu(display("Schema `{name}` is in use"))]
SchemaInUse {
name: String,
#[snafu(implicit)]
location: Location,
},
#[snafu(display("Table occurs error"))]
Table {
#[snafu(implicit)]
@@ -727,6 +734,7 @@ impl ErrorExt for Error {
| Error::IllegalPrimaryKeysDef { .. }
| Error::SchemaNotFound { .. }
| Error::SchemaExists { .. }
| Error::SchemaInUse { .. }
| Error::ColumnNotFound { .. }
| Error::BuildRegex { .. }
| Error::InvalidSchema { .. }

View File

@@ -68,9 +68,9 @@ use crate::error::{
CreateLogicalTablesSnafu, CreateTableInfoSnafu, DdlWithMultiCatalogsSnafu,
DdlWithMultiSchemasSnafu, DeserializePartitionSnafu, EmptyDdlExprSnafu, FlowNotFoundSnafu,
InvalidPartitionColumnsSnafu, InvalidPartitionRuleSnafu, InvalidTableNameSnafu,
InvalidViewNameSnafu, InvalidViewStmtSnafu, ParseSqlValueSnafu, Result, SchemaNotFoundSnafu,
SubstraitCodecSnafu, TableAlreadyExistsSnafu, TableMetadataManagerSnafu, TableNotFoundSnafu,
UnrecognizedTableOptionSnafu, ViewAlreadyExistsSnafu,
InvalidViewNameSnafu, InvalidViewStmtSnafu, ParseSqlValueSnafu, Result, SchemaInUseSnafu,
SchemaNotFoundSnafu, SubstraitCodecSnafu, TableAlreadyExistsSnafu, TableMetadataManagerSnafu,
TableNotFoundSnafu, UnrecognizedTableOptionSnafu, ViewAlreadyExistsSnafu,
};
use crate::expr_factory;
use crate::statement::show::create_partitions_stmt;
@@ -667,10 +667,14 @@ impl StatementExecutor {
.await
.context(CatalogSnafu)?
{
self.drop_database_procedure(catalog, schema, drop_if_exists, query_context)
.await?;
if schema == query_context.current_schema() {
SchemaInUseSnafu { name: schema }.fail()
} else {
self.drop_database_procedure(catalog, schema, drop_if_exists, query_context)
.await?;
Ok(Output::new_with_affected_rows(0))
Ok(Output::new_with_affected_rows(0))
}
} else if drop_if_exists {
// DROP TABLE IF EXISTS meets table not found - ignored
Ok(Output::new_with_affected_rows(0))

View File

@@ -120,6 +120,14 @@ SHOW TABLES FROM public WHERE Tables = 'numbers';
DROP SCHEMA test_public_schema;
Error: 1004(InvalidArguments), Schema `test_public_schema` is in use
USE public;
Affected Rows: 0
DROP SCHEMA test_public_schema;
Affected Rows: 0
SELECT * FROM test_public_schema.hello;

View File

@@ -40,6 +40,10 @@ SHOW TABLES FROM public WHERE Tables = 'numbers';
DROP SCHEMA test_public_schema;
USE public;
DROP SCHEMA test_public_schema;
SELECT * FROM test_public_schema.hello;
USE public;

View File

@@ -100,9 +100,13 @@ Affected Rows: 0
drop database upper_case_table_name;
Affected Rows: 0
Error: 1004(InvalidArguments), Schema `upper_case_table_name` is in use
use public;
Affected Rows: 0
drop database upper_case_table_name;
Affected Rows: 0

View File

@@ -43,3 +43,5 @@ drop table abcdefge;
drop database upper_case_table_name;
use public;
drop database upper_case_table_name;