You've already forked AstralRinth
54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
use log::{info, debug};
|
|
use sqlx::postgres::{PgPool, PgPoolOptions};
|
|
use sqlx::migrate::{Migrator, Migrate, MigrateDatabase};
|
|
use std::path::Path;
|
|
use sqlx::{PgConnection, Connection, Postgres};
|
|
|
|
const MIGRATION_FOLDER: &'static 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()
|
|
.max_connections(20)
|
|
.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(())
|
|
} |