Skip to content

Commit

Permalink
rename private functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Apr 20, 2024
1 parent dc83f72 commit 6c6242a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 8 additions & 8 deletions butane_core/src/db/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ where
fn sql_for_op(current: &mut ADB, op: &Operation) -> Result<String> {
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) => {
Expand Down Expand Up @@ -586,22 +586,22 @@ fn create_table(table: &ATable, allow_exists: bool) -> Result<String> {
))
}

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::<Vec<String>>()
.join("\n")
}

fn remove_table_constraints(table: &ATable) -> Result<String> {
fn remove_table_fkey_constraints(table: &ATable) -> Result<String> {
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::<Result<Vec<String>>>()?
.join("\n"))
}
Expand Down Expand Up @@ -632,7 +632,7 @@ fn define_column(col: &AColumn) -> Result<String> {
))
}

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()
Expand All @@ -651,7 +651,7 @@ fn define_constraint(table_name: &str, column: &AColumn) -> String {
}
}

fn drop_fk_constraints(table: &ATable, column: &AColumn) -> Result<String> {
fn drop_fkey_constraints(table: &ATable, column: &AColumn) -> Result<String> {
let mut modified_column = column.clone();
modified_column.remove_reference();
change_column(table, column, &modified_column)
Expand Down
7 changes: 4 additions & 3 deletions butane_core/src/migrations/adb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeIdentifier> {
Expand Down Expand Up @@ -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.
Expand All @@ -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`.
Expand Down

0 comments on commit 6c6242a

Please sign in to comment.