From 191a38332e085d13858cd33a816d8b067fab3d1c Mon Sep 17 00:00:00 2001 From: Patrick Donohoe Date: Thu, 3 Dec 2026 20:07:55 -0500 Subject: [PATCH] Accept a managed Postgres engine and send it in preflight floo.app.toml may set engine = "neon" or "hybrid" on a [managed.] Postgres block, but the CLI refused the key at load, so floo deploy and floo deploy --preflight failed on any hybrid Postgres config. The parser now accepts it under the API's rules (Postgres only, "neon" or "hybrid", hybrid without keep_warm, size, users or tier), and the preflight payload carries it unchanged so the API planner can refuse an engine change. An unset engine stays out of the payload. Co-Authored-By: Claude Opus 5.5 --- src/api_types.rs | 2 + src/commands/dev.rs | 1 + src/project_config/app_config.rs | 93 +++++++++++++++++++ src/project_config/discover.rs | 51 ++++++++++ .../valid/server_supported_keys.toml | 1 + tests/mock_api_tests.rs | 4 + 6 files changed, 152 insertions(+) diff --git a/src/api_types.rs b/src/api_types.rs index 1404f24..abe5e01 100644 --- a/src/api_types.rs +++ b/src/api_types.rs @@ -1039,6 +1039,8 @@ pub struct DeclaredManagedService { pub name: String, #[serde(skip_serializing_if = "Option::is_none")] pub tier: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub engine: Option, } #[derive(Debug, Clone, Serialize)] diff --git a/src/commands/dev.rs b/src/commands/dev.rs index 353386b..d5b4d2c 100644 --- a/src/commands/dev.rs +++ b/src/commands/dev.rs @@ -984,6 +984,7 @@ mod tests { service_type: kind.into(), name: name.into(), tier: None, + engine: None, }) .collect(); let session = crate::api_types::DevSessionResponse { diff --git a/src/project_config/app_config.rs b/src/project_config/app_config.rs index cb28904..2ac8e93 100644 --- a/src/project_config/app_config.rs +++ b/src/project_config/app_config.rs @@ -158,6 +158,10 @@ pub struct ManagedServiceBlock { /// an external app's own users. Validated by the server. #[serde(default, skip_serializing_if = "Option::is_none")] pub users: Option, + /// Postgres only: `"neon"` (the same as unset) or `"hybrid"`. Preflight sends it + /// unchanged. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub engine: Option, } const VALID_MANAGED_SERVICE_TYPES: &[&str] = &["postgres", "redis", "storage"]; @@ -753,6 +757,45 @@ fn validate_worker_command_collision(config: &AppFileConfig) -> Result<(), FlooE Ok(()) } +/// Mirror the API's `engine` rules: Postgres only, `"neon"` or `"hybrid"`, and hybrid +/// without the Neon settings. +fn validate_postgres_engine(name: &str, block: &ManagedServiceBlock) -> Result<(), FlooError> { + let invalid = |message: String, suggestion: &str| { + Err(FlooError::with_suggestion( + ErrorCode::InvalidProjectConfig, + message, + suggestion.to_string(), + )) + }; + match block.engine.as_deref() { + None => Ok(()), + Some(_) if block.service_type != "postgres" => invalid( + format!("[managed.{name}].engine applies only to postgres"), + "Remove engine from this block.", + ), + Some("neon") => Ok(()), + Some("hybrid") => { + let neon_only = [ + ("keep_warm", block.keep_warm.is_some()), + ("size", block.size.is_some()), + ("users", block.users.is_some()), + ("tier", block.tier.is_some()), + ]; + match neon_only.into_iter().find(|(_, set)| *set) { + Some((key, _)) => invalid( + format!("[managed.{name}].{key} is not available with engine = \"hybrid\""), + "Remove that key, or drop engine = \"hybrid\" to keep the database on Neon.", + ), + None => Ok(()), + } + } + Some(_) => invalid( + format!("[managed.{name}].engine must be \"neon\" or \"hybrid\""), + "Set engine = \"neon\" or engine = \"hybrid\".", + ), + } +} + /// Validate `[managed.]` blocks, mirroring the API's parse rules so local /// preflight predicts what the deploy accepts: a known type, and no duplicate /// `(type, name)` — including collisions with the legacy `[postgres]`/`[redis]`/ @@ -783,6 +826,7 @@ fn validate_managed_blocks(config: &AppFileConfig) -> Result<(), FlooError> { format!("Set type to \"postgres\", \"redis\", or \"storage\" in [managed.{name}]."), )); } + validate_postgres_engine(name, block)?; if !seen.insert((block.service_type.clone(), name.clone())) { return Err(FlooError::with_suggestion( ErrorCode::InvalidProjectConfig, @@ -1109,6 +1153,55 @@ tier = "hobby" assert!(err.message.contains("invalid type"), "got: {}", err.message); } + #[test] + fn test_managed_block_engine_hybrid_parses() { + let dir = TempDir::new().unwrap(); + write_app_toml( + &dir, + "[managed.db]\ntype = \"postgres\"\nengine = \"hybrid\"\n", + ); + let config = load_app_config(dir.path()).unwrap().unwrap(); + assert_eq!(config.managed["db"].engine.as_deref(), Some("hybrid")); + } + + #[test] + fn test_managed_block_engine_rules_match_the_api() { + for (body, expected) in [ + ( + "type = \"postgres\"\nengine = \"aurora\"\n", + "engine must be \"neon\" or \"hybrid\"", + ), + ( + "type = \"postgres\"\nengine = \"hybrid\"\nsize = \"small\"\n", + "size is not available with engine = \"hybrid\"", + ), + ( + "type = \"postgres\"\nengine = \"hybrid\"\ntier = \"basic\"\n", + "tier is not available with engine = \"hybrid\"", + ), + ] { + let dir = TempDir::new().unwrap(); + write_app_toml(&dir, &format!("[managed.db]\n{body}")); + let err = load_app_config(dir.path()).unwrap_err(); + assert!(err.message.contains(expected), "got: {}", err.message); + } + } + + #[test] + fn test_managed_block_engine_rejected_off_postgres() { + let dir = TempDir::new().unwrap(); + write_app_toml( + &dir, + "[managed.cache]\ntype = \"redis\"\nengine = \"hybrid\"\n", + ); + let err = load_app_config(dir.path()).unwrap_err(); + assert!( + err.message.contains("engine applies only to postgres"), + "got: {}", + err.message + ); + } + #[test] fn test_managed_block_missing_type_rejected() { let dir = TempDir::new().unwrap(); diff --git a/src/project_config/discover.rs b/src/project_config/discover.rs index a1fdc37..fa78693 100644 --- a/src/project_config/discover.rs +++ b/src/project_config/discover.rs @@ -364,6 +364,7 @@ pub fn discover_managed_services(resolved: &ResolvedApp) -> Vec Vec = legacy.chain(modern).collect(); result.sort_by(|a, b| (&a.service_type, &a.name).cmp(&(&b.service_type, &b.name))); @@ -2250,6 +2252,55 @@ command = "node worker.js" assert!(managed.is_empty()); } + /// The preflight payload carries a Postgres block's `engine` unchanged, and leaves + /// the key out where floo.app.toml does, so the API can refuse an engine change. + #[test] + fn test_preflight_payload_carries_postgres_engine_only_when_set() { + let dir = TempDir::new().unwrap(); + fs::write( + dir.path().join(crate::project_config::APP_CONFIG_FILE), + r#"[app] +name = "test-app" +[postgres] +tier = "basic" +[managed.main] +type = "postgres" +engine = "hybrid" +[managed.old] +type = "postgres" +engine = "neon" +[managed.cache] +type = "redis" +"#, + ) + .unwrap(); + let app_cfg = crate::project_config::load_app_config(dir.path()) + .unwrap() + .unwrap(); + let resolved = make_resolved( + dir.path(), + "test-app", + None, + Some(app_cfg), + AppSource::AppFile, + ); + + let declared = crate::api_types::DeclaredState { + managed_services: discover_managed_services(&resolved), + ..Default::default() + }; + let payload = serde_json::to_value(&declared).unwrap(); + assert_eq!( + payload["managed_services"], + serde_json::json!([ + {"type": "postgres", "name": "default", "tier": "basic"}, + {"type": "postgres", "name": "main", "engine": "hybrid"}, + {"type": "postgres", "name": "old", "engine": "neon"}, + {"type": "redis", "name": "cache"} + ]) + ); + } + #[test] fn test_discover_managed_services_empty_when_no_app_config() { let dir = TempDir::new().unwrap(); diff --git a/tests/fixtures/manifests/valid/server_supported_keys.toml b/tests/fixtures/manifests/valid/server_supported_keys.toml index 15dace2..880ef6f 100644 --- a/tests/fixtures/manifests/valid/server_supported_keys.toml +++ b/tests/fixtures/manifests/valid/server_supported_keys.toml @@ -43,6 +43,7 @@ tier = "basic" enabled = true [managed.db] type = "postgres" +engine = "neon" keep_warm = "always" size = "large" [managed.db.users] diff --git a/tests/mock_api_tests.rs b/tests/mock_api_tests.rs index fe9eff0..063f258 100644 --- a/tests/mock_api_tests.rs +++ b/tests/mock_api_tests.rs @@ -6580,6 +6580,7 @@ fn test_preflight_declares_enabled_modern_and_legacy_services_without_lock() { let planner = server.mock("POST", format!("/v1/apps/{TEST_APP_ID}/preflight").as_str()) .match_body(Matcher::PartialJson(serde_json::json!({ "managed_services": [ + {"type": "postgres", "name": "db", "engine": "hybrid"}, {"type": "postgres", "name": "default", "tier": "basic"}, {"type": "redis", "name": "cache", "tier": "basic"}, {"type": "redis", "name": "queue"} @@ -6600,6 +6601,9 @@ fn test_preflight_declares_enabled_modern_and_legacy_services_without_lock() { name = "{TEST_APP_NAME}" [postgres] tier = "basic" +[managed.db] +type = "postgres" +engine = "hybrid" [managed.cache] type = "redis" tier = "basic"