use crate::control::security::identity::AuthenticatedIdentity;
use crate::control::state::SharedState;
use super::super::super::result::{DdlError, DdlResult};
use super::super::auth_support::{require_tenant_admin, status};
pub fn drop_consumer_group(
state: &SharedState,
identity: &AuthenticatedIdentity,
parts: &[&str],
) -> Result<Vec<DdlResult>, DdlError> {
require_tenant_admin(identity, "drop consumer groups")?;
if parts.len() < 6 || !parts[4].eq_ignore_ascii_case("ON") {
return Err(DdlError {
sqlstate: "42601".to_string(),
message: "expected DROP CONSUMER GROUP <name> ON <stream>".to_string(),
});
}
let group_name = parts[3].to_lowercase();
let stream_name = parts[5].to_lowercase();
let tenant_id = identity.tenant_id.as_u64();
let catalog = state.credentials.catalog();
let existed = catalog
.delete_consumer_group(tenant_id, &stream_name, &group_name)
.map_err(|e| DdlError {
sqlstate: "XX000".to_string(),
message: format!("catalog delete: {e}"),
})?;
if !existed {
return Err(DdlError {
sqlstate: "42704".to_string(),
message: format!(
"consumer group '{group_name}' does not exist on stream '{stream_name}'"
),
});
}
state
.group_registry
.unregister(tenant_id, &stream_name, &group_name);
if let Err(e) = state
.offset_store
.delete_group(tenant_id, &stream_name, &group_name)
{
tracing::warn!(
error = %e,
"failed to delete offsets for consumer group {group_name}"
);
}
state.audit_record(
crate::control::security::audit::AuditEvent::AdminAction,
Some(identity.tenant_id),
&identity.username,
&format!("DROP CONSUMER GROUP {group_name} ON {stream_name}"),
);
Ok(status("DROP CONSUMER GROUP"))
}