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
todo.org
*.sqlite
*.sqlite*
*.sqlite.*

View File

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

View File

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

View File

@ -11,6 +11,7 @@ extern crate serde_derive;
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
use std::env;
pub mod catchers;
pub mod db;
@ -21,7 +22,16 @@ pub mod routes;
#[rocket::main]
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> = {
routes::root::routes()
@ -37,7 +47,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
rocket::ignite()
.attach(Template::fairing())
//.attach(db::TenebrousDbConn::fairing())
.manage(crate::db::create_pool().await?)
.manage(crate::db::create_pool(db_path).await?)
.mount("/", root_routes)
.mount("/characters", character_routes)
.mount("/api", api_routes)

View File

@ -1,6 +1,17 @@
use std::env;
pub mod migrator;
#[rocket::main]
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(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.
let conn = SqliteConnectOptions::from_str("sqlite://tenebrous.sqlite")?
let conn = SqliteConnectOptions::from_str(&format!("sqlite://{}", db_path))?
.create_if_missing(true)
.connect()
.await?;
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");
migrations::runner().run(&mut conn)?;
Ok(())

View File

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

View File

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