Don't create database conn pool when running mitractl

This commit is contained in:
silverpill 2022-01-05 22:52:17 +00:00
parent 81d60656e6
commit 040934da2d
4 changed files with 23 additions and 12 deletions

View file

@ -2,7 +2,7 @@ use clap::Clap;
use uuid::Uuid; use uuid::Uuid;
use mitra::config; 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::database::migrate::apply_migrations;
use mitra::ethereum::utils::generate_ethereum_address; use mitra::ethereum::utils::generate_ethereum_address;
use mitra::logger::configure_logger; use mitra::logger::configure_logger;
@ -87,9 +87,9 @@ async fn main() {
// Other commands require initialized app // Other commands require initialized app
let config = config::parse_config(); let config = config::parse_config();
configure_logger(config.log_level); configure_logger(config.log_level);
let db_pool = create_pool(&config.database_url); let db_config = config.database_url.parse().unwrap();
apply_migrations(&db_pool).await; let db_client = &mut create_database_client(&db_config).await;
let db_client = &mut **get_database_client(&db_pool).await.unwrap(); apply_migrations(db_client).await;
match subcmd { match subcmd {
SubCommand::GenerateInviteCode(_) => { SubCommand::GenerateInviteCode(_) => {

View file

@ -1,16 +1,13 @@
use crate::database::Pool; use tokio_postgres::Client;
mod embedded { mod embedded {
use refinery::embed_migrations; use refinery::embed_migrations;
embed_migrations!("migrations"); embed_migrations!("migrations");
} }
pub async fn apply_migrations(pool: &Pool) { pub async fn apply_migrations(db_client: &mut Client) {
// https://github.com/rust-db/refinery/issues/105
let mut client_object = pool.get().await.unwrap();
let client = &mut *(*client_object);
let migration_report = embedded::migrations::runner() let migration_report = embedded::migrations::runner()
.run_async(client) .run_async(db_client)
.await.unwrap(); .await.unwrap();
for migration in migration_report.applied_migrations() { for migration in migration_report.applied_migrations() {

View file

@ -1,8 +1,21 @@
use tokio_postgres::config::{Config as DbConfig};
pub mod int_enum; pub mod int_enum;
pub mod migrate; pub mod migrate;
pub type Pool = deadpool_postgres::Pool; 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 { pub fn create_pool(database_url: &str) -> Pool {
deadpool_postgres::Pool::new( deadpool_postgres::Pool::new(
deadpool_postgres::Manager::new( deadpool_postgres::Manager::new(

View file

@ -7,7 +7,7 @@ use actix_web::{
use mitra::activitypub::views as activitypub; use mitra::activitypub::views as activitypub;
use mitra::config::{Environment, parse_config}; 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::database::migrate::apply_migrations;
use mitra::logger::configure_logger; use mitra::logger::configure_logger;
use mitra::mastodon_api::accounts::views::account_api_scope; use mitra::mastodon_api::accounts::views::account_api_scope;
@ -32,7 +32,8 @@ async fn main() -> std::io::Result<()> {
let config = parse_config(); let config = parse_config();
configure_logger(config.log_level); configure_logger(config.log_level);
let db_pool = create_pool(&config.database_url); 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() { if !config.media_dir().exists() {
std::fs::create_dir(config.media_dir()) std::fs::create_dir(config.media_dir())
.expect("failed to created media directory"); .expect("failed to created media directory");