From cedd052a4da08b7a23ab0ee3f9838d408eff8bd6 Mon Sep 17 00:00:00 2001 From: qpismont Date: Wed, 14 Oct 2026 16:50:02 +0000 Subject: [PATCH] same migration process test and prod --- .woodpecker/tests.yml | 1 + README.md | 2 +- crates/steep-server/src/db.rs | 37 +++-------------------------------- 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/.woodpecker/tests.yml b/.woodpecker/tests.yml index f05e763..044829e 100644 --- a/.woodpecker/tests.yml +++ b/.woodpecker/tests.yml @@ -39,6 +39,7 @@ steps: environment: DATABASE_URL: postgresql://postgres:postgres@postgres:5432/steep commands: + - bash scripts/migrate-apply.sh - cargo test --workspace --all-targets -- --include-ignored - name: container-build diff --git a/README.md b/README.md index 44cd51a..aa7a657 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ After changing a model in `crates/steep-server/src/schemas/`, generate a migrati ./scripts/migrate-generate.sh describe_change ``` -Apply the pending migrations **before starting the service** — the server does not migrate at boot: +Apply the pending migrations **before starting the service** — the server does not migrate at boot. The tests need the same schema: run this before `cargo test` too (CI does it as well): ```sh ./scripts/migrate-apply.sh diff --git a/crates/steep-server/src/db.rs b/crates/steep-server/src/db.rs index fd38ed2..5954319 100644 --- a/crates/steep-server/src/db.rs +++ b/crates/steep-server/src/db.rs @@ -9,43 +9,12 @@ pub async fn open(url: &str) -> toasty::Result { /// A connection for a test, on the database `DATABASE_URL` points to. /// -/// Every `#[tokio::test]` runs on its own runtime, and the connection tasks live on -/// the runtime that opened the pool: sharing one `Db` between tests would leave -/// them with a pool whose runtime is gone. Each test therefore opens its own, and -/// only the migrations are applied once. +/// The schema must already be migrated: `scripts/migrate-apply.sh` does it, the same +/// way it does for the server. Each test opens its own connection, because the +/// connection tasks live on the runtime of the test that opened them. #[cfg(test)] pub(crate) async fn test_connect() -> Db { - static MIGRATED: tokio::sync::OnceCell<()> = tokio::sync::OnceCell::const_new(); - let url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - // The migrations run on a runtime of their own, which outlives the first test. - MIGRATED - .get_or_init(|| async { - let url = url.clone(); - - std::thread::spawn(move || { - tokio::runtime::Runtime::new() - .expect("migration runtime") - .block_on(async { - let migrations = - std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("toasty"); - let config = toasty_cli::Config::new() - .migration(toasty_cli::MigrationConfig::new().path(migrations)); - - toasty_cli::ToastyCli::with_config( - open(&url).await.expect("open pool"), - config, - ) - .parse_from(["toasty", "migration", "apply"]) - .await - .expect("apply migrations"); - }); - }) - .join() - .expect("migrations panicked"); - }) - .await; - open(&url).await.expect("connect to postgres") }