use std::path::Path;
use log::{debug, info};
use rusqlite::Connection;
use crate::persistence::SqliteSetupArgs;
pub fn initialize_db(args: &SqliteSetupArgs) -> Result<Connection, String> {
let conn = Connection::open(Path::new(&args.output)).unwrap();
debug!("PRAGMA journal_mode = WAL; -- Write-Ahead Logging for better concurrency");
conn.execute_batch("PRAGMA journal_mode = WAL;").unwrap(); debug!("PRAGMA synchronous = NORMAL; -- Faster writes with reasonable safety");
conn.execute("PRAGMA synchronous = NORMAL;", []).unwrap(); debug!("PRAGMA cache_size = -2000000; -- Bigger cache size");
conn.execute("PRAGMA cache_size = -2000000;", []).unwrap(); debug!("PRAGMA temp_store = MEMORY; -- Store any temp tables in memory");
conn.execute("PRAGMA temp_store = MEMORY;", []).unwrap(); debug!("PRAGMA auto_vacuum = FULL; -- Shrink databases with a delete");
conn.execute("PRAGMA auto_vacuum = FULL;", []).unwrap();
for statement in args.create_table_sql().iter() {
info!("Creating table {statement}");
conn.execute(statement, []).unwrap();
}
Ok(conn)
}