fix: use explicit SqliteConnectOptions with create_if_missing(true) to fix database connection issues in some environments
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use sqlx::SqlitePool;
|
||||
use sqlx::sqlite::{SqlitePool, SqliteConnectOptions};
|
||||
use std::str::FromStr;
|
||||
use tracing::info;
|
||||
|
||||
use crate::config::DatabaseConfig;
|
||||
@@ -9,13 +10,18 @@ pub type DbPool = SqlitePool;
|
||||
pub async fn init(config: &DatabaseConfig) -> Result<DbPool> {
|
||||
// Ensure the database directory exists
|
||||
if let Some(parent) = config.path.parent() {
|
||||
if !parent.as_os_str().is_empty() {
|
||||
tokio::fs::create_dir_all(parent).await?;
|
||||
}
|
||||
}
|
||||
|
||||
let database_url = format!("sqlite:{}", config.path.display());
|
||||
info!("Connecting to database at {}", database_url);
|
||||
let database_path = config.path.to_string_lossy().to_string();
|
||||
info!("Connecting to database at {}", database_path);
|
||||
|
||||
let pool = SqlitePool::connect(&database_url).await?;
|
||||
let options = SqliteConnectOptions::from_str(&format!("sqlite:{}", database_path))?
|
||||
.create_if_missing(true);
|
||||
|
||||
let pool = SqlitePool::connect_with(options).await?;
|
||||
|
||||
// Run migrations
|
||||
run_migrations(&pool).await?;
|
||||
|
||||
Reference in New Issue
Block a user