Create Initial Database Schema for Tasks. (#1)
Create migration scripts for this and initial Task DB Model in rust along with unit tests. Reviewed-on: #1 Co-authored-by: Drew Galbraith <drew@tiramisu.one> Co-committed-by: Drew Galbraith <drew@tiramisu.one>
This commit is contained in:
parent
c2b7c12905
commit
82d524a62f
13 changed files with 1923 additions and 15 deletions
45
backend/src/database/connection.rs
Normal file
45
backend/src/database/connection.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use anyhow::Result;
|
||||
use sqlx::{SqlitePool, sqlite::SqliteConnectOptions};
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Database configuration
|
||||
pub struct DatabaseConfig {
|
||||
pub database_url: String,
|
||||
}
|
||||
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
database_url: "sqlite:local.db".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a SQLx connection pool
|
||||
pub async fn create_pool(config: &DatabaseConfig) -> Result<SqlitePool> {
|
||||
let options = SqliteConnectOptions::from_str(&config.database_url)?.create_if_missing(true);
|
||||
|
||||
let pool = SqlitePool::connect_with(options).await?;
|
||||
|
||||
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
/// Initialize database with connection pool
|
||||
pub async fn initialize_database() -> Result<SqlitePool> {
|
||||
let config = DatabaseConfig::default();
|
||||
create_pool(&config).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub async fn create_test_pool() -> Result<SqlitePool> {
|
||||
let options = SqliteConnectOptions::from_str("sqlite::memory:")?.create_if_missing(true);
|
||||
|
||||
let pool = SqlitePool::connect_with(options).await?;
|
||||
|
||||
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue