From 040934da2dcf3b3bd07e26e60035f166e23bc3cd Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 5 Jan 2022 22:52:17 +0000 Subject: [PATCH] Don't create database conn pool when running mitractl --- src/bin/mitractl.rs | 8 ++++---- src/database/migrate.rs | 9 +++------ src/database/mod.rs | 13 +++++++++++++ src/main.rs | 5 +++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/bin/mitractl.rs b/src/bin/mitractl.rs index 6ecf66a..76da6a2 100644 --- a/src/bin/mitractl.rs +++ b/src/bin/mitractl.rs @@ -2,7 +2,7 @@ use clap::Clap; use uuid::Uuid; use mitra::config; -use mitra::database::{create_pool, get_database_client}; +use mitra::database::create_database_client; use mitra::database::migrate::apply_migrations; use mitra::ethereum::utils::generate_ethereum_address; use mitra::logger::configure_logger; @@ -87,9 +87,9 @@ async fn main() { // Other commands require initialized app let config = config::parse_config(); configure_logger(config.log_level); - let db_pool = create_pool(&config.database_url); - apply_migrations(&db_pool).await; - let db_client = &mut **get_database_client(&db_pool).await.unwrap(); + let db_config = config.database_url.parse().unwrap(); + let db_client = &mut create_database_client(&db_config).await; + apply_migrations(db_client).await; match subcmd { SubCommand::GenerateInviteCode(_) => { diff --git a/src/database/migrate.rs b/src/database/migrate.rs index 49a803b..17175eb 100644 --- a/src/database/migrate.rs +++ b/src/database/migrate.rs @@ -1,16 +1,13 @@ -use crate::database::Pool; +use tokio_postgres::Client; mod embedded { use refinery::embed_migrations; embed_migrations!("migrations"); } -pub async fn apply_migrations(pool: &Pool) { - // https://github.com/rust-db/refinery/issues/105 - let mut client_object = pool.get().await.unwrap(); - let client = &mut *(*client_object); +pub async fn apply_migrations(db_client: &mut Client) { let migration_report = embedded::migrations::runner() - .run_async(client) + .run_async(db_client) .await.unwrap(); for migration in migration_report.applied_migrations() { diff --git a/src/database/mod.rs b/src/database/mod.rs index bf3620e..33b2e78 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,8 +1,21 @@ +use tokio_postgres::config::{Config as DbConfig}; + pub mod int_enum; pub mod migrate; pub type Pool = deadpool_postgres::Pool; +pub async fn create_database_client(db_config: &DbConfig) -> tokio_postgres::Client { + let (client, connection) = db_config.connect(tokio_postgres::NoTls) + .await.unwrap(); + tokio::spawn(async move { + if let Err(err) = connection.await { + log::error!("connection error: {}", err); + }; + }); + client +} + pub fn create_pool(database_url: &str) -> Pool { deadpool_postgres::Pool::new( deadpool_postgres::Manager::new( diff --git a/src/main.rs b/src/main.rs index 7746a1e..c8fe516 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use actix_web::{ use mitra::activitypub::views as activitypub; use mitra::config::{Environment, parse_config}; -use mitra::database::create_pool; +use mitra::database::{get_database_client, create_pool}; use mitra::database::migrate::apply_migrations; use mitra::logger::configure_logger; use mitra::mastodon_api::accounts::views::account_api_scope; @@ -32,7 +32,8 @@ async fn main() -> std::io::Result<()> { let config = parse_config(); configure_logger(config.log_level); let db_pool = create_pool(&config.database_url); - apply_migrations(&db_pool).await; + let db_client = &mut **get_database_client(&db_pool).await.unwrap(); + apply_migrations(db_client).await; if !config.media_dir().exists() { std::fs::create_dir(config.media_dir()) .expect("failed to created media directory");