Skip to content

Commit

Permalink
Default settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris7 committed Sep 24, 2024
1 parent 6d112bb commit 0a626e8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
9 changes: 9 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 8 additions & 0 deletions migrations/operations/01-initial/up.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
82 changes: 71 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +26,13 @@ struct Cli {
command: Option<Commands>,
}

fn get_default_collection(conn: &Connection) -> Option<String> {
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.
Expand All @@ -38,7 +45,7 @@ enum Commands {
gfa: Option<String>,
/// The name of the collection to store the entry under
#[arg(short, long)]
name: String,
name: Option<String>,
/// Don't store the sequence in the database, instead store the filename
#[arg(short, long, action)]
shallow: bool,
Expand All @@ -47,7 +54,7 @@ enum Commands {
Update {
/// The name of the collection to update
#[arg(short, long)]
name: String,
name: Option<String>,
/// A fasta file to insert
#[arg(short, long)]
fasta: Option<String>,
Expand Down Expand Up @@ -104,11 +111,19 @@ enum Commands {
Export {
/// The name of the collection to export
#[arg(short, long)]
name: String,
name: Option<String>,
/// 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<String>,
/// The default collection to use
#[arg(short, long)]
collection: Option<String>,
},
}

fn main() {
Expand All @@ -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<String> = 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);
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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.");
Expand Down Expand Up @@ -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,
}) => {}
}
}

0 comments on commit 0a626e8

Please sign in to comment.