Skip to content

Commit

Permalink
implement rename_column(); confirm loads in cli.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcmicu committed Nov 17, 2024
1 parent ee9f95d commit 8f789f3
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 145 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ api_test: sqlite_api_test pg_api_test
.PHONY: sqlite_api_test
sqlite_api_test: valve test/src/table.tsv build/valve.db test/insert_update.sh | test/output
@echo "Testing API functions on sqlite ..."
./$< --source $(word 2,$^) --database $(word 3,$^) --assume-yes test-api
./$< --source $(word 2,$^) --database $(word 3,$^) --assume-yes test api
$(word 4,$^) $(word 3,$^) $(word 2,$^)
scripts/export_messages.py $(word 3,$^) $| $(tables_to_test)
diff --strip-trailing-cr -q test/expected/messages_after_api_test.tsv test/output/messages.tsv
Expand All @@ -110,7 +110,7 @@ sqlite_api_test: valve test/src/table.tsv build/valve.db test/insert_update.sh |
pg_api_test: valve test/src/table.tsv test/insert_update.sh | test/output
@echo "Testing API functions on postgresql ..."
./$< --source $(word 2,$^) --database $(pg_connect_string) --assume-yes load-all
./$< --source $(word 2,$^) --database $(pg_connect_string) --assume-yes test-api
./$< --source $(word 2,$^) --database $(pg_connect_string) --assume-yes test api
$(word 3,$^) $(pg_connect_string) $(word 2,$^)
scripts/export_messages.py $(pg_connect_string) $| $(tables_to_test)
diff --strip-trailing-cr -q test/expected/messages_after_api_test.tsv test/output/messages.tsv
Expand Down
145 changes: 131 additions & 14 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Command line interface
use crate::{
tests::{run_api_tests, run_dt_hierarchy_tests},
toolkit::{generic_select_with_message_values, local_sql_syntax, DbKind},
Expand Down Expand Up @@ -524,6 +526,10 @@ pub enum RenameSubcommands {
#[arg(value_name = "NEW_NAME", action = ArgAction::Set,
help = "The desired new name for the table")]
new_name: String,

#[arg(long, action = ArgAction::SetTrue,
help = "Do not load the table after deleting COLUMN")]
no_load: bool,
},

/// Rename a datatype
Expand Down Expand Up @@ -562,15 +568,15 @@ pub enum RenameSubcommands {
pub enum TestSubcommands {
/// Run a set of predefined tests, on a specified pre-loaded database, that will test Valve's
/// Application Programmer Interface.
TestApi {},
Api {},

/// Run a set of predefined tests, on a specified pre-loaded database, that will test Valve's
/// Application Programmer Interface.
TestCli {},
Cli {},

/// Run a set of predefined tests, on a specified pre-loaded database, that will test the
/// validity of the configured datatype hierarchy.
TestDtHierarchy {},
DtHierarchy {},
}

/// Build a Valve instance in conformity with the given command-line options and parameters.
Expand All @@ -592,9 +598,40 @@ pub async fn add_column(cli: &Cli, table: &Option<String>, column: &Option<Strin
let json_row = read_json_row_for_table(&valve, "column");
let column_json = extract_column_fields(&json_row, table, column);
valve
.add_column(table, column, &column_json, no_load)
.add_column(table, column, &column_json)
.await
.expect("Error adding column");

let column = column_json
.get("column")
.expect("No column given")
.as_str()
.expect("Not a string");
let table = column_json
.get("table")
.expect("No table given")
.as_str()
.expect("Not a string");
let load_table = {
if no_load {
false
} else if !valve.interactive {
true
} else {
// Ask the user if they would like to load now:
print!(
"Added column '{column}' to '{table}'. Do you want to load '{table}' \
now? [y/N] ",
);
proceed::proceed()
}
};
if load_table {
valve
.load_tables(&vec![table], true)
.await
.expect("Error loading table");
}
}

/// Use Valve, in conformity with the given command-line parameters, to add a datatype to the
Expand Down Expand Up @@ -666,9 +703,26 @@ pub async fn add_table(
) {
let mut valve = build_valve(&cli).await;
valve
.add_table(table, path, sample_size, error_rate, seed, no_load)
.add_table(table, path, sample_size, error_rate, seed)
.await
.expect("Error adding table");
let load_table = {
if no_load {
false
} else if !valve.interactive {
true
} else {
// Ask the user if they would like to load now:
print!("Added table '{table}'. Do you want to load '{table}' now? [y/N] ",);
proceed::proceed()
}
};
if load_table {
valve
.load_tables(&vec![table], true)
.await
.expect("Error loading table");
}
}

/// Use Valve, in conformity with the given command-line parameters, to (re)create an empty
Expand Down Expand Up @@ -701,9 +755,31 @@ pub async fn delete_datatype(cli: &Cli, datatype: &str) {
pub async fn delete_column(cli: &Cli, table: &str, column: &str, no_load: bool) {
let mut valve = build_valve(&cli).await;
valve
.delete_column(table, column, no_load)
.delete_column(table, column)
.await
.expect("Error deleting column");

// Possibly load the data table:
let load_table = {
if no_load {
false
} else if !valve.interactive {
true
} else {
// Ask the user if they would like to load now:
print!(
"Column '{column}' deleted from '{table}'. Do you want to load '{table}' \
now? [y/N] ",
);
proceed::proceed()
}
};
if load_table {
valve
.load_tables(&vec![table], true)
.await
.expect("Error loading table");
}
}

/// Use Valve, in conformity with the given command-line parameters, to either (i) in the case
Expand Down Expand Up @@ -1286,9 +1362,31 @@ pub async fn rename_column(
) {
let mut valve = build_valve(&cli).await;
valve
.rename_column(table, column, new_name, label, no_load)
.rename_column(table, column, new_name, label)
.await
.expect("Error renaming column");

let load_table = {
if no_load {
false
} else if !valve.interactive {
true
} else {
// Ask the user if they would like to load now:
print!(
"Column '{column}' renamed in '{table}'. Do you want to load '{table}' \
now? [y/N] ",
);
proceed::proceed()
}
};
if load_table {
valve.set_interactive(false);
valve
.load_tables(&vec![table], true)
.await
.expect("Error loading table");
}
}

/// Use Valve, in conformity with the given command-line parameters, to rename the given datatype
Expand All @@ -1303,12 +1401,29 @@ pub async fn rename_datatype(cli: &Cli, datatype: &str, new_name: &str) {

/// Use Valve, in conformity with the given command-line parameters, to rename the given table
/// to the given new table name.
pub async fn rename_table(cli: &Cli, table: &str, new_name: &str) {
pub async fn rename_table(cli: &Cli, table: &str, new_name: &str, no_load: bool) {
let mut valve = build_valve(&cli).await;
valve
.rename_table(table, new_name)
.await
.expect("Error renaming table");
let load_table = {
if no_load {
false
} else if !valve.interactive {
true
} else {
// Ask the user if they would like to load now:
print!("Table '{table}' was renamed. Do you want to load '{table}' now? [y/N] ",);
proceed::proceed()
}
};
if load_table {
valve
.load_tables(&vec![table], true)
.await
.expect("Error loading table");
}
}

/// Use Valve, in conformity with the given command-line parameters, to save the given tables,
Expand Down Expand Up @@ -2001,9 +2116,11 @@ pub async fn process_command() {
RenameSubcommands::Datatype { datatype, new_name } => {
rename_datatype(&cli, datatype, new_name).await
}
RenameSubcommands::Table { table, new_name } => {
rename_table(&cli, table, new_name).await
}
RenameSubcommands::Table {
table,
new_name,
no_load,
} => rename_table(&cli, table, new_name, *no_load).await,
};
}
Commands::Save { save_dir, tables } => {
Expand All @@ -2014,13 +2131,13 @@ pub async fn process_command() {
}
Commands::Test { subcommand } => {
match subcommand {
TestSubcommands::TestApi {} => {
TestSubcommands::Api {} => {
test_api(&cli).await;
}
TestSubcommands::TestCli {} => {
TestSubcommands::Cli {} => {
test_cli(&cli).await;
}
TestSubcommands::TestDtHierarchy {} => {
TestSubcommands::DtHierarchy {} => {
test_dt_hierarchy(&cli).await;
}
};
Expand Down
25 changes: 25 additions & 0 deletions src/toolkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,7 @@ pub fn get_table_constraints(
match &*args[0] {
Expression::Field(ftable, fcolumn) => {
foreigns.push(ValveForeignConstraint {
original: structure.to_string(),
table: table_name.to_string(),
column: column_name.to_string(),
ftable: ftable.to_string(),
Expand Down Expand Up @@ -1597,6 +1598,8 @@ pub fn get_table_constraints(
);
}
trees.push(ValveTreeConstraint {
original: structure.to_string(),
table: table_name.to_string(),
child: child.to_string(),
parent: column_name.to_string(),
});
Expand Down Expand Up @@ -2093,6 +2096,28 @@ pub fn get_label_for_column(
}
}

/// Given a table name, a column name, and a new name for the column, rename the column to the
/// new name in the database table and associated views, using the given database transaction.
pub async fn rename_db_column_tx(
tx: &mut Transaction<'_, sqlx::Any>,
table: &str,
column: &str,
new_name: &str,
) -> Result<()> {
for table in [
table,
&format!("{table}_conflict"),
&format!("{table}_view"),
&format!("{table}_text_view"),
] {
let sql = format!(r#"ALTER TABLE "{table}" RENAME COLUMN "{column}" TO "{new_name}""#);
let query = sqlx_query(&sql);
query.execute(tx.acquire().await?).await?;
}

Ok(())
}

/// Given a global config map, a map of compiled datatype conditions, a table name and a column
/// name, get the value type of the column.
pub fn get_value_type(
Expand Down
Loading

0 comments on commit 8f789f3

Please sign in to comment.