Add helpers to rewrite REFERENCES from foriegn keys in ColumnDefinition

This commit is contained in:
PThorpe92
2025-10-21 10:40:31 -04:00
parent 7c746e476c
commit 06e3b9611b

View File

@@ -1331,6 +1331,39 @@ pub fn extract_view_columns(
Ok(ViewColumnSchema { tables, columns })
}
pub fn rewrite_fk_parent_cols_if_self_ref(
clause: &mut ast::ForeignKeyClause,
table: &str,
from: &str,
to: &str,
) {
if normalize_ident(clause.tbl_name.as_str()) == normalize_ident(table) {
for c in &mut clause.columns {
if normalize_ident(c.col_name.as_str()) == normalize_ident(from) {
c.col_name = ast::Name::exact(to.to_owned());
}
}
}
}
/// Update a column-level REFERENCES <tbl>(col,...) constraint
pub fn rewrite_column_references_if_needed(
col: &mut ast::ColumnDefinition,
table: &str,
from: &str,
to: &str,
) {
for cc in &mut col.constraints {
if let ast::NamedColumnConstraint {
constraint: ast::ColumnConstraint::ForeignKey { clause, .. },
..
} = cc
{
rewrite_fk_parent_cols_if_self_ref(clause, table, from, to);
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;