Add ability to pass in custom database path.

This commit is contained in:
projectmoon 2020-12-31 14:02:14 +00:00
parent 62f05060c0
commit ce4e02594d
8 changed files with 37 additions and 13 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
/target /target
todo.org todo.org
*.sqlite *.sqlite*
*.sqlite.* *.sqlite.*

View File

@ -7,7 +7,7 @@ build = "build.rs"
default-run = "tenebrous" default-run = "tenebrous"
[[bin]] [[bin]]
name = "migrate" name = "tenebrous-migrate"
path = "src/migrate.rs" path = "src/migrate.rs"
[[bin]] [[bin]]

View File

@ -4,11 +4,13 @@ use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use sqlx::ConnectOptions; use sqlx::ConnectOptions;
use std::str::FromStr; use std::str::FromStr;
/// Type alias for the Rocket-managed singleton database connection.
pub type TenebrousDbConn<'a> = rocket::State<'a, SqlitePool>; pub type TenebrousDbConn<'a> = rocket::State<'a, SqlitePool>;
pub(crate) async fn create_pool() -> Result<SqlitePool, crate::errors::Error> { /// Create a connection pool to the database.
pub(crate) async fn create_pool(db_path: &str) -> Result<SqlitePool, crate::errors::Error> {
//Create database if missing. //Create database if missing.
let conn = SqliteConnectOptions::from_str("sqlite://tenebrous.sqlite")? let conn = SqliteConnectOptions::from_str(&format!("sqlite://{}", db_path))?
.create_if_missing(true) .create_if_missing(true)
.connect() .connect()
.await?; .await?;
@ -18,7 +20,7 @@ pub(crate) async fn create_pool() -> Result<SqlitePool, crate::errors::Error> {
//Return actual conncetion pool. //Return actual conncetion pool.
SqlitePoolOptions::new() SqlitePoolOptions::new()
.max_connections(5) .max_connections(5)
.connect("tenebrous.sqlite") .connect(db_path)
.await .await
.map_err(|e| e.into()) .map_err(|e| e.into())
} }

View File

@ -11,6 +11,7 @@ extern crate serde_derive;
use rocket_contrib::serve::StaticFiles; use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use std::env;
pub mod catchers; pub mod catchers;
pub mod db; pub mod db;
@ -21,7 +22,16 @@ pub mod routes;
#[rocket::main] #[rocket::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
migrator::migrate().await?; let args: Vec<String> = env::args().collect();
let db_path: &str = match &args[..] {
[_, path] => path.as_ref(),
[_, _, ..] => panic!("Expected exactly 0 or 1 argument"),
_ => "tenebrous.sqlite",
};
println!("Using database: {}", db_path);
migrator::migrate(db_path).await?;
let root_routes: Vec<rocket::Route> = { let root_routes: Vec<rocket::Route> = {
routes::root::routes() routes::root::routes()
@ -37,7 +47,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .attach(Template::fairing())
//.attach(db::TenebrousDbConn::fairing()) //.attach(db::TenebrousDbConn::fairing())
.manage(crate::db::create_pool().await?) .manage(crate::db::create_pool(db_path).await?)
.mount("/", root_routes) .mount("/", root_routes)
.mount("/characters", character_routes) .mount("/characters", character_routes)
.mount("/api", api_routes) .mount("/api", api_routes)

View File

@ -1,6 +1,17 @@
use std::env;
pub mod migrator; pub mod migrator;
#[rocket::main] #[rocket::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
crate::migrator::migrate().await let args: Vec<String> = env::args().collect();
let db_path: &str = match &args[..] {
[_, path] => path.as_ref(),
[_, _, ..] => panic!("Expected exactly 0 or 1 argument"),
_ => "tenebrous.sqlite",
};
println!("Using database: {}", db_path);
crate::migrator::migrate(db_path).await
} }

View File

@ -5,16 +5,17 @@ use std::str::FromStr;
pub mod migrations; pub mod migrations;
pub(crate) async fn migrate() -> Result<(), Box<dyn std::error::Error>> { /// Run database migrations against the sqlite database.
pub(crate) async fn migrate(db_path: &str) -> Result<(), Box<dyn std::error::Error>> {
//Create database if missing. //Create database if missing.
let conn = SqliteConnectOptions::from_str("sqlite://tenebrous.sqlite")? let conn = SqliteConnectOptions::from_str(&format!("sqlite://{}", db_path))?
.create_if_missing(true) .create_if_missing(true)
.connect() .connect()
.await?; .await?;
drop(conn); drop(conn);
let mut conn = Config::new(ConfigDbType::Sqlite).set_db_path("tenebrous.sqlite"); let mut conn = Config::new(ConfigDbType::Sqlite).set_db_path(db_path);
println!("Running migrations"); println!("Running migrations");
migrations::runner().run(&mut conn)?; migrations::runner().run(&mut conn)?;
Ok(()) Ok(())

View File

@ -3,7 +3,7 @@ use barrel::{types, Migration};
pub fn migration() -> String { pub fn migration() -> String {
let mut m = Migration::new(); let mut m = Migration::new();
println!("Applying: {}", file!()); println!("Applying migration: {}", file!());
m.create_table("users", |t| { m.create_table("users", |t| {
t.add_column("id", types::primary()); t.add_column("id", types::primary());

View File

@ -3,7 +3,7 @@ use barrel::{types, Migration};
pub fn migration() -> String { pub fn migration() -> String {
let mut m = Migration::new(); let mut m = Migration::new();
println!("Applying: {}", file!()); println!("Applying migration: {}", file!());
m.create_table("characters", move |t| { m.create_table("characters", move |t| {
let db_enum = r#"CHECK(data_type IN ('chronicles_of_darkness_v1', 'changeling_v1'))"#; let db_enum = r#"CHECK(data_type IN ('chronicles_of_darkness_v1', 'changeling_v1'))"#;