Add better error handling for dataset rename command

This commit is contained in:
Kiana Sheibani 2023-12-01 19:21:13 -05:00
parent 4f240b318d
commit fc7e6df283
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 17 additions and 1 deletions

View file

@ -132,6 +132,10 @@ pub fn delete_dataset(connection: &Connection, dataset: &str) -> sqlite::Result<
} }
pub fn rename_dataset(connection: &Connection, old: &str, new: &str) -> sqlite::Result<()> { pub fn rename_dataset(connection: &Connection, old: &str, new: &str) -> sqlite::Result<()> {
if old == new {
return Ok(());
}
let query = format!( let query = format!(
r#"UPDATE datasets SET name = '{1}' WHERE name = '{0}'; r#"UPDATE datasets SET name = '{1}' WHERE name = '{0}';
ALTER TABLE "{0}_players" RENAME TO "{1}_players"; ALTER TABLE "{0}_players" RENAME TO "{1}_players";

View file

@ -472,7 +472,19 @@ fn dataset_rename(old: Option<String>, new: Option<String>) {
let connection = let connection =
open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 2)); open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 2));
rename_dataset(&connection, &old, &new).unwrap();
match rename_dataset(&connection, &old, &new) {
Ok(()) => (),
Err(sqlite::Error {
code: Some(1),
message: _,
}) => error(&format!("Dataset {:?} does not exist", &old), 1),
Err(sqlite::Error {
code: Some(19),
message: _,
}) => error(&format!("Dataset {:?} already exists", &new), 1),
Err(_) => error("Unknown error occurred", 2),
};
} }
// Players // Players