diff --git a/AGENTS.md b/AGENTS.md index 467a3091..9bd24b60 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -70,6 +70,9 @@ design is in `docs/design.md`; read it before changing the architecture, and upd anything main's code still names. This is deploy safety, not backwards compatibility: the old column still goes, one release later. (4 Dec 2026, a dropped organizations column failed every request for ~20 s, a new user's first build among them.) +- The server connects with sqlx's prepared-statement cache off (`config::without_statement_cache`): a migration that + adds a column makes Postgres refuse an old pod's cached `SELECT table.*` ("cached plan must not change result type"). + Keep it off. `tests/rollouts.rs` fails if it comes back on. (4 Dec 2026, #178's rollout.) - Every background process (workers, renderers, scheduled jobs) ships with a health check or heartbeat that checks the work is done, not just that the process is up, in the same change. - Production reads its secrets from 1Password through the cluster's ExternalSecrets (talira-ai/infra, diff --git a/app/src/config.rs b/app/src/config.rs index c1306bf8..26ffdece 100644 --- a/app/src/config.rs +++ b/app/src/config.rs @@ -684,6 +684,20 @@ fn application_name(name: &str) -> Option { .then(|| name.to_string()) } +/// The database URL with sqlx's prepared-statement cache off (`statement-cache-capacity=0`). Migrations run before the +/// rollout, so the previous release serves against the new schema for a few seconds: a statement it prepared for +/// `SELECT builds.*` before a migration added a column fails with "cached plan must not change result type" (SQLSTATE +/// 0A000) until its connection is replaced (4 Dec 2026, #178's rollout: build creates answered 500). Without the cache, +/// every query is planned against the schema as it is. A URL that sets the capacity itself keeps it. +#[must_use] +pub fn without_statement_cache(url: &str) -> String { + if url.is_empty() || url.contains("statement-cache-capacity=") { + return url.to_string(); + } + let separator = if url.contains('?') { '&' } else { '?' }; + format!("{url}{separator}statement-cache-capacity=0") +} + /// The database connection's pool: a short wait for a pooled connection (`VISUALCI_DB_ACQUIRE_TIMEOUT`), so a burst of /// CI builds that takes every connection answers 503 with Retry-After at once (`http::busy`) rather than holding each /// request for the framework's default 30 seconds while the queue grows (today's server hung on 3 Dec 2026, @@ -693,6 +707,9 @@ fn tune_database(database: &mut laravel::config::database::Database, network: &N let Some(connection) = database.connections.remove(&default) else { return }; let seconds = i64::try_from(network.db_acquire_timeout).unwrap_or(5); let mut connection = connection.acquire_timeout(laravel::time::SignedDuration::from_secs(seconds)); + if let laravel::config::database::Source::Url(url) = &mut connection.source { + *url = without_statement_cache(url); + } if let Some(name) = network.db_application_name.as_deref().and_then(application_name) { connection = connection.initialize(laravel::db::sqlx::AssertSqlSafe(format!("SET application_name = '{name}'"))); @@ -707,6 +724,20 @@ mod tests { use super::*; + #[test] + fn the_database_url_turns_the_statement_cache_off() { + assert_eq!(without_statement_cache("postgres://a@db/app"), "postgres://a@db/app?statement-cache-capacity=0"); + assert_eq!( + without_statement_cache("postgres://a@db/app?sslmode=require"), + "postgres://a@db/app?sslmode=require&statement-cache-capacity=0" + ); + assert_eq!( + without_statement_cache("postgres://a@db/app?statement-cache-capacity=50"), + "postgres://a@db/app?statement-cache-capacity=50" + ); + assert_eq!(without_statement_cache(""), ""); + } + #[test] fn an_application_name_is_used_only_when_it_is_safe() { assert_eq!(application_name(" visualci-web ").as_deref(), Some("visualci-web")); diff --git a/app/tests/rollouts.rs b/app/tests/rollouts.rs new file mode 100644 index 00000000..6cc0ebef --- /dev/null +++ b/app/tests/rollouts.rs @@ -0,0 +1,19 @@ +//! Rollouts: migrations run before the new release's pods start, so the previous release serves against the new +//! schema for a few seconds. Its queries must keep working when a migration adds a column (4 Dec 2026, #178's +//! rollout: a pooled connection's prepared `select "builds".*` failed with "cached plan must not change result type"). + +use app::models::Build; +use laravel::db; +use laravel::testing::prelude::*; + +#[laravel::test] +async fn a_server_keeps_reading_a_table_after_a_migration_adds_a_column(_app: Application) -> laravel::Result<()> { + // The same query the build create path runs, before and after the column appears. + let read = || async { Build::query().filter(Build::id.eq(0)).first().await }; + for _ in 0..3 { + read().await?; + } + db::query!("ALTER TABLE builds ADD COLUMN rollout_probe TEXT").update().await?; + read().await.map_err(|e| laravel::Error::msg(format!("after a column was added: {e}")))?; + Ok(()) +}