use sqlx::migrate::Migrator;
use sqlx::pool::PoolConnection;
use sqlx::postgres::{PgConnection, Postgres};
use sqlx::Executor;
use sqlx::Row;
use std::path::Path;
#[sqlx::test(migrations = false)]
async fn simple(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_simple")).await?;
migrator.run(&mut conn).await?;
let res: String = conn
.fetch_one("SELECT some_payload FROM migrations_simple_test")
.await?
.get(0);
assert_eq!(res, "110_suffix");
migrator.run(&mut conn).await?;
Ok(())
}
#[sqlx::test(migrations = false)]
async fn reversible(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_reversible")).await?;
migrator.run(&mut conn).await?;
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 101);
migrator.undo(&mut conn, 20220721125033).await?;
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 101);
migrator.undo(&mut conn, 20220721124650).await?;
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 100);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn skip(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_reversible")).await?;
let sql = include_str!("migrations_reversible/20220721124650_add_table.up.sql");
let statements: Vec<&str> = sql.split(';').filter(|s| !s.trim().is_empty()).collect();
for statement in statements {
conn.execute(statement).await?;
}
migrator.skip(&mut conn, Some(20220721124650)).await?;
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 100);
migrator.run(&mut conn).await?;
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 101);
migrator.undo(&mut conn, 20220721124650).await?;
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 100);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn no_tx(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_no_tx")).await?;
migrator.run(&mut conn).await?;
let res: String = conn
.fetch_one("SELECT datname FROM pg_database WHERE datname = 'test_db'")
.await?
.get(0);
assert_eq!(res, "test_db");
Ok(())
}
async fn clean_up(conn: &mut PgConnection) -> anyhow::Result<()> {
conn.execute("DROP DATABASE IF EXISTS test_db").await.ok();
conn.execute("DROP TABLE migrations_simple_test").await.ok();
conn.execute("DROP TABLE migrations_reversible_test")
.await
.ok();
conn.execute("DROP TABLE _sqlx_migrations").await.ok();
Ok(())
}