Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/database/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::{DatabaseConnectionError, DatabaseError, DatabaseTransaction};
use sqlx::{
PgPool,
migrate::Migrator,
postgres::{PgConnectOptions, PgPoolOptions, PgSslMode},
};
use std::{str::FromStr, time::Duration};
Expand All @@ -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);

Expand Down Expand Up @@ -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)))?;
Expand Down
16 changes: 15 additions & 1 deletion crates/database/src/connection/tests.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
);
}
}
4 changes: 3 additions & 1 deletion docs/architecture/database-crate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading