stackless_integrations/providers/railway/
postgres.rs1use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12
13pub const RESOURCE_KIND: &str = "integration-railway-postgres";
14
15#[derive(Debug, Serialize)]
16pub struct RailwayPostgresConfig {
17 #[serde(skip_serializing_if = "Option::is_none")]
18 pub region: Option<String>,
19}
20
21impl CatalogService for RailwayPostgresConfig {
22 const REFERENCE: &'static str = "railway/postgres";
23}
24
25#[derive(Debug)]
26pub struct RailwayPostgres;
27
28impl Hostable for RailwayPostgres {
29 const PROVIDER: &'static str = "railway-postgres";
30 const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
31 const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
32 const RESOURCE_KIND: &'static str = RESOURCE_KIND;
33 const OUTPUTS: &'static [&'static str] = &["database_url"];
34}
35
36impl FamilyResource for RailwayPostgres {
37 type Config = RailwayPostgresConfig;
38 const PROVIDER_PREFIX: &'static str = "RAILWAY";
39 const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] =
41 &[("DATABASE_URL", "database_url", true)];
42
43 fn build_config(ctx: &ProvisionContext<'_>) -> Result<RailwayPostgresConfig, IntegrationError> {
44 let config = super::integration_config(ctx)?;
45 Ok(RailwayPostgresConfig {
46 region: super::interp_optional(ctx, &config, "region")?,
47 })
48 }
49}
50
51pub fn validate_config(
52 name: &str,
53 config: &BTreeMap<String, toml::Value>,
54) -> Result<(), IntegrationError> {
55 let _ = (name, config);
56 Ok(())
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62 use crate::ProviderOps;
63 use crate::resource::ResourcePayload;
64 use stackless_core::def::StackDef;
65 use stackless_stripe_projects::stripe::StripeProjects;
66 use stackless_stripe_projects::test_support;
67
68 #[test]
69 fn config_matches_catalog() {
70 const FIXTURE: &str = include_str!(concat!(
71 env!("CARGO_MANIFEST_DIR"),
72 "/../stackless-stripe-projects/tests/fixtures/catalog.json"
73 ));
74 let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
75 let failures = stackless_stripe_projects::verify_service(
76 &catalog,
77 &RailwayPostgresConfig { region: None },
78 );
79 assert!(
80 failures.is_empty(),
81 "railway/postgres catalog gaps:\n{}",
82 failures.join("\n")
83 );
84 }
85
86 const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_postgres","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_railway","provider_name":"Railway","service_id":"postgres","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"region":{"description":"Deployment region (e.g. us-west1, us-east4)","type":"string"}},"type":"object"}}]}}"##;
87
88 fn test_def() -> StackDef {
89 StackDef::parse(
90 r#"
91[stack]
92name = "atto"
93[stack.projects.stripe]
94project = "project_1"
95[integrations.res]
96provider = "railway-postgres"
97[services.api]
98source = { repo = "r", ref = "main" }
99env = { OUT = "${integrations.res.database_url}" }
100health = { path = "/health" }
101[services.api.local]
102run = "true"
103"#,
104 )
105 .unwrap()
106 }
107
108 #[tokio::test]
109 async fn provision_records_outputs() {
110 let runner = test_support::provision_script(
111 CATALOG_ENVELOPE,
112 serde_json::json!({"RAILWAY_DATABASE_URL": "val_database_url"}),
113 0,
114 );
115 let dir = tempfile::tempdir().unwrap();
116 std::fs::write(
117 dir.path().join("stackless.toml"),
118 "[stack]\nname=\"atto\"\n",
119 )
120 .unwrap();
121 let stripe = StripeProjects::new(&runner, dir.path());
122
123 let resource = RailwayPostgres
124 .provision(
125 &stripe.as_dyn(),
126 &test_def(),
127 dir.path(),
128 "demo",
129 "res",
130 "local",
131 false,
132 )
133 .await
134 .unwrap();
135 assert_eq!(resource.resource_kind, "integration-railway-postgres");
136 let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
137 assert_eq!(payload.outputs["database_url"], "val_database_url");
138 }
139}