Going somewhere.
This commit is contained in:
1 parent
ded31da7a3
commit
fc13cf7f99
6 files changed
+244
-26
No files matched your search
+57
-4
@@ -12,6 +12,7 @@ use crate::migration::migrate;
|
||||
|
||||
mod migration;
|
||||
mod users;
|
||||
mod securities;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct Output<T> {
|
||||
@@ -31,7 +32,7 @@ trait CommandLineUserLogic {
|
||||
|
||||
fn list_user(&mut self, conn: &Connection);
|
||||
|
||||
// fn list_securities(&mut self, conn: &Connection);
|
||||
fn list_securities(&mut self, conn: &Connection);
|
||||
}
|
||||
|
||||
fn verbosity_arg() -> Arg {
|
||||
@@ -84,11 +85,23 @@ fn user_command_line_parser() -> Command {
|
||||
.subcommand(ls_user_command_line_parser());
|
||||
}
|
||||
|
||||
fn ls_securities_command_line_parser() -> Command {
|
||||
return Command::new("ls")
|
||||
.long_flag("ls")
|
||||
.about("List all securities.");
|
||||
}
|
||||
|
||||
fn securities_command_line_parser() -> Command {
|
||||
return Command::new("securities")
|
||||
.subcommand(ls_securities_command_line_parser());
|
||||
}
|
||||
|
||||
fn command_line_parser() -> Command {
|
||||
return Command::new("prog")
|
||||
.arg(verbosity_arg())
|
||||
.arg(db_path_arg())
|
||||
.subcommand(user_command_line_parser());
|
||||
.subcommand(user_command_line_parser())
|
||||
.subcommand(securities_command_line_parser());
|
||||
}
|
||||
|
||||
fn process_new_user_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
@@ -116,12 +129,33 @@ fn process_user_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn process_list_securities_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing list securities command line {:?}.", m);
|
||||
logic.list_securities(conn);
|
||||
}
|
||||
|
||||
|
||||
fn process_securities_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing securities command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("ls", list_securities_match)) => {
|
||||
process_list_securities_command_line(conn, list_securities_match, logic);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn process_command_line(conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic) {
|
||||
debug!("Processing command line {:?}.", m);
|
||||
match m.subcommand() {
|
||||
Some(("users", user_match)) => {
|
||||
process_user_command_line(conn, user_match, logic);
|
||||
}
|
||||
|
||||
Some(("securities", securities_match)) => {
|
||||
process_securities_command_line(conn, securities_match, logic);
|
||||
}
|
||||
_ => {
|
||||
println!("Received unparsable command. {:?}", m.subcommand());
|
||||
unreachable!();
|
||||
@@ -136,7 +170,7 @@ mod test {
|
||||
struct TestCommandLineLogic {
|
||||
list_user_count: usize,
|
||||
create_user_count: usize,
|
||||
|
||||
list_securities_count: usize,
|
||||
}
|
||||
|
||||
impl TestCommandLineLogic {
|
||||
@@ -144,6 +178,7 @@ mod test {
|
||||
return TestCommandLineLogic {
|
||||
list_user_count: 0,
|
||||
create_user_count: 0,
|
||||
list_securities_count: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -156,6 +191,10 @@ mod test {
|
||||
fn list_user(&mut self, conn: &Connection) {
|
||||
self.list_user_count += 1;
|
||||
}
|
||||
|
||||
fn list_securities(&mut self, conn: &Connection) {
|
||||
self.list_securities_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -210,6 +249,16 @@ mod test {
|
||||
process_command_line(&conn, &m, &mut logic);
|
||||
assert_eq!(logic.list_user_count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_securities() {
|
||||
let cmd = command_line_parser();
|
||||
let m = cmd.get_matches_from(vec!["test", "securities", "ls"]);
|
||||
let mut logic = TestCommandLineLogic::new();
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
process_command_line(&conn, &m, &mut logic);
|
||||
assert_eq!(logic.list_securities_count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
struct ProCommandLineLogic {}
|
||||
@@ -243,6 +292,10 @@ impl CommandLineUserLogic for ProCommandLineLogic {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn list_securities(&mut self, conn: &Connection) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -262,7 +315,7 @@ fn main() {
|
||||
|
||||
set_logger(&m);
|
||||
let conn = get_connection(&m);
|
||||
migrate(&conn);
|
||||
// migrate(&conn);
|
||||
let mut logic = ProCommandLineLogic::new();
|
||||
process_command_line(&conn, &m, &mut logic);
|
||||
}
|
||||
+115
-17
@@ -1,4 +1,13 @@
|
||||
use rusqlite::Connection;
|
||||
use rusqlite::{Connection, Transaction};
|
||||
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
|
||||
const INITIAL_MIGRATION: &str = "
|
||||
CREATE TABLE IF NOT EXISTS shitty_migrations (
|
||||
id INTEGER PRIMARY KEY,
|
||||
sha256 BLOB(2)
|
||||
);
|
||||
";
|
||||
|
||||
const MIGRATION_SCRIPT: &str = "
|
||||
|
||||
@@ -20,24 +29,113 @@ CREATE TABLE IF NOT EXISTS securities (
|
||||
symbol TEXT NOT NULL UNIQUE,
|
||||
|
||||
FOREIGN KEY(security_type) REFERENCES security_types(id)
|
||||
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS account(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
security INTEGER NOT NULL,
|
||||
owner INTEGER NOT NULL,
|
||||
|
||||
FOREIGN KEY(security) REFERENCES securities(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS account_values(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
);
|
||||
|
||||
//
|
||||
// CREATE TABLE IF NOT EXISTS account(
|
||||
// id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
// security INTEGER NOT NULL,
|
||||
// owner INTEGER NOT NULL,
|
||||
//
|
||||
// FOREIGN KEY(security) REFERENCES securities(id)
|
||||
// );
|
||||
//
|
||||
// CREATE TABLE IF NOT EXISTS account_values(
|
||||
// id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
// );
|
||||
|
||||
";
|
||||
|
||||
pub fn migrate(conn: & Connection) {
|
||||
conn.execute(MIGRATION_SCRIPT, ()).unwrap();
|
||||
fn get_id_of_new_migration(tx: &Transaction) -> usize {
|
||||
let q = "SELECT COALESCE(MAX(id) + 1, 0) FROM shitty_migrations";
|
||||
return tx.query_row(q, (), |row| row.get::<usize, usize>(0)).unwrap();
|
||||
}
|
||||
|
||||
|
||||
pub fn migrate(conn: &mut Connection, migrations: &Vec<String>) {
|
||||
let tx = conn.transaction().unwrap();
|
||||
tx.execute(INITIAL_MIGRATION, ()).unwrap();
|
||||
let id = get_id_of_new_migration(&tx);
|
||||
|
||||
for (i, migration) in migrations.iter().enumerate() {
|
||||
if i < id {
|
||||
check_migration(&tx, i, migration);
|
||||
} else {
|
||||
apply_migration(&tx, i, migration);
|
||||
}
|
||||
}
|
||||
tx.commit().unwrap();
|
||||
}
|
||||
|
||||
fn apply_migration(tx: &Transaction, id: usize, migration: &String) {
|
||||
let hash = compute_migration_sha256(migration);
|
||||
tx.execute(migration, ()).unwrap();
|
||||
let q = "INSERT INTO shitty_migrations(id, sha256) VALUES (?1, ?2)";
|
||||
tx.execute(q, (&id, &hash)).unwrap();
|
||||
}
|
||||
|
||||
fn compute_migration_sha256(migration: &String) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(migration);
|
||||
let ret = hasher.finalize();
|
||||
return format!("{:x}", ret);
|
||||
}
|
||||
|
||||
fn check_migration(tx: &Transaction, id: usize, migration: &String) -> Result<String, io::Error> {
|
||||
let q = "SELECT sha256 FROM shitty_migrations WHERE id = ?1";
|
||||
let res = tx.query_row(q, (&id, ), |row| row.get::<usize, String>(0)).unwrap();
|
||||
let expected = compute_migration_sha256(migration);
|
||||
assert_eq!(&res, &expected);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
fn create_basic_migrations() -> Vec<String> {
|
||||
let migration1 = "CREATE TABLE people(name TEXT, age INTEGER);".to_string();
|
||||
let migration2 = "INSERT INTO people(name, age) VALUES (\"didier\", 37);".to_string();
|
||||
return vec![migration1, migration2];
|
||||
}
|
||||
|
||||
fn create_basic_migrations_2() -> Vec<String> {
|
||||
let migration1 = "CREATE TABLE people(name TEXT, age INTEGER);".to_string();
|
||||
let migration2 = "INSERT INTO people(name, age) VALUES (\"stephanie\", 38);".to_string();
|
||||
return vec![migration1, migration2];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let mut conn = Connection::open_in_memory().unwrap();
|
||||
migrate(&mut conn, &create_basic_migrations());
|
||||
let tx = conn.transaction().unwrap();
|
||||
assert_eq!(get_id_of_new_migration(&tx), 2);
|
||||
|
||||
let q = "SELECT COUNT(*) FROM people;";
|
||||
let c = tx.query_row(q, (), |x| x.get::<usize, usize>(0)).unwrap();
|
||||
assert_eq!(c, c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idempotency() {
|
||||
let mut conn = Connection::open_in_memory().unwrap();
|
||||
|
||||
migrate(&mut conn, &create_basic_migrations());
|
||||
migrate(&mut conn, &create_basic_migrations());
|
||||
|
||||
let tx = conn.transaction().unwrap();
|
||||
|
||||
assert_eq!(get_id_of_new_migration(&tx), 2);
|
||||
let q = "SELECT COUNT(*) FROM people;";
|
||||
let c = tx.query_row(q, (), |x| x.get::<usize, usize>(0)).unwrap();
|
||||
assert_eq!(c, c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broken_migration() {
|
||||
let mut conn = Connection::open_in_memory().unwrap();
|
||||
|
||||
migrate(&mut conn, &create_basic_migrations());
|
||||
migrate(&mut conn, &create_basic_migrations_2());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
|
||||
Reference in new issue
Block a user