mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-23 08:55:40 +01:00
Add helpers to rewrite REFERENCES from foriegn keys in ColumnDefinition
This commit is contained in:
33
core/util.rs
33
core/util.rs
@@ -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::*;
|
||||
|
||||
Reference in New Issue
Block a user