feat: switch to proper config file
This commit is contained in:
parent
e17269ac3b
commit
97e42588ed
7 changed files with 158 additions and 102 deletions
|
|
@ -2,10 +2,12 @@ pub mod entities;
|
|||
mod migrator;
|
||||
pub mod query;
|
||||
|
||||
use crate::config::DbConfig;
|
||||
|
||||
pub use entities::{prelude::*, *};
|
||||
pub use migrator::Migrator;
|
||||
|
||||
use sea_orm::{DeriveActiveEnum, EnumIter};
|
||||
use sea_orm::{ConnectionTrait, Database, DbConn, DeriveActiveEnum, EnumIter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
type Result<T> = std::result::Result<T, sea_orm::DbErr>;
|
||||
|
|
@ -50,3 +52,50 @@ pub struct FullPackage {
|
|||
related: Vec<(PackageRelatedEnum, String)>,
|
||||
files: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn connect(conn: &DbConfig) -> crate::Result<DbConn> {
|
||||
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 != "" {
|
||||
url = format!("{url}?currentSchema={schema}");
|
||||
}
|
||||
|
||||
let options = sea_orm::ConnectOptions::new(url)
|
||||
.max_connections(*max_connections)
|
||||
.to_owned();
|
||||
Ok(Database::connect(options).await?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue