From 39b6202b503512cdab88d27e702eb269b6040bdc Mon Sep 17 00:00:00 2001 From: piquel Date: Fri, 23 Oct 2026 01:03:37 +0200 Subject: [PATCH] fix(database): reject duplicate migration numbers Two branches that each add the next migration under different file names merge without a git conflict. sqlx accepts both, and every database test then fails with an unrelated-looking VersionMismatch. A unit test over the embedded migrator now requires versions to count up from 1 without gaps or repeats, and names the migration to renumber. Co-Authored-By: Claude Opus 5.5 --- crates/database/src/connection/mod.rs | 6 +++++- crates/database/src/connection/tests.rs | 16 +++++++++++++++- docs/architecture/database-crate.md | 4 +++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/database/src/connection/mod.rs b/crates/database/src/connection/mod.rs index d66055779..241c37152 100644 --- a/crates/database/src/connection/mod.rs +++ b/crates/database/src/connection/mod.rs @@ -3,6 +3,7 @@ use crate::{DatabaseConnectionError, DatabaseError, DatabaseTransaction}; use sqlx::{ PgPool, + migrate::Migrator, postgres::{PgConnectOptions, PgPoolOptions, PgSslMode}, }; use std::{str::FromStr, time::Duration}; @@ -23,6 +24,9 @@ const HOST_ADDRESS_PARAMETER: &str = "hostaddr"; /// Socket connections require this explicit local URL authority. const SOCKET_HOST: &str = "localhost"; +/// The schema migrations embedded at compile time, in version order. +static MIGRATOR: Migrator = sqlx::migrate!("./migrations"); + /// Readiness must fail quickly when the database stops responding. const HEALTH_CHECK_TIMEOUT: Duration = Duration::from_secs(2); @@ -199,7 +203,7 @@ impl Database { /// history differs from the embedded migrations, or another [`DatabaseError`] /// if filling in data fails. pub async fn run_migrations(&self) -> Result<(), DatabaseError> { - sqlx::migrate!("./migrations") + MIGRATOR .run(&self.pool) .await .map_err(|error| DatabaseError::Operation(Box::new(error)))?; diff --git a/crates/database/src/connection/tests.rs b/crates/database/src/connection/tests.rs index fbebc165e..2bd6703cb 100644 --- a/crates/database/src/connection/tests.rs +++ b/crates/database/src/connection/tests.rs @@ -1,6 +1,6 @@ //! Focused persistence and security contract tests. -use super::{Database, DatabaseConfig}; +use super::{Database, DatabaseConfig, MIGRATOR}; use sqlx::postgres::PgSslMode; use std::time::Duration; /// TCP requires verified TLS unless plaintext is explicitly selected. @@ -263,3 +263,17 @@ fn configuration_errors_name_the_constraint_and_preserve_parsers() { .expect_err("verified transport"); assert!(!error.to_string().contains("secret")); } + +/// Migration versions count up from 1 with no gaps or repeats. Two branches +/// that each add the next migration under different names fail here once the +/// second is rebased, instead of failing every database test at runtime. +#[test] +fn migration_versions_are_unique_and_contiguous() { + for (expected, migration) in (1..).zip(MIGRATOR.iter()) { + assert_eq!( + migration.version, expected, + "migration {} ({}) should be numbered {expected:04}; renumber it after the latest migration on main", + migration.version, migration.description, + ); + } +} diff --git a/docs/architecture/database-crate.md b/docs/architecture/database-crate.md index d1f28299f..3232e1363 100644 --- a/docs/architecture/database-crate.md +++ b/docs/architecture/database-crate.md @@ -102,7 +102,9 @@ Important invariants: The schema starts with the initial migration, `migrations/0001_initial.sql`. The prototype schema is disposable and the release requires a fresh database. Released -migrations are immutable; later schema changes go in new numbered files. Run +migrations are immutable; later schema changes go in new numbered files. Numbers +count up from `0001` without gaps or repeats, which a unit test enforces, so a +branch rebased past another migration must renumber its own. Run migrations with `just db migrate` (`auth-service migrate` in deployed images); startup does not silently mutate the schema. Migrations expand, then contract. A deploy runs `migrate` first, and the previous