Creating a DB handle.
This commit is contained in:
parent
50458d9f6e
commit
1928d4fb0f
14
migrations/01_init.sql
Normal file
14
migrations/01_init.sql
Normal 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
19
src/db/init.rs
Normal 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
3
src/db/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
mod init;
|
||||||
|
|
||||||
|
pub(crate) use init::DbHandle;
|
11
src/main.rs
11
src/main.rs
@ -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(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user