cleanup code

This commit is contained in:
Mateusz Gruszczyński
2026-07-24 11:22:44 +02:00
parent f52cf91470
commit 33b4667e42
12 changed files with 2387 additions and 712 deletions
+21 -8
View File
@@ -1,5 +1,5 @@
use crate::queries;
use sqlx::{any::AnyPoolOptions, AnyPool};
use sqlx::{AnyPool, any::AnyPoolOptions};
use tracing::{debug, info};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -26,9 +26,15 @@ impl Database {
.await?;
if kind == DatabaseKind::Sqlite {
debug!("applying SQLite connection pragmas");
sqlx::query(queries::SQLITE_FOREIGN_KEYS_ON).execute(&pool).await?;
sqlx::query(queries::SQLITE_JOURNAL_WAL).execute(&pool).await?;
sqlx::query(queries::SQLITE_BUSY_TIMEOUT).execute(&pool).await?;
sqlx::query(queries::SQLITE_FOREIGN_KEYS_ON)
.execute(&pool)
.await?;
sqlx::query(queries::SQLITE_JOURNAL_WAL)
.execute(&pool)
.await?;
sqlx::query(queries::SQLITE_BUSY_TIMEOUT)
.execute(&pool)
.await?;
}
info!(?kind, max_connections, "database pool ready");
Ok(Self { pool, kind })
@@ -44,9 +50,16 @@ impl Database {
impl DatabaseKind {
fn from_url(url: &str) -> Result<Self, sqlx::Error> {
if url.starts_with("sqlite:") { Ok(Self::Sqlite) }
else if url.starts_with("postgres:") || url.starts_with("postgresql:") { Ok(Self::Postgres) }
else if url.starts_with("mysql:") { Ok(Self::MySql) }
else { Err(sqlx::Error::Configuration("DATABASE_URL must use sqlite://, postgres:// or mysql://".into())) }
if url.starts_with("sqlite:") {
Ok(Self::Sqlite)
} else if url.starts_with("postgres:") || url.starts_with("postgresql:") {
Ok(Self::Postgres)
} else if url.starts_with("mysql:") {
Ok(Self::MySql)
} else {
Err(sqlx::Error::Configuration(
"DATABASE_URL must use sqlite://, postgres:// or mysql://".into(),
))
}
}
}