Creating a DB handle.

This commit is contained in:
Tom Alexander 2024-05-05 00:48:04 -04:00
parent 50458d9f6e
commit 1928d4fb0f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 45 additions and 2 deletions

14
migrations/01_init.sql Normal file
View File

@ -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)
);

19
src/db/init.rs Normal file
View File

@ -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<P: AsRef<Path>>(db_path: P) -> Result<DbHandle, Box<dyn std::error::Error>> {
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 })
}
}

3
src/db/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod init;
pub(crate) use init::DbHandle;

View File

@ -1,3 +1,10 @@
fn main() { mod db;
println!("Hello, world!");
fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
} }