risingwave_frontend/handler/
drop_database.rsuse pgwire::pg_response::{PgResponse, StatementType};
use risingwave_sqlparser::ast::{DropMode, ObjectName};
use super::RwPgResponse;
use crate::binder::Binder;
use crate::error::{ErrorCode, Result};
use crate::handler::HandlerArgs;
pub async fn handle_drop_database(
handler_args: HandlerArgs,
database_name: ObjectName,
if_exists: bool,
mode: Option<DropMode>,
) -> Result<RwPgResponse> {
let session = handler_args.session;
let catalog_reader = session.env().catalog_reader();
let database_name = Binder::resolve_database_name(database_name)?;
if session.database() == database_name {
return Err(ErrorCode::InternalError(
"cannot drop the currently open database".to_string(),
)
.into());
}
if mode.is_some() {
return Err(ErrorCode::BindError("Drop database not support drop mode".to_string()).into());
}
let database = {
let reader = catalog_reader.read_guard();
match reader.get_database_by_name(&database_name) {
Ok(db) => db.clone(),
Err(err) => {
return if if_exists {
Ok(PgResponse::builder(StatementType::DROP_DATABASE)
.notice(format!(
"database \"{}\" does not exist, skipping",
database_name
))
.into())
} else {
Err(err.into())
};
}
}
};
session.check_privilege_for_drop_alter_db_schema(&database)?;
let catalog_writer = session.catalog_writer()?;
catalog_writer.drop_database(database.id()).await?;
Ok(PgResponse::empty_result(StatementType::DROP_DATABASE))
}
#[cfg(test)]
mod tests {
use crate::test_utils::LocalFrontend;
#[tokio::test]
async fn test_drop_database() {
let frontend = LocalFrontend::new(Default::default()).await;
let session = frontend.session_ref();
let catalog_reader = session.env().catalog_reader();
frontend.run_sql("CREATE DATABASE database").await.unwrap();
frontend.run_sql("CREATE SCHEMA schema").await.unwrap();
frontend.run_sql("DROP SCHEMA public").await.unwrap();
frontend.run_sql("CREATE USER user WITH NOSUPERUSER NOCREATEDB PASSWORD 'md5827ccb0eea8a706c4c34a16891f84e7b'").await.unwrap();
let user_id = {
let user_reader = session.env().user_info_reader();
user_reader
.read_guard()
.get_user_by_name("user")
.unwrap()
.id
};
let res = frontend
.run_user_sql(
"DROP DATABASE database",
"dev".to_string(),
"user".to_string(),
user_id,
)
.await;
assert!(res.is_err());
frontend.run_sql("DROP DATABASE database").await.unwrap();
let database = catalog_reader
.read_guard()
.get_database_by_name("database")
.ok()
.cloned();
assert!(database.is_none());
}
}