mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-28 03:33:37 +00:00
* feat: create `sqlx.toml` format * feat: add support for ignored_chars config to sqlx_core::migrate * chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM) https://en.wikipedia.org/wiki/Byte_order_mark * refactor: make `Config` always compiled simplifies usage while still making parsing optional for less generated code * refactor: add origin information to `Column` * feat(macros): implement `type_override` and `column_override` from `sqlx.toml` * refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates` * feat: make macros aware of `macros.preferred-crates` * feat: make `sqlx-cli` aware of `database-url-var` * feat: teach macros about `migrate.table-name`, `migrations-dir` * feat: teach macros about `migrate.ignored-chars` * chore: delete unused source file `sqlx-cli/src/migration.rs` * feat: teach `sqlx-cli` about `migrate.defaults` * feat: teach `sqlx-cli` about `migrate.migrations-dir` * feat: teach `sqlx-cli` about `migrate.table-name` * feat: introduce `migrate.create-schemas` * WIP feat: create multi-tenant database example * fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared statements * feat: progress on axum-multi-tenant example * feat(config): better errors for mislabeled fields * WIP feat: filling out axum-multi-tenant example * feat: multi-tenant example No longer Axum-based because filling out the request routes would have distracted from the purpose of the example. * chore(ci): test multi-tenant example * fixup after merge * fix(ci): enable `sqlx-toml` in CLI build for examples * fix: CI, README for `multi-tenant` * fix: clippy warnings * fix: multi-tenant README * fix: sequential versioning inference for migrations * fix: migration versioning with explicit overrides * fix: only warn on ambiguous crates if the invocation relies on it * fix: remove unused imports * fix: doctest * fix: `sqlx mig add` behavior and tests * fix: restore original type-checking order * fix: deprecation warning in `tests/postgres/macros.rs` * feat: create postgres/multi-database example * fix: examples/postgres/multi-database * fix: cargo fmt * chore: add tests for config `migrate.defaults` * fix: sqlx-cli/tests/add.rs * feat(cli): add `--config` override to all relevant commands * chore: run `sqlx mig add` test with `RUST_BACKTRACE=1` * fix: properly canonicalize config path for `sqlx mig add` test * fix: get `sqlx mig add` test passing * fix(cli): test `migrate.ignored-chars`, fix bugs * feat: create `macros.preferred-crates` example * fix(examples): use workspace `sqlx` * fix: examples * fix(sqlite): unexpected feature flags in `type_checking.rs` * fix: run `cargo fmt` * fix: more example fixes * fix(ci): preferred-crates setup * fix(examples): enable default-features for workspace `sqlx` * fix(examples): issues in `preferred-crates` * chore: adjust error message for missing param type in `query!()` * doc: mention new `sqlx.toml` configuration * chore: add `CHANGELOG` entry Normally I generate these when cutting the release, but I wanted to take time to editorialize this one. * doc: fix new example titles * refactor: make `sqlx-toml` feature non-default, improve errors * refactor: eliminate panics in `Config` read path * chore: remove unused `axum` dependency from new examples * fix(config): restore fallback to default config for macros * chore(config): remove use of `once_cell` (to match `main`)
168 lines
5.8 KiB
Rust
168 lines
5.8 KiB
Rust
mod common;
|
|
|
|
use common::TestDatabase;
|
|
|
|
#[tokio::test]
|
|
async fn run_reversible_migrations() {
|
|
let all_migrations: Vec<i64> = vec![
|
|
20230101000000,
|
|
20230201000000,
|
|
20230301000000,
|
|
20230401000000,
|
|
20230501000000,
|
|
];
|
|
// Without --target-version specified.k
|
|
{
|
|
let db = TestDatabase::new("run_reversible_latest", "migrations_reversible");
|
|
db.run_migration(false, None, false).success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations);
|
|
}
|
|
// With --target-version specified.
|
|
{
|
|
let db = TestDatabase::new("run_reversible_latest_explicit", "migrations_reversible");
|
|
|
|
// Move to latest, explicitly specified.
|
|
db.run_migration(false, Some(20230501000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations);
|
|
|
|
// Move to latest when we're already at the latest.
|
|
db.run_migration(false, Some(20230501000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations);
|
|
|
|
// Upgrade to an old version.
|
|
db.run_migration(false, Some(20230301000000), false)
|
|
.failure();
|
|
assert_eq!(db.applied_migrations().await, all_migrations);
|
|
}
|
|
// With --target-version, incrementally upgrade.
|
|
{
|
|
let db = TestDatabase::new("run_reversible_incremental", "migrations_reversible");
|
|
|
|
// First version
|
|
db.run_migration(false, Some(20230101000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, vec![20230101000000]);
|
|
|
|
// Dry run upgrade to latest.
|
|
db.run_migration(false, None, true).success();
|
|
assert_eq!(db.applied_migrations().await, vec![20230101000000]);
|
|
|
|
// Dry run upgrade + 2
|
|
db.run_migration(false, Some(20230301000000), true)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, vec![20230101000000]);
|
|
|
|
// Upgrade to non-existent version.
|
|
db.run_migration(false, Some(20230901000000999), false)
|
|
.failure();
|
|
assert_eq!(db.applied_migrations().await, vec![20230101000000]);
|
|
|
|
// Upgrade + 1
|
|
db.run_migration(false, Some(20230201000000), false)
|
|
.success();
|
|
assert_eq!(
|
|
db.applied_migrations().await,
|
|
vec![20230101000000, 20230201000000]
|
|
);
|
|
|
|
// Upgrade + 2
|
|
db.run_migration(false, Some(20230401000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..4]);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn revert_migrations() {
|
|
let all_migrations: Vec<i64> = vec![
|
|
20230101000000,
|
|
20230201000000,
|
|
20230301000000,
|
|
20230401000000,
|
|
20230501000000,
|
|
];
|
|
|
|
// Without --target-version
|
|
{
|
|
let db = TestDatabase::new("revert_incremental", "migrations_reversible");
|
|
db.run_migration(false, None, false).success();
|
|
|
|
// Dry-run
|
|
db.run_migration(true, None, true).success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations);
|
|
|
|
// Downgrade one
|
|
db.run_migration(true, None, false).success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..4]);
|
|
|
|
// Downgrade one
|
|
db.run_migration(true, None, false).success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..3]);
|
|
}
|
|
// With --target-version
|
|
{
|
|
let db = TestDatabase::new("revert_incremental", "migrations_reversible");
|
|
db.run_migration(false, None, false).success();
|
|
|
|
// Dry-run downgrade to version 3.
|
|
db.run_migration(true, Some(20230301000000), true).success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations);
|
|
|
|
// Downgrade to version 3.
|
|
db.run_migration(true, Some(20230301000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..3]);
|
|
|
|
// Try downgrading to the same version.
|
|
db.run_migration(true, Some(20230301000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..3]);
|
|
|
|
// Try downgrading to a newer version.
|
|
db.run_migration(true, Some(20230401000000), false)
|
|
.failure();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..3]);
|
|
|
|
// Try downgrading to a non-existent version.
|
|
db.run_migration(true, Some(9999), false).failure();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..3]);
|
|
|
|
// Ensure we can still upgrade
|
|
db.run_migration(false, Some(20230401000000), false)
|
|
.success();
|
|
assert_eq!(db.applied_migrations().await, all_migrations[..4]);
|
|
|
|
// Downgrade to zero.
|
|
db.run_migration(true, Some(0), false).success();
|
|
assert_eq!(db.applied_migrations().await, Vec::<i64>::new());
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn ignored_chars() {
|
|
let mut db = TestDatabase::new("ignored-chars", "ignored-chars/LF");
|
|
db.config_path = Some("tests/ignored-chars/sqlx.toml".into());
|
|
|
|
db.run_migration(false, None, false).success();
|
|
|
|
db.set_migrations("ignored-chars/CRLF");
|
|
|
|
let expected_info = "1/installed user\n2/installed post\n3/installed comment\n";
|
|
|
|
// `ignored-chars` should produce the same migration checksum here
|
|
db.migrate_info().success().stdout(expected_info);
|
|
|
|
// Running migration should be a no-op
|
|
db.run_migration(false, None, false).success().stdout("");
|
|
|
|
db.set_migrations("ignored-chars/BOM");
|
|
db.migrate_info().success().stdout(expected_info);
|
|
db.run_migration(false, None, false).success().stdout("");
|
|
|
|
db.set_migrations("ignored-chars/oops-all-tabs");
|
|
db.migrate_info().success().stdout(expected_info);
|
|
db.run_migration(false, None, false).success().stdout("");
|
|
}
|