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
2 changes: 2 additions & 0 deletions src/api_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,8 @@ pub struct DeclaredManagedService {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub tier: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub engine: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ mod tests {
service_type: kind.into(),
name: name.into(),
tier: None,
engine: None,
})
.collect();
let session = crate::api_types::DevSessionResponse {
Expand Down
93 changes: 93 additions & 0 deletions src/project_config/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<toml::Value>,
/// Postgres only: `"neon"` (the same as unset) or `"hybrid"`. Preflight sends it
/// unchanged.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub engine: Option<String>,
}

const VALID_MANAGED_SERVICE_TYPES: &[&str] = &["postgres", "redis", "storage"];
Expand Down Expand Up @@ -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.<name>]` 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]`/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
51 changes: 51 additions & 0 deletions src/project_config/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ pub fn discover_managed_services(resolved: &ResolvedApp) -> Vec<ManagedServiceDe
service_type: service_type.to_string(),
name: "default".to_string(),
tier: section.tier.clone(),
engine: None,
})
});
let modern = app_cfg
Expand All @@ -374,6 +375,7 @@ pub fn discover_managed_services(resolved: &ResolvedApp) -> Vec<ManagedServiceDe
service_type: block.service_type.clone(),
name: name.clone(),
tier: block.tier.clone(),
engine: block.engine.clone(),
});
let mut result: Vec<_> = legacy.chain(modern).collect();
result.sort_by(|a, b| (&a.service_type, &a.name).cmp(&(&b.service_type, &b.name)));
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tier = "basic"
enabled = true
[managed.db]
type = "postgres"
engine = "neon"
keep_warm = "always"
size = "large"
[managed.db.users]
Expand Down
4 changes: 4 additions & 0 deletions tests/mock_api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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"
Expand Down