Files
AstralRinth/src/database/postgres_database.rs
T
Redblueflame efa8d5c575 Added monitoring, limited concurent connections (#245)
* reduced the default, and added environment override.

* Using parse is more stable and doesn't fail CI this time :P

* Added support for monitoring
This support is currently basic, but it can be improved later down the road.

* Forgot scheduler file

* Added health check

* Cargo fix

* Update cargo.lock to avoid action fails.

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2021-09-20 21:26:16 -07:00

66 lines
1.9 KiB
Rust

use log::info;
use sqlx::migrate::{Migrate, MigrateDatabase, Migrator};
use sqlx::postgres::{PgPool, PgPoolOptions};
use sqlx::{Connection, PgConnection, Postgres};
use std::path::Path;
const MIGRATION_FOLDER: &str = "migrations";
pub async fn connect() -> Result<PgPool, sqlx::Error> {
info!("Initializing database connection");
let database_url = dotenv::var("DATABASE_URL").expect("`DATABASE_URL` not in .env");
let pool = PgPoolOptions::new()
.min_connections(
dotenv::var("DATABASE_MIN_CONNECTIONS")
.ok()
.map(|x| x.parse::<u32>().ok())
.flatten()
.unwrap_or(16),
)
.max_connections(
dotenv::var("DATABASE_MAX_CONNECTIONS")
.ok()
.map(|x| x.parse::<u32>().ok())
.flatten()
.unwrap_or(16),
)
.connect(&database_url)
.await?;
Ok(pool)
}
pub async fn check_for_migrations() -> Result<(), sqlx::Error> {
let uri = &*dotenv::var("DATABASE_URL").expect("`DATABASE_URL` not in .env");
if !Postgres::database_exists(uri).await? {
info!("Creating database...");
Postgres::create_database(uri).await?;
}
info!("Applying migrations...");
run_migrations(uri).await?;
Ok(())
}
pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> {
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
let mut conn: PgConnection = PgConnection::connect(uri).await?;
conn.ensure_migrations_table().await?;
let (version, dirty) = conn.version().await?.unwrap_or((0, false));
if dirty {
panic!("The database is dirty ! Please check your database status.");
}
for migration in migrator.iter() {
if migration.version > version {
let _elapsed = conn.apply(migration).await?;
} else {
conn.validate(migration).await?;
}
}
Ok(())
}