From 6c6242a2c5e28736f86ab13416dc8a360117ec85 Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Sun, 21 Apr 2024 00:48:28 +0800 Subject: [PATCH] rename private functions --- butane_core/src/db/pg.rs | 16 ++++++++-------- butane_core/src/migrations/adb.rs | 7 ++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/butane_core/src/db/pg.rs b/butane_core/src/db/pg.rs index 811b4c5..96703a4 100644 --- a/butane_core/src/db/pg.rs +++ b/butane_core/src/db/pg.rs @@ -548,10 +548,10 @@ where fn sql_for_op(current: &mut ADB, op: &Operation) -> Result { match op { Operation::AddTable(table) => Ok(create_table(table, false)?), - Operation::AddTableConstraints(table) => Ok(create_table_constraints(table)), + Operation::AddTableConstraints(table) => Ok(create_table_fkey_constraints(table)), Operation::AddTableIfNotExists(table) => Ok(create_table(table, true)?), Operation::RemoveTable(name) => Ok(drop_table(name)), - Operation::RemoveTableConstraints(table) => remove_table_constraints(table), + Operation::RemoveTableConstraints(table) => remove_table_fkey_constraints(table), Operation::AddColumn(tbl, col) => add_column(tbl, col), Operation::RemoveColumn(tbl, name) => Ok(remove_column(tbl, name)), Operation::ChangeColumn(tbl, old, new) => { @@ -586,22 +586,22 @@ fn create_table(table: &ATable, allow_exists: bool) -> Result { )) } -fn create_table_constraints(table: &ATable) -> String { +fn create_table_fkey_constraints(table: &ATable) -> String { table .columns .iter() .filter(|column| column.reference().is_some()) - .map(|column| define_constraint(&table.name, column)) + .map(|column| define_fkey_constraint(&table.name, column)) .collect::>() .join("\n") } -fn remove_table_constraints(table: &ATable) -> Result { +fn remove_table_fkey_constraints(table: &ATable) -> Result { Ok(table .columns .iter() .filter(|column| column.reference().is_some()) - .map(|column| drop_fk_constraints(table, column)) + .map(|column| drop_fkey_constraints(table, column)) .collect::>>()? .join("\n")) } @@ -632,7 +632,7 @@ fn define_column(col: &AColumn) -> Result { )) } -fn define_constraint(table_name: &str, column: &AColumn) -> String { +fn define_fkey_constraint(table_name: &str, column: &AColumn) -> String { let reference = column .reference() .as_ref() @@ -651,7 +651,7 @@ fn define_constraint(table_name: &str, column: &AColumn) -> String { } } -fn drop_fk_constraints(table: &ATable, column: &AColumn) -> Result { +fn drop_fkey_constraints(table: &ATable, column: &AColumn) -> Result { let mut modified_column = column.clone(); modified_column.remove_reference(); change_column(table, column, &modified_column) diff --git a/butane_core/src/migrations/adb.rs b/butane_core/src/migrations/adb.rs index 9138bb8..b5526b5 100644 --- a/butane_core/src/migrations/adb.rs +++ b/butane_core/src/migrations/adb.rs @@ -486,7 +486,7 @@ impl AColumn { } /// Remove the column that this column refers to. pub fn remove_reference(&mut self) { - self.reference = None + self.reference = None; } /// Get the type identifier. pub fn typeid(&self) -> Result { @@ -578,13 +578,12 @@ pub fn create_many_table( } /// Individual operation use to apply a migration. +/// The order of operations in a diff roughly follows this enum order. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub enum Operation { //future improvement: support column renames /// Add a table. AddTable(ATable), - /// Add table constraints referring to other tables, if the backend supports it. - AddTableConstraints(ATable), /// Add a table, if it doesnt already exist. AddTableIfNotExists(ATable), /// Remove table constraints referring to other tables, if the backend supports it. @@ -597,6 +596,8 @@ pub enum Operation { RemoveColumn(String, String), /// Change a table columns type. ChangeColumn(String, AColumn, AColumn), + /// Add table constraints referring to other tables, if the backend supports it. + AddTableConstraints(ATable), } /// Determine the operations necessary to move the database schema from `old` to `new`.