mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-14 00:07:05 +00:00
5e56da87e0
* fix: ensure migration progress is not lost for PG Fixes #1966. * fix: ensure migration progress is not lost for sqlite This is similar to #1966. * fix: ensure reverse migration progress is not lost for PG See #1966. * fix: ensure reverse migration progress is not lost for sqlite See #1966. * fix: ensure migration progress is not lost for mysql This is similar to #1966. * fix: ensure reverse migration progress is not lost for mysql See #1966. * test: check migration type as well * test: extend migrations testing * fix: work around MySQL implicit commits * refactor: simplify migration testing
78 lines
2 KiB
Rust
78 lines
2 KiB
Rust
use sqlx::migrate::Migrator;
|
|
use sqlx::mysql::{MySql, MySqlConnection};
|
|
use sqlx::pool::PoolConnection;
|
|
use sqlx::Executor;
|
|
use sqlx::Row;
|
|
use std::path::Path;
|
|
|
|
#[sqlx::test(migrations = false)]
|
|
async fn simple(mut conn: PoolConnection<MySql>) -> anyhow::Result<()> {
|
|
clean_up(&mut conn).await?;
|
|
|
|
let migrator = Migrator::new(Path::new("tests/mysql/migrations_simple")).await?;
|
|
|
|
// run migration
|
|
migrator.run(&mut conn).await?;
|
|
|
|
// check outcome
|
|
let res: String = conn
|
|
.fetch_one("SELECT some_payload FROM migrations_simple_test")
|
|
.await?
|
|
.get(0);
|
|
assert_eq!(res, "110_suffix");
|
|
|
|
// running it a 2nd time should still work
|
|
migrator.run(&mut conn).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx::test(migrations = false)]
|
|
async fn reversible(mut conn: PoolConnection<MySql>) -> anyhow::Result<()> {
|
|
clean_up(&mut conn).await?;
|
|
|
|
let migrator = Migrator::new(Path::new("tests/mysql/migrations_reversible")).await?;
|
|
|
|
// run migration
|
|
migrator.run(&mut conn).await?;
|
|
|
|
// check outcome
|
|
let res: i64 = conn
|
|
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
|
|
.await?
|
|
.get(0);
|
|
assert_eq!(res, 101);
|
|
|
|
// roll back nothing (last version)
|
|
migrator.undo(&mut conn, 20220721125033).await?;
|
|
|
|
// check outcome
|
|
let res: i64 = conn
|
|
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
|
|
.await?
|
|
.get(0);
|
|
assert_eq!(res, 101);
|
|
|
|
// roll back one version
|
|
migrator.undo(&mut conn, 20220721124650).await?;
|
|
|
|
// check outcome
|
|
let res: i64 = conn
|
|
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
|
|
.await?
|
|
.get(0);
|
|
assert_eq!(res, 100);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Ensure that we have a clean initial state.
|
|
async fn clean_up(conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
|
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(())
|
|
}
|