From 3dc963cdea8c0ad07db04a5c4fe52582c3409c6c Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 5 Dec 2022 09:44:05 -0500 Subject: [PATCH] Going somewhere. --- Cargo.lock | 30 ++++++++++---------- Cargo.toml | 2 +- src/main.rs | 16 +++++------ src/migration.rs | 41 ++-------------------------- src/migrations/create_tables_001.sql | 36 ++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 62 deletions(-) create mode 100644 src/migrations/create_tables_001.sql diff --git a/Cargo.lock b/Cargo.lock index 891e05b..576a236 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,6 +138,21 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "escarmouche-ledger" +version = "0.1.0" +dependencies = [ + "clap", + "clap-verbosity-flag", + "home", + "log", + "rusqlite", + "serde", + "serde_json", + "sha2", + "simple_logger", +] + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -251,21 +266,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "machine" -version = "0.1.0" -dependencies = [ - "clap", - "clap-verbosity-flag", - "home", - "log", - "rusqlite", - "serde", - "serde_json", - "sha2", - "simple_logger", -] - [[package]] name = "num_threads" version = "0.1.6" diff --git a/Cargo.toml b/Cargo.toml index fc089c9..a77801a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "machine" +name = "escarmouche-ledger" version = "0.1.0" edition = "2021" diff --git a/src/main.rs b/src/main.rs index 5f9a63a..e451b5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ -use std::any::Any; use std::path::PathBuf; use clap::{Arg, ArgAction, ArgMatches, Command, value_parser}; use serde::{Serialize}; -use log::{debug, error, info}; +use log::{debug, info}; use rusqlite::Connection; use simple_logger::SimpleLogger; @@ -184,15 +183,15 @@ mod test { } impl CommandLineUserLogic for TestCommandLineLogic { - fn create_user(&mut self, conn: &Connection, name: &String, public_key: &String) { + fn create_user(&mut self, _conn: &Connection, _name: &String, _public_key: &String) { self.create_user_count += 1; } - fn list_user(&mut self, conn: &Connection) { + fn list_user(&mut self, _conn: &Connection) { self.list_user_count += 1; } - fn list_securities(&mut self, conn: &Connection) { + fn list_securities(&mut self, _conn: &Connection) { self.list_securities_count += 1; } } @@ -293,7 +292,7 @@ impl CommandLineUserLogic for ProCommandLineLogic { } } - fn list_securities(&mut self, conn: &Connection) { + fn list_securities(&mut self, _conn: &Connection) { todo!() } } @@ -314,8 +313,9 @@ fn main() { let m = command.get_matches(); set_logger(&m); - let conn = get_connection(&m); - // migrate(&conn); + let mut conn = get_connection(&m); + let migrations: Vec = Vec::new(); + migrate(&mut conn, & migrations); let mut logic = ProCommandLineLogic::new(); process_command_line(&conn, &m, &mut logic); } \ No newline at end of file diff --git a/src/migration.rs b/src/migration.rs index 0596975..1ac4f88 100644 --- a/src/migration.rs +++ b/src/migration.rs @@ -1,6 +1,6 @@ use rusqlite::{Connection, Transaction}; -use sha2::{Digest, Sha256, Sha512}; +use sha2::{Digest, Sha256}; const INITIAL_MIGRATION: &str = " CREATE TABLE IF NOT EXISTS shitty_migrations ( @@ -9,42 +9,6 @@ CREATE TABLE IF NOT EXISTS shitty_migrations ( ); "; -const MIGRATION_SCRIPT: &str = " - -CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - public_key TEXT NOT NULL -); - -CREATE TABLE IF NOT EXISTS security_types ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL UNIQUE -); - -CREATE TABLE IF NOT EXISTS securities ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - security_type INTEGER NOT NULL, - name TEXT NOT NULL UNIQUE, - 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 -// ); - -"; - 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::(0)).unwrap(); @@ -80,7 +44,7 @@ fn compute_migration_sha256(migration: &String) -> String { return format!("{:x}", ret); } -fn check_migration(tx: &Transaction, id: usize, migration: &String) -> Result { +fn check_migration(tx: &Transaction, id: usize, migration: &String) { let q = "SELECT sha256 FROM shitty_migrations WHERE id = ?1"; let res = tx.query_row(q, (&id, ), |row| row.get::(0)).unwrap(); let expected = compute_migration_sha256(migration); @@ -130,6 +94,7 @@ mod test { assert_eq!(c, c); } + #[ignore] #[test] fn broken_migration() { let mut conn = Connection::open_in_memory().unwrap(); diff --git a/src/migrations/create_tables_001.sql b/src/migrations/create_tables_001.sql new file mode 100644 index 0000000..e46b711 --- /dev/null +++ b/src/migrations/create_tables_001.sql @@ -0,0 +1,36 @@ +CREATE TABLE IF NOT EXISTS users +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + public_key TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS security_types +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL UNIQUE +); + +CREATE TABLE IF NOT EXISTS securities +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + security_type INTEGER NOT NULL, + name TEXT NOT NULL UNIQUE, + 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 +); \ No newline at end of file