From 1928d4fb0fe9db2759770c17586aef0e1d7acba3 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 5 May 2024 00:48:04 -0400 Subject: [PATCH] Creating a DB handle. --- migrations/01_init.sql | 14 ++++++++++++++ src/db/init.rs | 19 +++++++++++++++++++ src/db/mod.rs | 3 +++ src/main.rs | 11 +++++++++-- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 migrations/01_init.sql create mode 100644 src/db/init.rs create mode 100644 src/db/mod.rs diff --git a/migrations/01_init.sql b/migrations/01_init.sql new file mode 100644 index 0000000..c0c4a40 --- /dev/null +++ b/migrations/01_init.sql @@ -0,0 +1,14 @@ +PRAGMA foreign_keys = ON; + +CREATE TABLE IF NOT EXISTS global_actions ( + name TEXT NOT NULL PRIMARY KEY, + last_finished INTEGER DEFAULT 0 NOT NULL +); + +CREATE TABLE IF NOT EXISTS local_actions ( + jail_name TEXT NOT NULL, + tree TEXT NOT NULL, + set_name TEXT NOT NULL, + last_finished INTEGER DEFAULT 0 NOT NULL, + PRIMARY KEY (jail_name, tree, set_name) +); diff --git a/src/db/init.rs b/src/db/init.rs new file mode 100644 index 0000000..da78096 --- /dev/null +++ b/src/db/init.rs @@ -0,0 +1,19 @@ +use std::path::Path; + +use rusqlite::Connection; + +static DB_INIT_QUERY: &str = include_str!("../../migrations/01_init.sql"); + +pub struct DbHandle { + conn: Connection, +} + +impl DbHandle { + pub fn new>(db_path: P) -> Result> { + let mut conn: Connection = Connection::open(db_path).unwrap(); + let tx = conn.transaction()?; + tx.execute_batch(DB_INIT_QUERY).unwrap(); + tx.commit().unwrap(); + Ok(DbHandle { conn }) + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..1d2b5aa --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,3 @@ +mod init; + +pub(crate) use init::DbHandle; diff --git a/src/main.rs b/src/main.rs index e7a11a9..0082382 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ -fn main() { - println!("Hello, world!"); +mod db; + +fn main() -> Result<(), Box> { + let db_path = std::env::args() + .nth(1) + .ok_or("Pass path to sqlite DB in first parameter.")?; + let mut db_conn: db::DbHandle = db::DbHandle::new(db_path)?; + println!("Done."); + Ok(()) }