cleanup code
This commit is contained in:
+21
-8
@@ -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(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user