ALTER TABLE: prevent dropping/renaming column referenced in VIEW

This commit is contained in:
Jussi Saurio
2025-09-27 09:37:15 +03:00
parent 3137357092
commit 67d320960d

View File

@@ -7929,6 +7929,16 @@ pub fn op_drop_column(
}
}
for (view_name, view) in schema.views.iter() {
let view_select_sql = format!("SELECT * FROM {view_name}");
conn.prepare(view_select_sql.as_str()).map_err(|e| {
LimboError::ParseError(format!(
"cannot drop column \"{}\": referenced in VIEW {view_name}: {}",
column_name, view.sql,
))
})?;
}
state.pc += 1;
Ok(InsnFunctionStepResult::Step)
}
@@ -7984,6 +7994,20 @@ pub fn op_alter_column(
let conn = program.connection.clone();
let normalized_table_name = normalize_ident(table_name.as_str());
let old_column_name = {
let schema = conn.schema.read();
let table = schema
.tables
.get(table_name)
.expect("table being ALTERed should be in schema");
table
.get_column_at(*column_index)
.expect("column being ALTERed should be in schema")
.name
.as_ref()
.expect("column being ALTERed should be named")
.clone()
};
let new_column = crate::schema::Column::from(definition);
conn.with_schema_mut(|schema| {
@@ -8025,6 +8049,27 @@ pub fn op_alter_column(
}
});
let schema = conn.schema.read();
if *rename {
let table = schema
.tables
.get(&normalized_table_name)
.expect("table being ALTERed should be in schema");
let column = table
.get_column_at(*column_index)
.expect("column being ALTERed should be in schema");
for (view_name, view) in schema.views.iter() {
let view_select_sql = format!("SELECT * FROM {view_name}");
// FIXME: this should rewrite the view to reference the new column name
conn.prepare(view_select_sql.as_str()).map_err(|e| {
LimboError::ParseError(format!(
"cannot rename column \"{}\": referenced in VIEW {view_name}: {}",
old_column_name, view.sql,
))
})?;
}
}
state.pc += 1;
Ok(InsnFunctionStepResult::Step)
}