Something went wrong. Try again.
A simple to-do app focused on tasks that can be completed within a specific time span. taskline.tobinio.dev
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344use std::{fs, io, path::Path};
use migration::{Migrator, MigratorTrait};use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr};use thiserror::Error;
pub mod entities;
pub type OrmConnection = DatabaseConnection;
#[derive(Debug, Error)]pub enum OrmError { #[error("database error: {0}")] DbError(#[from] DbErr), #[error("unsupported database backend")] UnsupportedBackend, #[error("failed to create sqlite folder")] FailedToCreateFolder(#[from] io::Error),}
pub async fn init_db(url: &str) -> Result<OrmConnection, OrmError> { // If using SQLite, extract the file path and create parent directories if let Some(path_str) = url.strip_prefix("sqlite:") { let clean_path = path_str.split('?').next().unwrap_or(path_str);
let file_path = clean_path.strip_prefix("//").unwrap_or(clean_path);
if let Some(parent) = Path::new(file_path).parent() && !parent.as_os_str().is_empty() { fs::create_dir_all(parent).map_err(OrmError::FailedToCreateFolder)?; } }
let mut options = ConnectOptions::new(url); options.sqlx_logging(false);
let connection = Database::connect(options).await?;
Migrator::up(&connection, None).await?;
Ok(connection)}