multidb
This commit is contained in:
+14
-6
@@ -1,14 +1,16 @@
|
||||
mod api;
|
||||
mod app;
|
||||
mod config;
|
||||
mod database;
|
||||
mod db;
|
||||
mod queries;
|
||||
mod state;
|
||||
mod websocket;
|
||||
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use config::Config;
|
||||
use sqlx::sqlite::SqlitePoolOptions;
|
||||
use database::{Database, DatabaseKind};
|
||||
use state::AppState;
|
||||
use tokio::net::TcpListener;
|
||||
use tracing::info;
|
||||
@@ -23,11 +25,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(path) = config.database_url.strip_prefix("sqlite://").and_then(|v| v.split('?').next()) {
|
||||
if let Some(parent) = std::path::Path::new(path).parent() { std::fs::create_dir_all(parent)?; }
|
||||
}
|
||||
let db = SqlitePoolOptions::new()
|
||||
.max_connections(config.database_max_connections)
|
||||
.connect(&config.database_url)
|
||||
.await?;
|
||||
sqlx::migrate!().run(&db).await?;
|
||||
let db = Database::connect(&config.database_url, config.database_max_connections).await?;
|
||||
run_migrations(&db).await?;
|
||||
|
||||
std::fs::create_dir_all(&config.files_dir)?;
|
||||
let state = Arc::new(AppState::new(
|
||||
@@ -78,3 +77,12 @@ async fn shutdown_signal() {
|
||||
let terminate = std::future::pending::<()>();
|
||||
tokio::select! { () = ctrl_c => {}, () = terminate => {} }
|
||||
}
|
||||
|
||||
async fn run_migrations(db: &Database) -> Result<(), sqlx::migrate::MigrateError> {
|
||||
let path = match db.kind() {
|
||||
DatabaseKind::Sqlite => std::path::Path::new("migrations/sqlite"),
|
||||
DatabaseKind::Postgres => std::path::Path::new("migrations/postgres"),
|
||||
DatabaseKind::MySql => std::path::Path::new("migrations/mysql"),
|
||||
};
|
||||
sqlx::migrate::Migrator::new(path).await?.run(db.pool()).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user