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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Migration impact analysis.
use super::scanner::CodeReference;
use crate::ast::{Action, Qail};
use crate::migrate::Schema;
use std::collections::HashMap;
/// Result of analyzing migration impact on codebase.
#[derive(Debug, Default)]
pub struct MigrationImpact {
/// Breaking changes that will cause runtime errors
pub breaking_changes: Vec<BreakingChange>,
/// Warnings that may cause issues
pub warnings: Vec<Warning>,
pub safe_to_run: bool,
/// Total number of affected files
pub affected_files: usize,
}
/// A breaking change detected in the migration.
#[derive(Debug)]
pub enum BreakingChange {
/// A column is being dropped that is still referenced in code
DroppedColumn {
table: String,
column: String,
references: Vec<CodeReference>,
},
/// A table is being dropped that is still referenced in code
DroppedTable {
table: String,
references: Vec<CodeReference>,
},
/// A column is being renamed (requires code update)
RenamedColumn {
table: String,
old_name: String,
new_name: String,
references: Vec<CodeReference>,
},
/// A column type is changing (may cause runtime errors)
TypeChanged {
table: String,
column: String,
old_type: String,
new_type: String,
references: Vec<CodeReference>,
},
}
/// A warning about the migration.
#[derive(Debug)]
pub enum Warning {
OrphanedReference {
table: String,
references: Vec<CodeReference>,
},
}
impl MigrationImpact {
/// Analyze migration commands against codebase references.
pub fn analyze(
commands: &[Qail],
code_refs: &[CodeReference],
_old_schema: &Schema,
_new_schema: &Schema,
) -> Self {
let mut impact = MigrationImpact::default();
let mut table_refs: HashMap<String, Vec<&CodeReference>> = HashMap::new();
let mut column_refs: HashMap<(String, String), Vec<&CodeReference>> = HashMap::new();
for code_ref in code_refs {
table_refs
.entry(code_ref.table.clone())
.or_default()
.push(code_ref);
for col in &code_ref.columns {
column_refs
.entry((code_ref.table.clone(), col.clone()))
.or_default()
.push(code_ref);
}
}
// Analyze each migration command
for cmd in commands {
match cmd.action {
Action::Drop => {
// Table being dropped
if let Some(refs) = table_refs.get(&cmd.table)
&& !refs.is_empty()
{
impact.breaking_changes.push(BreakingChange::DroppedTable {
table: cmd.table.clone(),
references: refs.iter().map(|r| (*r).clone()).collect(),
});
}
}
Action::AlterDrop => {
for col_expr in &cmd.columns {
if let crate::ast::Expr::Named(col_name) = col_expr {
let key = (cmd.table.clone(), col_name.clone());
if let Some(refs) = column_refs.get(&key)
&& !refs.is_empty()
{
impact.breaking_changes.push(BreakingChange::DroppedColumn {
table: cmd.table.clone(),
column: col_name.clone(),
references: refs.iter().map(|r| (*r).clone()).collect(),
});
}
}
}
}
Action::Mod => {
// Rename operation - check for references to old name
// Would need to parse the rename details from the command
// For now, flag any table with Mod action
if let Some(refs) = table_refs.get(&cmd.table)
&& !refs.is_empty()
{
impact.breaking_changes.push(BreakingChange::RenamedColumn {
table: cmd.table.clone(),
old_name: "unknown".to_string(),
new_name: "unknown".to_string(),
references: refs.iter().map(|r| (*r).clone()).collect(),
});
}
}
_ => {}
}
}
// Count affected files
let mut affected: std::collections::HashSet<_> = std::collections::HashSet::new();
for change in &impact.breaking_changes {
match change {
BreakingChange::DroppedColumn { references, .. }
| BreakingChange::DroppedTable { references, .. }
| BreakingChange::RenamedColumn { references, .. }
| BreakingChange::TypeChanged { references, .. } => {
for r in references {
affected.insert(r.file.clone());
}
}
}
}
impact.affected_files = affected.len();
impact.safe_to_run = impact.breaking_changes.is_empty();
impact
}
/// Generate a human-readable report.
pub fn report(&self) -> String {
let mut output = String::new();
if self.safe_to_run {
output.push_str("✓ Migration is safe to run\n");
return output;
}
output.push_str("⚠️ BREAKING CHANGES DETECTED\n\n");
output.push_str(&format!("Affected files: {}\n\n", self.affected_files));
for change in &self.breaking_changes {
match change {
BreakingChange::DroppedColumn {
table,
column,
references,
} => {
output.push_str(&format!(
"DROP COLUMN {}.{} ({} references)\n",
table,
column,
references.len()
));
for r in references.iter().take(5) {
// Show the specific column that was matched, not just the generic snippet
output.push_str(&format!(
" ❌ {}:{} → uses \"{}\" in {}\n",
r.file.display(),
r.line,
column, // The actual matched column
r.snippet
));
}
if references.len() > 5 {
output.push_str(&format!(" ... and {} more\n", references.len() - 5));
}
output.push('\n');
}
BreakingChange::DroppedTable { table, references } => {
output.push_str(&format!(
"DROP TABLE {} ({} references)\n",
table,
references.len()
));
for r in references.iter().take(5) {
output.push_str(&format!(
" ❌ {}:{} → {}\n",
r.file.display(),
r.line,
r.snippet
));
}
output.push('\n');
}
BreakingChange::RenamedColumn {
table,
old_name,
new_name,
references,
} => {
output.push_str(&format!(
"RENAME {}.{} → {} ({} references)\n",
table,
old_name,
new_name,
references.len()
));
for r in references.iter().take(5) {
output.push_str(&format!(
" ⚠️ {}:{} → {}\n",
r.file.display(),
r.line,
r.snippet
));
}
output.push('\n');
}
BreakingChange::TypeChanged {
table,
column,
old_type,
new_type,
references,
} => {
output.push_str(&format!(
"TYPE CHANGE {}.{}: {} → {} ({} references)\n",
table,
column,
old_type,
new_type,
references.len()
));
for r in references.iter().take(5) {
output.push_str(&format!(
" ⚠️ {}:{} → {}\n",
r.file.display(),
r.line,
r.snippet
));
}
output.push('\n');
}
}
}
output
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_detect_dropped_table() {
let cmd = Qail {
action: Action::Drop,
table: "users".to_string(),
..Default::default()
};
let code_ref = CodeReference {
file: PathBuf::from("src/handlers.rs"),
line: 42,
table: "users".to_string(),
columns: vec!["name".to_string()],
query_type: super::super::scanner::QueryType::Qail,
snippet: "get users fields *".to_string(),
};
let old_schema = Schema::new();
let new_schema = Schema::new();
let impact = MigrationImpact::analyze(&[cmd], &[code_ref], &old_schema, &new_schema);
assert!(!impact.safe_to_run);
assert_eq!(impact.breaking_changes.len(), 1);
}
}