migratrom-postgres 0.2.0

PostgreSQL (sqlx) adapter for migratrom.
Documentation
//! End-to-end PostgreSQL tests.
//!
//! These require a reachable server and are `#[ignore]` by default. Run with:
//!
//! ```sh
//! DATABASE_URL=postgres://localhost/migratrom_test \
//!   cargo test -p migratrom-postgres -- --ignored
//! ```

use migratrom::operations::create_table;
use migratrom::{ApplyOptions, ColumnDef, Migration, PostgresDialect, PrimaryKey};
use migratrom_postgres::apply_migrations;
use sqlx::PgPool;

async fn pool() -> PgPool {
    let url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set for ignored tests");
    PgPool::connect(&url).await.expect("connect postgres")
}

fn users_migration(history_clean_table: &str) -> Migration {
    let dialect = PostgresDialect;
    let op = create_table(
        "public",
        history_clean_table,
        &[
            ColumnDef::new("id", "bigint"),
            ColumnDef::new("email", "text"),
        ],
        Some(&PrimaryKey {
            columns: vec!["id".into()],
        }),
        &dialect,
    )
    .unwrap();
    Migration::new(1, None, vec![op])
}

#[tokio::test]
#[ignore = "requires a running PostgreSQL server (DATABASE_URL)"]
async fn applies_and_is_idempotent() {
    let pool = pool().await;
    // Isolate from prior runs.
    sqlx::query("DROP TABLE IF EXISTS \"mt_e2e_users\"")
        .execute(&pool)
        .await
        .unwrap();
    sqlx::query("DROP TABLE IF EXISTS \"__migratron_history__\"")
        .execute(&pool)
        .await
        .unwrap();

    let opts = ApplyOptions::default();
    let first = apply_migrations(&[users_migration("mt_e2e_users")], &pool, &opts)
        .await
        .unwrap();
    assert_eq!(first.applied, vec![1]);

    let second = apply_migrations(&[users_migration("mt_e2e_users")], &pool, &opts)
        .await
        .unwrap();
    assert!(second.applied.is_empty());
}