1 parent
ecc201ef50
commit
698e3c4a1b
124 files changed
+4593
-2721
No files matched your search
@@ -6,6 +6,7 @@ use sqlx::{FromRow, MySqlPool, PgPool};
|
||||
|
||||
use super::{PersistedSession, SessionPersistence};
|
||||
use crate::config::AppConfig;
|
||||
use crate::db::pool::{DatabaseBackend, global_schema_for_backend};
|
||||
use crate::queries;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -49,21 +50,19 @@ impl TryFrom<DbSessionRow> for PersistedSession {
|
||||
password_ciphertext: row.password_ciphertext,
|
||||
user_hash: row.user_hash,
|
||||
imap_host: row.imap_host,
|
||||
imap_port: u16::try_from(row.imap_port)
|
||||
.map_err(|_| "invalid imap_port in session database")?,
|
||||
imap_port: u16::try_from(row.imap_port).map_err(|_| "invalid imap_port in database")?,
|
||||
imap_tls: row.imap_tls != 0,
|
||||
imap_tls_verify: row.imap_tls_verify != 0,
|
||||
smtp_host: row.smtp_host,
|
||||
smtp_port: u16::try_from(row.smtp_port)
|
||||
.map_err(|_| "invalid smtp_port in session database")?,
|
||||
smtp_port: u16::try_from(row.smtp_port).map_err(|_| "invalid smtp_port in database")?,
|
||||
smtp_tls: row.smtp_tls != 0,
|
||||
smtp_tls_verify: row.smtp_tls_verify != 0,
|
||||
timeout_secs: u64::try_from(row.timeout_secs)
|
||||
.map_err(|_| "invalid timeout_secs in session database")?,
|
||||
.map_err(|_| "invalid timeout_secs in database")?,
|
||||
created_at_unix: u64::try_from(row.created_at_unix)
|
||||
.map_err(|_| "invalid created_at_unix in session database")?,
|
||||
.map_err(|_| "invalid created_at_unix in database")?,
|
||||
last_accessed_unix: u64::try_from(row.last_accessed_unix)
|
||||
.map_err(|_| "invalid last_accessed_unix in session database")?,
|
||||
.map_err(|_| "invalid last_accessed_unix in database")?,
|
||||
user_agent: row.user_agent,
|
||||
})
|
||||
}
|
||||
@@ -133,8 +132,8 @@ where
|
||||
F: FnOnce(Connection) -> Result<T, String> + Send + 'static,
|
||||
{
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let conn = Connection::open(path)
|
||||
.map_err(|e| format!("failed to open SQLite session database: {e}"))?;
|
||||
let conn =
|
||||
Connection::open(path).map_err(|e| format!("failed to open SQLite database: {e}"))?;
|
||||
conn.busy_timeout(std::time::Duration::from_secs(5))
|
||||
.map_err(|e| format!("failed to configure SQLite busy timeout: {e}"))?;
|
||||
operation(conn)
|
||||
@@ -154,9 +153,11 @@ impl DatabaseSessionStore {
|
||||
"sqlite" => {
|
||||
let path = sqlite_path(config)?;
|
||||
let migration_path = path.clone();
|
||||
sqlite_task(migration_path, |conn| {
|
||||
conn.execute_batch(include_str!("../../migrations/sqlite/V047__sessions.sql"))
|
||||
.map_err(|e| format!("SQLite session migration failed: {e}"))?;
|
||||
let schema = global_schema_for_backend(DatabaseBackend::Sqlite)?;
|
||||
sqlite_task(migration_path, move |conn| {
|
||||
conn.execute_batch(&schema).map_err(|e| {
|
||||
format!("SQLite database schema initialization failed: {e}")
|
||||
})?;
|
||||
Ok(())
|
||||
})
|
||||
.await?;
|
||||
@@ -169,11 +170,12 @@ impl DatabaseSessionStore {
|
||||
.ok_or("DATABASE_URL required for mysql")?;
|
||||
let pool = MySqlPool::connect(url)
|
||||
.await
|
||||
.map_err(|e| format!("failed to connect MySQL session database: {e}"))?;
|
||||
sqlx::raw_sql(include_str!("../../migrations/mysql/V047__sessions.sql"))
|
||||
.map_err(|e| format!("failed to connect MySQL database: {e}"))?;
|
||||
let schema = global_schema_for_backend(DatabaseBackend::Mysql)?;
|
||||
sqlx::raw_sql(&schema)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.map_err(|e| format!("MySQL session migration failed: {e}"))?;
|
||||
.map_err(|e| format!("MySQL database schema initialization failed: {e}"))?;
|
||||
Pool::Mysql(pool)
|
||||
}
|
||||
"pgsql" | "postgres" | "postgresql" => {
|
||||
@@ -183,11 +185,11 @@ impl DatabaseSessionStore {
|
||||
.ok_or("DATABASE_URL required for pgsql")?;
|
||||
let pool = PgPool::connect(url)
|
||||
.await
|
||||
.map_err(|e| format!("failed to connect PostgreSQL session database: {e}"))?;
|
||||
sqlx::raw_sql(include_str!("../../migrations/pgsql/V047__sessions.sql"))
|
||||
.execute(&pool)
|
||||
.await
|
||||
.map_err(|e| format!("PostgreSQL session migration failed: {e}"))?;
|
||||
.map_err(|e| format!("failed to connect PostgreSQL database: {e}"))?;
|
||||
let schema = global_schema_for_backend(DatabaseBackend::Pgsql)?;
|
||||
sqlx::raw_sql(&schema).execute(&pool).await.map_err(|e| {
|
||||
format!("PostgreSQL database schema initialization failed: {e}")
|
||||
})?;
|
||||
Pool::Pgsql(pool)
|
||||
}
|
||||
other => return Err(format!("unsupported database backend: {other}")),
|
||||
|
||||
Reference in new issue
Block a user