pub mod entities; mod migrator; pub mod query; use crate::config::DbConfig; pub use entities::{prelude::*, *}; pub use migrator::Migrator; use sea_orm::{ConnectionTrait, Database, DbConn, DeriveActiveEnum, EnumIter}; use serde::{Deserialize, Serialize}; type Result = std::result::Result; #[derive(EnumIter, DeriveActiveEnum, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] #[sea_orm(rs_type = "i32", db_type = "Integer")] #[serde(rename_all = "lowercase")] pub enum PackageRelatedEnum { #[sea_orm(num_value = 0)] Conflicts, #[sea_orm(num_value = 1)] Replaces, #[sea_orm(num_value = 2)] Provides, #[sea_orm(num_value = 3)] Depend, #[sea_orm(num_value = 4)] Makedepend, #[sea_orm(num_value = 5)] Checkdepend, #[sea_orm(num_value = 6)] Optdepend, } #[derive(EnumIter, DeriveActiveEnum, Deserialize, Serialize, PartialEq, Eq, Clone, Debug)] #[sea_orm(rs_type = "i32", db_type = "Integer")] pub enum PackageState { #[sea_orm(num_value = 0)] PendingCommit, #[sea_orm(num_value = 1)] Committed, #[sea_orm(num_value = 2)] PendingDeletion, } #[derive(Serialize)] pub struct FullPackage { #[serde(flatten)] entry: package::Model, licenses: Vec, groups: Vec, related: Vec<(PackageRelatedEnum, String)>, files: Vec, } pub async fn connect(conn: &DbConfig) -> crate::Result { match conn { DbConfig::Sqlite { db_dir, max_connections, } => { let url = format!( "sqlite://{}?mode=rwc", db_dir.join("rieter.sqlite").to_string_lossy() ); let options = sea_orm::ConnectOptions::new(url) .max_connections(*max_connections) .to_owned(); let conn = Database::connect(options).await?; // synchronous=NORMAL still ensures database consistency with WAL mode, as per the docs // https://www.sqlite.org/pragma.html#pragma_synchronous conn.execute_unprepared("PRAGMA journal_mode=WAL;").await?; conn.execute_unprepared("PRAGMA synchronous=NORMAL;") .await?; Ok(conn) } DbConfig::Postgres { host, port, db, user, password, schema, max_connections, } => { let mut url = format!("postgres://{}:{}@{}:{}/{}", user, password, host, port, db); if !schema.is_empty() { url = format!("{url}?currentSchema={schema}"); } let options = sea_orm::ConnectOptions::new(url) .max_connections(*max_connections) .to_owned(); Ok(Database::connect(options).await?) } } }