1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
// TODO: Implement DropSchemaExecutor following the same pattern as DropGraphExecutor
use crate::ast::ast::DropSchemaStatement;
use crate::catalog::manager::CatalogManager;
use crate::catalog::operations::{CatalogOperation, EntityType};
use crate::exec::write_stmt::ddl_stmt::DDLStatementExecutor;
use crate::exec::write_stmt::{ExecutionContext, StatementExecutor};
use crate::exec::ExecutionError;
use crate::session::manager;
use crate::storage::StorageManager;
use crate::txn::state::OperationType;
pub struct DropSchemaExecutor {
statement: DropSchemaStatement,
}
impl DropSchemaExecutor {
pub fn new(statement: DropSchemaStatement) -> Self {
Self { statement }
}
}
impl StatementExecutor for DropSchemaExecutor {
fn operation_type(&self) -> OperationType {
OperationType::DropTable // TODO: Add DropSchema to OperationType enum
}
fn operation_description(&self, _context: &ExecutionContext) -> String {
format!(
"DROP SCHEMA {}",
self.statement.schema_path.name().map_or("unknown", |v| v)
)
}
}
impl DDLStatementExecutor for DropSchemaExecutor {
fn execute_ddl_operation(
&self,
_context: &ExecutionContext,
catalog_manager: &mut CatalogManager,
_storage: &StorageManager,
) -> Result<(String, usize), ExecutionError> {
let schema_name = self
.statement
.schema_path
.name()
.map_or("unknown".to_string(), |v| v.clone());
let drop_op = CatalogOperation::Drop {
entity_type: EntityType::Schema,
name: schema_name.clone(),
cascade: self.statement.cascade,
};
// Check for dependent objects based on CASCADE vs RESTRICT
log::info!(
"DROP SCHEMA: cascade={}, schema_name={}",
self.statement.cascade,
schema_name
);
// Get all graphs to check for dependencies
let list_all_graphs_op = CatalogOperation::List {
entity_type: EntityType::Graph,
filters: None, // Get all graphs
};
let graphs_result = catalog_manager.execute("graph_metadata", list_all_graphs_op);
let dependent_graphs = match graphs_result {
Ok(response) => {
if let crate::catalog::operations::CatalogResponse::List { items } = response {
// Filter graphs that belong to this schema
items
.iter()
.filter_map(|graph_entry| {
let graph_schema = graph_entry
.get("id")
.and_then(|id_obj| id_obj.get("schema_name"))
.and_then(|v| v.as_str());
let schema_matches =
graph_schema.map(|s| s == schema_name).unwrap_or(false);
if schema_matches {
graph_entry
.get("id")
.and_then(|id| id.get("name"))
.and_then(|v| v.as_str())
.map(|s| s.to_string())
} else {
None
}
})
.collect::<Vec<_>>()
} else {
Vec::new()
}
}
Err(e) => {
log::error!("Failed to query graph catalog for dependency check: {}", e);
Vec::new()
}
};
// Handle CASCADE vs RESTRICT
if self.statement.cascade {
// CASCADE: Drop all dependent graphs first
log::info!(
"CASCADE: Starting dependency cleanup for schema '{}'",
schema_name
);
for graph_name in &dependent_graphs {
log::info!(
"CASCADE: Dropping dependent graph '{}' in schema '{}'",
graph_name,
schema_name
);
let drop_graph_op = CatalogOperation::Drop {
entity_type: EntityType::Graph,
name: graph_name.clone(),
cascade: true,
};
let drop_result = catalog_manager.execute("graph_metadata", drop_graph_op);
if let Err(e) = drop_result {
log::error!("Failed to drop dependent graph '{}': {}", graph_name, e);
} else {
log::info!("Successfully dropped graph '{}' during CASCADE", graph_name);
}
}
} else {
// RESTRICT: Fail if there are dependent objects
if !dependent_graphs.is_empty() {
let graph_list = dependent_graphs.join(", ");
return Err(ExecutionError::CatalogError(format!(
"Cannot drop schema '{}': contains objects (graphs: {})",
schema_name, graph_list
)));
}
}
// Dependency handling complete - proceed with schema drop
let drop_result = catalog_manager.execute("schema", drop_op);
match drop_result {
Ok(response) => {
match response {
crate::catalog::operations::CatalogResponse::Success { data: _ } => {
// Reset sessions that were using this schema
if let Some(session_manager) = manager::get_session_manager() {
let sessions = session_manager.get_active_session_ids();
for session_id in sessions {
if let Some(session_arc) = session_manager.get_session(&session_id)
{
if let Ok(mut session) = session_arc.write() {
let mut reset_session = false;
// Reset if current schema matches dropped schema
if let Some(current_schema) = &session.current_schema {
if current_schema == &schema_name
|| current_schema == &format!("/{}", schema_name)
{
log::info!("Resetting session {} schema '{}' due to schema drop", session_id, current_schema);
reset_session = true;
}
}
// Reset if current graph is in the dropped schema
if let Some(current_graph) = &session.current_graph {
let graph_pattern = format!("/{}/", schema_name);
if current_graph.starts_with(&graph_pattern) {
log::info!("Resetting session {} graph '{}' due to schema drop", session_id, current_graph);
reset_session = true;
}
}
if reset_session {
session.current_schema = None;
session.current_graph = None;
}
}
}
}
}
// Persist both schema and graph catalogs after successful drop
let persist_result = catalog_manager.persist_catalog("schema");
if let Err(e) = persist_result {
log::error!("Failed to persist schema catalog: {}", e);
}
if self.statement.cascade {
let graph_persist_result =
catalog_manager.persist_catalog("graph_metadata");
if let Err(e) = graph_persist_result {
log::error!("Failed to persist graph catalog after CASCADE: {}", e);
}
}
let message = if self.statement.if_exists {
format!("Schema '{}' dropped (if exists)", schema_name)
} else {
format!("Schema '{}' dropped", schema_name)
};
Ok((message, 1))
}
crate::catalog::operations::CatalogResponse::Error { message }
if message.contains("not found") =>
{
if self.statement.if_exists {
let message =
format!("Schema '{}' does not exist (if exists)", schema_name);
Ok((message, 0))
} else {
Err(ExecutionError::CatalogError(format!(
"Schema '{}' does not exist",
schema_name
)))
}
}
crate::catalog::operations::CatalogResponse::Error { message } => {
Err(ExecutionError::CatalogError(format!(
"Failed to drop schema '{}': {}",
schema_name, message
)))
}
_ => Err(ExecutionError::CatalogError(format!(
"Unexpected response from schema catalog"
))),
}
}
Err(e) => {
if e.to_string().contains("not found") && self.statement.if_exists {
Ok((
format!("Schema '{}' does not exist (if exists)", schema_name),
0,
))
} else {
Err(ExecutionError::CatalogError(format!(
"Failed to drop schema: {}",
e
)))
}
}
}
}
}