From 0a626e8f32852e2b4ebfc9006b3636f86afeb924 Mon Sep 17 00:00:00 2001 From: chris Mitchell Date: Tue, 24 Sep 2024 13:04:22 -0400 Subject: [PATCH 1/2] Default settings --- docs/commands.md | 9 +++ migrations/operations/01-initial/up.sql | 8 +++ src/main.rs | 82 +++++++++++++++++++++---- 3 files changed, 88 insertions(+), 11 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index 05bbd74..494b952 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -1,3 +1,12 @@ +# Defaults + +This controls default choices for the `gen` command line + +- database + - This controls the default database for `gen` to work on, it is what is passed to the `--db` argument. +- collection + - This controls the default collection for `gen` to work on, it is what is passed to the `--name` argument. + # Apply Apply operations to the current branch. diff --git a/migrations/operations/01-initial/up.sql b/migrations/operations/01-initial/up.sql index 23edc05..2354b18 100644 --- a/migrations/operations/01-initial/up.sql +++ b/migrations/operations/01-initial/up.sql @@ -1,3 +1,9 @@ +CREATE TABLE defaults ( + id INTEGER PRIMARY KEY NOT NULL, + db_name TEXT, + collection_name TEXT +) STRICT; + CREATE TABLE operation_state ( db_uuid TEXT PRIMARY KEY NOT NULL, operation_id INTEGER, @@ -41,3 +47,5 @@ CREATE TABLE branch ( FOREIGN KEY(current_operation_id) REFERENCES operation(id) ) STRICT; CREATE UNIQUE INDEX branch_uidx ON branch(db_uuid, name); + +INSERT INTO defaults values (1, NULL, NULL); \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 25ade30..e7ed255 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,10 @@ use gen::get_connection; use gen::imports::fasta::import_fasta; use gen::imports::gfa::import_gfa; use gen::models::metadata; -use gen::models::operations::{setup_db, Branch, Operation, OperationState}; +use gen::models::operations::{setup_db, Branch, OperationState}; use gen::operation_management; use gen::updates::vcf::update_with_vcf; -use rusqlite::types::Value; +use rusqlite::{types::Value, Connection}; use std::fmt::Debug; use std::path::PathBuf; use std::str; @@ -26,6 +26,13 @@ struct Cli { command: Option, } +fn get_default_collection(conn: &Connection) -> Option { + let mut stmt = conn + .prepare("select collection_name from defaults where id = 1") + .unwrap(); + stmt.query_row((), |row| row.get(0)).unwrap() +} + #[derive(Subcommand)] enum Commands { /// Import a new sequence collection. @@ -38,7 +45,7 @@ enum Commands { gfa: Option, /// The name of the collection to store the entry under #[arg(short, long)] - name: String, + name: Option, /// Don't store the sequence in the database, instead store the filename #[arg(short, long, action)] shallow: bool, @@ -47,7 +54,7 @@ enum Commands { Update { /// The name of the collection to update #[arg(short, long)] - name: String, + name: Option, /// A fasta file to insert #[arg(short, long)] fasta: Option, @@ -104,11 +111,19 @@ enum Commands { Export { /// The name of the collection to export #[arg(short, long)] - name: String, + name: Option, /// The name of the GFA file to export to #[arg(short, long)] gfa: String, }, + Defaults { + /// The default database to use + #[arg(short, long)] + database: Option, + /// The default collection to use + #[arg(short, long)] + collection: Option, + }, } fn main() { @@ -120,11 +135,42 @@ fn main() { return; } - let binding = cli.db.expect("No db specified."); + let operation_conn = get_operation_connection(); + if let Some(Commands::Defaults { + database, + collection, + }) = &cli.command + { + if let Some(name) = database { + operation_conn + .execute("update defaults set db_name=?1 where id = 1", (name,)) + .unwrap(); + println!("Default database set to {name}"); + } + if let Some(name) = collection { + operation_conn + .execute( + "update defaults set collection_name=?1 where id = 1", + (name,), + ) + .unwrap(); + println!("Default collection set to {name}"); + } + return; + } + + let binding = cli.db.unwrap_or_else(|| { + let mut stmt = operation_conn + .prepare("select db_name from defaults where id = 1 and db_name is not null;") + .unwrap(); + let row: Option = stmt.query_row((), |row| row.get(0)).unwrap(); + row.expect("No db specified and no default database chosen.") + }); let db = binding.as_str(); let conn = get_connection(db); let db_uuid = metadata::get_db_uuid(&conn); - let operation_conn = get_operation_connection(); + + let default_collection = get_default_collection(&operation_conn); // initialize the selected database if needed. setup_db(&operation_conn, &db_uuid); @@ -137,6 +183,9 @@ fn main() { shallow, }) => { conn.execute("BEGIN TRANSACTION", []).unwrap(); + let name = &name.clone().unwrap_or_else(|| { + default_collection.expect("No collection specified and default not setup.") + }); if fasta.is_some() { import_fasta( &fasta.clone().unwrap(), @@ -162,6 +211,9 @@ fn main() { sample, }) => { conn.execute("BEGIN TRANSACTION", []).unwrap(); + let name = &name.clone().unwrap_or_else(|| { + default_collection.expect("No collection specified and default not setup.") + }); update_with_vcf( vcf, name, @@ -173,10 +225,6 @@ fn main() { conn.execute("END TRANSACTION", []).unwrap(); } - Some(Commands::Init {}) => { - config::get_or_create_gen_dir(); - println!("Gen repository initialized."); - } Some(Commands::Operations { branch }) => { let current_op = OperationState::get_operation(&operation_conn, &db_uuid) .expect("Unable to read operation."); @@ -288,10 +336,22 @@ fn main() { operation_management::checkout(&conn, &operation_conn, &db_uuid, branch, *id); } Some(Commands::Export { name, gfa }) => { + let name = &name.clone().unwrap_or_else(|| { + default_collection.expect("No collection specified and default not setup.") + }); conn.execute("BEGIN TRANSACTION", []).unwrap(); export_gfa(&conn, name, &PathBuf::from(gfa)); conn.execute("END TRANSACTION", []).unwrap(); } None => {} + // these will never be handled by this method as we search for them earlier. + Some(Commands::Init {}) => { + config::get_or_create_gen_dir(); + println!("Gen repository initialized."); + } + Some(Commands::Defaults { + database, + collection, + }) => {} } } From deb99f76c55452aa4ea3c896441565c90c34a659 Mon Sep 17 00:00:00 2001 From: chris Mitchell Date: Tue, 24 Sep 2024 13:05:53 -0400 Subject: [PATCH 2/2] Fix dbname query --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e7ed255..a60fb2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,7 +161,7 @@ fn main() { let binding = cli.db.unwrap_or_else(|| { let mut stmt = operation_conn - .prepare("select db_name from defaults where id = 1 and db_name is not null;") + .prepare("select db_name from defaults where id = 1;") .unwrap(); let row: Option = stmt.query_row((), |row| row.get(0)).unwrap(); row.expect("No db specified and no default database chosen.")