The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,195 lines 43.1 KB
Raw
1 import { randomBytes } from "node:crypto";
2 import { Query, type UnwrapPlan } from "@distilled.cloud/core/query";
3 import {
4 Railway,
5 type ServiceCreateInput,
6 type ServiceInstance,
7 type ServiceInstanceUpdateInput,
8 type Service as RailwayService,
9 type TCPProxy,
10 type TCPProxyCreateInput,
11 type VariableUpsertInput,
12 type VolumeCreateInput,
13 type VolumeInstance,
14 type VolumeInstanceUpdateInput,
15 type VolumeState,
16 } from "@distilled.cloud/railway";
17 import { Unowned } from "alchemy/AdoptPolicy";
18 import { isResolved } from "alchemy/Diff";
19 import * as Provider from "alchemy/Provider";
20 import { Resource } from "alchemy/Resource";
21 import * as Data from "effect/Data";
22 import * as Effect from "effect/Effect";
23 import * as Redacted from "effect/Redacted";
24 import * as Schedule from "effect/Schedule";
25 import * as Stream from "effect/Stream";
26 import {
27 environmentServiceInstances,
28 waitUntilDeleted,
29 projectServices,
30 environmentVolumes,
31 } from "./GraphQL.ts";
32 import { createRailwayName, matchesAlchemyPhysicalName } from "./Metadata.ts";
33 import { ownedProjects, type Project } from "./Project.ts";
34 import type { Providers } from "./Providers.ts";
35
36 /**
37 * A resource-valued prop: the resource itself, or an Effect that produces
38 * it (so `yield* Project(...)` and `Project(...)` both type-check).
39 */
40 type Ref<T> = T | Effect.Effect<T, never, Providers>;
41
42 export const DEFAULT_POSTGRES_IMAGE = "ghcr.io/railwayapp-templates/postgres-ssl:16";
43 export const DEFAULT_POSTGRES_USER = "postgres";
44 export const DEFAULT_POSTGRES_DATABASE = "railway";
45 export const POSTGRES_PORT = 5432;
46 export const POSTGRES_MOUNT_PATH = "/var/lib/postgresql/data";
47 export const POSTGRES_PGDATA = "/var/lib/postgresql/data/pgdata";
48 import { DATABASE_PUBLIC_URL_SECRET, DATABASE_URL_SECRET } from "./ConnectPostgres.ts";
49
50 const serviceFields = <E>(service: Query<RailwayService, E>) => ({
51 id: service.id,
52 name: service.name,
53 deletedAt: service.deletedAt,
54 });
55 const attributeInstanceFields = <E>(instance: Query<ServiceInstance, E>) => ({
56 source: instance.source.pipe(Query.map((source) => ({ image: source.image }))),
57 region: instance.region,
58 latestDeployment: instance.latestDeployment.pipe(
59 Query.map((deployment) => ({ id: deployment.id, status: deployment.status })),
60 ),
61 });
62 const instanceFields = <E>(instance: Query<ServiceInstance, E>) => ({
63 ...attributeInstanceFields(instance),
64 deletedAt: instance.deletedAt,
65 sleepApplication: instance.sleepApplication,
66 });
67 const volumeFields = <E>(instance: Query<VolumeInstance, E>) => ({
68 id: instance.id,
69 volumeId: instance.volumeId,
70 environmentId: instance.environmentId,
71 serviceId: instance.serviceId,
72 deletedAt: instance.deletedAt,
73 isPendingDeletion: instance.isPendingDeletion,
74 state: instance.state,
75 mountPath: instance.mountPath,
76 volume: { id: instance.volume.id, name: instance.volume.name },
77 });
78 const proxyFields = <E>(proxy: Query<TCPProxy, E>) => ({
79 id: proxy.id,
80 applicationPort: proxy.applicationPort,
81 deletedAt: proxy.deletedAt,
82 syncStatus: proxy.syncStatus,
83 domain: proxy.domain,
84 proxyPort: proxy.proxyPort,
85 });
86 type CloudService = UnwrapPlan<ReturnType<typeof serviceFields>>;
87 type AttributeInstance = UnwrapPlan<ReturnType<typeof attributeInstanceFields>>;
88 type ServiceInstanceResponse = UnwrapPlan<ReturnType<typeof instanceFields>>;
89 type CloudInstance = UnwrapPlan<ReturnType<typeof volumeFields>>;
90 type CloudProxy = UnwrapPlan<ReturnType<typeof proxyFields>>;
91
92 export { DATABASE_PUBLIC_URL_SECRET, DATABASE_URL_SECRET };
93
94 /**
95 * Environment identity Postgres is deployed into. Accepts a
96 * `Railway.Project` (its primary environment), a `Railway.Environment`,
97 * or an `{ environmentId }` stub.
98 */
99 export type PostgresEnvironment = { readonly environmentId: string };
100
101 export interface PostgresProps {
102 /**
103 * Parent Railway Project. Accepts a `Railway.Project` or an Effect
104 * that produces one. Changing the Project replaces Postgres.
105 */
106 project: Ref<Project>;
107 /**
108 * Environment to deploy into. Accepts a `Railway.Project` (primary
109 * environment), a `Railway.Environment`, or `{ environmentId }`.
110 * Defaults to the project's primary environment. Changing it replaces
111 * Postgres.
112 */
113 environment?: Ref<PostgresEnvironment>;
114 /**
115 * Service name. Unique per Project. If omitted, a unique name is
116 * generated from the stack, stage and logical ID. Used as the private
117 * hostname `{name}.railway.internal`. Changing it updates in place.
118 */
119 name?: string;
120 /**
121 * Postgres image. Default is the official SSL image tag 16.
122 *
123 * @default "ghcr.io/railwayapp-templates/postgres-ssl:16"
124 */
125 image?: string;
126 /**
127 * Region for the service instance and volume (`us-west2`, `us-east4`,
128 * …). If omitted, Railway picks the default. Changing it replaces
129 * Postgres.
130 */
131 region?: string;
132 /**
133 * Superuser name. Create-only (used at `initdb`).
134 *
135 * @default "postgres"
136 */
137 user?: string;
138 /**
139 * Superuser password. Wrap with `Redacted.make(...)`. If omitted, a
140 * password is generated on first create and stored as
141 * `POSTGRES_PASSWORD`. Create-only.
142 */
143 password?: Redacted.Redacted<string> | string;
144 /**
145 * Initial database name. Create-only.
146 *
147 * @default "railway"
148 */
149 database?: string;
150 /**
151 * Expose Postgres on a public TCP proxy (`*.proxy.rlwy.net`) for
152 * laptop access and deploy-time migrations. In-service connections
153 * always use `{name}.railway.internal`.
154 *
155 * @default true
156 */
157 public?: boolean;
158 }
159
160 export type Postgres = Resource<
161 "Railway.Postgres",
162 PostgresProps,
163 {
164 /** Railway service id for the Postgres container. */
165 serviceId: string;
166 /** Physical service name. Private hostname is `{name}.railway.internal`. */
167 name: string;
168 /** Parent Railway project id. */
169 projectId: string;
170 /** Environment the instance is deployed in. */
171 environmentId: string;
172 /** Observed `source.image`. */
173 image: string | undefined;
174 /** Observed region, if Railway reported one. */
175 region: string | undefined;
176 /** Volume id holding PGDATA. */
177 volumeId: string;
178 /** Volume instance id in the target environment. */
179 volumeInstanceId: string;
180 /** TCP proxy id, when `public` is enabled. */
181 tcpProxyId: string | undefined;
182 /** Public proxy hostname (`*.proxy.rlwy.net`). */
183 tcpProxyDomain: string | undefined;
184 /** Public proxy port. Pair with `tcpProxyDomain`. */
185 tcpProxyPort: number | undefined;
186 /** Superuser name. */
187 user: string;
188 /** Database name. */
189 database: string;
190 /**
191 * Private Postgres URI (`{name}.railway.internal:5432`). Prefer
192 * {@link ConnectPostgres} from a {@link Service}.
193 */
194 connectionUri: string;
195 /**
196 * Public TCP-proxy URI. Empty when `public` is false. Use this from
197 * the laptop / deploy-time migrations.
198 */
199 publicConnectionUri: string;
200 /** Latest deployment id, if one exists. */
201 deploymentId: string | undefined;
202 /** Latest deployment status (`SUCCESS`, `DEPLOYING`, …). */
203 deploymentStatus: string | undefined;
204 },
205 never,
206 Providers
207 >;
208
209 const resolvePostgresProps = (
210 props: PostgresProps | Effect.Effect<PostgresProps, never, Providers>,
211 ): Effect.Effect<PostgresProps, never, Providers> =>
212 Effect.gen(function* () {
213 const resolved = Effect.isEffect(props) ? yield* props : props;
214 if (globalThis.__ALCHEMY_RUNTIME__) return resolved;
215 const project = Effect.isEffect(resolved.project)
216 ? yield* resolved.project as Effect.Effect<Project, never, Providers>
217 : resolved.project;
218 const environment =
219 resolved.environment === undefined
220 ? undefined
221 : Effect.isEffect(resolved.environment)
222 ? yield* resolved.environment as Effect.Effect<PostgresEnvironment, never, Providers>
223 : resolved.environment;
224 return { ...resolved, project, environment };
225 });
226
227 const PostgresResource = Resource<Postgres>("Railway.Postgres");
228
229 /**
230 * A Railway.Postgres is a Postgres-as-a-Service: the official
231 * `ghcr.io/railwayapp-templates/postgres-ssl:16` image, a Volume at
232 * `/var/lib/postgresql/data`, `POSTGRES_*` / `DATABASE_URL` variables,
233 * and an optional TCP proxy for the public URL.
234 *
235 * Private hostname is `{name}.railway.internal`. From a
236 * {@link Service}, yield {@link ConnectPostgres}. From a laptop, use
237 * `publicConnectionUri`.
238 *
239 * @see https://docs.railway.com/databases/build-a-database-service
240 *
241 * ### Create Postgres
242 * Pass a Project. Alchemy generates a unique name, password, volume,
243 * and a public TCP proxy.
244 *
245 * **Example:** Generated name
246 * ```typescript
247 * const site = yield* Railway.Project("Site");
248 * const db = yield* Railway.Postgres("Db", { project: site });
249 * ```
250 *
251 * :::caution[Changing `project` replaces Postgres]
252 * A new service + volume are created in the new Project. The old
253 * service and volume are deleted. Data is not copied.
254 * :::
255 *
256 * ### Connect from a Service
257 * Yield `ConnectPostgres` inside init. Provide
258 * {@link ConnectPostgresHttp}. Pass `conn.connectionString` to
259 * `Drizzle.Postgres` or `SQL.Postgres`. The binding packs the private
260 * URI (`{name}.railway.internal`).
261 *
262 * **Example:** Bind and query
263 * ```typescript
264 * import * as Drizzle from "@alchemy.run/drizzle/Postgres";
265 * import * as HttpServerResponse from "effect/http/HttpServerResponse";
266 *
267 * export default class Api extends Railway.Service<Api>()(
268 * "Api",
269 * { project: Site, main: import.meta.url, build: { install: ["pg"] } },
270 * Effect.gen(function* () {
271 * const conn = yield* Railway.ConnectPostgres(Db);
272 * const db = yield* Drizzle.Postgres(conn.connectionString);
273 * return {
274 * fetch: Effect.gen(function* () {
275 * const rows = yield* db.execute("select 1 as ok");
276 * return HttpServerResponse.json({ rows });
277 * }),
278 * };
279 * }).pipe(Effect.provide(Railway.ConnectPostgresHttp)),
280 * ) {}
281 * ```
282 *
283 * ### Public TCP
284 * `public` (default `true`) creates a TCP proxy on 5432.
285 * `publicConnectionUri` is `{domain}:{proxyPort}` for laptop access
286 * and deploy-time migrations.
287 *
288 * **Example:** Private only
289 * ```typescript
290 * const db = yield* Railway.Postgres("Db", {
291 * project: site,
292 * public: false,
293 * });
294 * ```
295 *
296 * ### Image
297 * Default is Postgres 16 with SSL. Pass `image` to pin another tag.
298 *
299 * **Example:** Postgres 17
300 * ```typescript
301 * const db = yield* Railway.Postgres("Db", {
302 * project: site,
303 * image: "ghcr.io/railwayapp-templates/postgres-ssl:17",
304 * });
305 * ```
306 *
307 * ### Module-scope declarations
308 * Resource-valued props accept the resource or an Effect producing it.
309 *
310 * **Example:** Module-scope Postgres
311 * ```typescript
312 * // src/db.ts
313 * import * as Railway from "@alchemy.run/railway";
314 *
315 * export const Site = Railway.Project("Site");
316 * export const Db = Railway.Postgres("Db", { project: Site });
317 * ```
318 *
319 * @resource
320 * @product Postgres
321 */
322 export const Postgres: typeof PostgresResource = Object.assign(
323 (id: string, props: PostgresProps | Effect.Effect<PostgresProps, never, Providers>) =>
324 PostgresResource(id, resolvePostgresProps(props)),
325 PostgresResource,
326 );
327
328 export class PostgresNotCreated extends Data.TaggedError("Railway.PostgresNotCreated")<{
329 resourceName: string;
330 projectId: string;
331 }> {}
332
333 export class PostgresProjectRequired extends Data.TaggedError("Railway.PostgresProjectRequired")<{
334 message: string;
335 }> {}
336
337 export class PostgresDeployFailed extends Data.TaggedError("Railway.PostgresDeployFailed")<{
338 serviceId: string;
339 status: string;
340 deploymentId: string | undefined;
341 }> {}
342
343 export class PostgresVolumeNotCreated extends Data.TaggedError("Railway.PostgresVolumeNotCreated")<{
344 resourceName: string;
345 serviceId: string;
346 }> {}
347
348 class PostgresPending extends Data.TaggedError("Railway.PostgresPending")<{
349 serviceId: string;
350 status: string;
351 }> {}
352
353 class PostgresDeployPending extends Data.TaggedError("Railway.PostgresDeployPending")<{
354 serviceId: string;
355 status: string;
356 }> {}
357
358 class VolumePending extends Data.TaggedError("Railway.PostgresVolumePending")<{
359 volumeId: string;
360 state: string;
361 }> {
362 override get message() {
363 return `Postgres volume ${this.volumeId} is still ${this.state}`;
364 }
365 }
366
367 const projectIdOf = (value: unknown): string | undefined => {
368 if (value === null || typeof value !== "object") return undefined;
369 const rec = value as { projectId?: unknown };
370 return typeof rec.projectId === "string" && rec.projectId.length > 0 ? rec.projectId : undefined;
371 };
372
373 const environmentIdOf = (value: unknown): string | undefined => {
374 if (value === null || typeof value !== "object") return undefined;
375 const rec = value as { environmentId?: unknown };
376 return typeof rec.environmentId === "string" && rec.environmentId.length > 0
377 ? rec.environmentId
378 : undefined;
379 };
380
381 const unwrapSecret = (value: Redacted.Redacted<string> | string): string =>
382 Redacted.isRedacted(value) ? Redacted.value(value) : value;
383
384 const generatePassword = Effect.sync(() => {
385 const bytes = randomBytes(16);
386 return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
387 });
388
389 const isGoneService = (service: CloudService | undefined) =>
390 service === undefined || service.deletedAt != null;
391
392 const isGoneInstance = (instance: ServiceInstanceResponse | undefined) =>
393 instance === undefined || instance.deletedAt != null;
394
395 const goneVolumeState = (state: VolumeState | null | undefined) =>
396 state === "DELETED" || state === "DELETING";
397
398 const transientVolumeState = (state: VolumeState | null | undefined) =>
399 state === "UPDATING" ||
400 state === "MIGRATING" ||
401 state === "MIGRATION_PENDING" ||
402 state === "RESTORING";
403
404 const isGoneVolume = (instance: CloudInstance | undefined) =>
405 instance === undefined ||
406 instance.deletedAt != null ||
407 instance.isPendingDeletion ||
408 goneVolumeState(instance.state);
409
410 const isGoneProxy = (proxy: CloudProxy | undefined) =>
411 proxy === undefined || proxy.deletedAt != null || proxy.syncStatus === "DELETED";
412
413 const normalizeDomain = (domain: string) => domain.replace(/\.+$/, "");
414
415 const sameImage = (observed: string | null | undefined, desired: string) => {
416 if (observed == null || observed.length === 0) return false;
417 if (observed === desired) return true;
418 if (observed === `${desired}:latest` || desired === `${observed}:latest`) {
419 return true;
420 }
421 return observed.endsWith(`/${desired}`) || observed.endsWith(`/${desired}:latest`);
422 };
423
424 const isPostgresImage = (image: string | null | undefined) =>
425 image != null && /postgres/i.test(image);
426
427 const deployReady = (status: string | undefined) => status === "SUCCESS";
428
429 const deployFailed = (status: string | undefined) =>
430 status === "FAILED" || status === "CRASHED" || status === "REMOVED";
431
432 const resolveName = (id: string, name: string | undefined, existing?: string) =>
433 Effect.gen(function* () {
434 if (name !== undefined) return name;
435 if (existing !== undefined) return existing;
436 return yield* createRailwayName(id);
437 });
438
439 const encodePart = (value: string) => encodeURIComponent(value);
440
441 export const privateConnectionUri = (input: {
442 user: string;
443 password: string;
444 name: string;
445 database: string;
446 }): string =>
447 `postgresql://${encodePart(input.user)}:${encodePart(input.password)}@${input.name}.railway.internal:${POSTGRES_PORT}/${encodePart(input.database)}?sslmode=no-verify`;
448
449 export const publicConnectionUri = (input: {
450 user: string;
451 password: string;
452 domain: string;
453 port: number;
454 database: string;
455 }): string =>
456 `postgresql://${encodePart(input.user)}:${encodePart(input.password)}@${input.domain}:${input.port}/${encodePart(input.database)}?sslmode=no-verify`;
457
458 const desiredVariables = (input: {
459 user: string;
460 password: string;
461 database: string;
462 }): Record<string, string> => ({
463 POSTGRES_USER: input.user,
464 POSTGRES_PASSWORD: input.password,
465 POSTGRES_DB: input.database,
466 PGDATA: POSTGRES_PGDATA,
467 PGUSER: "${{POSTGRES_USER}}",
468 PGPASSWORD: "${{POSTGRES_PASSWORD}}",
469 PGDATABASE: "${{POSTGRES_DB}}",
470 PGHOST: "${{RAILWAY_PRIVATE_DOMAIN}}",
471 PGPORT: String(POSTGRES_PORT),
472 [DATABASE_URL_SECRET]:
473 "postgresql://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:5432/${{POSTGRES_DB}}",
474 [DATABASE_PUBLIC_URL_SECRET]:
475 "postgresql://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{POSTGRES_DB}}",
476 });
477
478 const toAttrs = (input: {
479 service: CloudService;
480 instance: AttributeInstance | undefined;
481 volume: CloudInstance | undefined;
482 proxy: CloudProxy | undefined;
483 projectId: string;
484 environmentId: string;
485 user: string;
486 password: string;
487 database: string;
488 }): Postgres["Attributes"] => {
489 const name = input.service.name;
490 const domain = input.proxy !== undefined ? normalizeDomain(input.proxy.domain) : undefined;
491 const proxyPort = input.proxy?.proxyPort;
492 return {
493 serviceId: input.service.id,
494 name,
495 projectId: input.projectId,
496 environmentId: input.environmentId,
497 image: input.instance?.source?.image ?? undefined,
498 region: input.instance?.region ?? undefined,
499 volumeId: input.volume?.volumeId || input.volume?.volume.id || "",
500 volumeInstanceId: input.volume?.id ?? "",
501 tcpProxyId: input.proxy?.id,
502 tcpProxyDomain: domain,
503 tcpProxyPort: proxyPort,
504 user: input.user,
505 database: input.database,
506 connectionUri:
507 input.password.length > 0
508 ? privateConnectionUri({
509 user: input.user,
510 password: input.password,
511 name,
512 database: input.database,
513 })
514 : "",
515 publicConnectionUri:
516 input.password.length > 0 &&
517 domain !== undefined &&
518 domain.length > 0 &&
519 proxyPort !== undefined
520 ? publicConnectionUri({
521 user: input.user,
522 password: input.password,
523 domain,
524 port: proxyPort,
525 database: input.database,
526 })
527 : "",
528 deploymentId: input.instance?.latestDeployment?.id,
529 deploymentStatus: input.instance?.latestDeployment?.status,
530 };
531 };
532
533 const readService = Query.fn((id: string) => serviceFields(Railway.service({ id })));
534
535 const readInstance = Query.fn((environmentId: string, serviceId: string) =>
536 instanceFields(Railway.serviceInstance({ environmentId, serviceId })),
537 );
538
539 const readVolumeInstance = Query.fn((id: string) => volumeFields(Railway.volumeInstance({ id })));
540
541 const readProxies = Query.fn((environmentId: string, serviceId: string) =>
542 Railway.tcpProxies({ environmentId, serviceId }).pipe(Query.map(proxyFields)),
543 );
544
545 const tcpProxyDelete = Query.fn((id: string) => Railway.tcpProxyDelete({ id }));
546
547 const readVariables = Query.fn((projectId: string, environmentId: string, serviceId: string) =>
548 Railway.variables({ projectId, environmentId, serviceId, unrendered: true }),
549 );
550
551 const variableUpsert = Query.fn((input: VariableUpsertInput) => Railway.variableUpsert({ input }));
552
553 const volumeUpdateName = Query.fn((volumeId: string, name: string) => ({
554 id: Railway.volumeUpdate({ volumeId, input: { name } }).id,
555 }));
556
557 const serviceCreate = Query.fn((input: ServiceCreateInput) =>
558 serviceFields(Railway.serviceCreate({ input })),
559 );
560
561 const serviceUpdateName = Query.fn((id: string, name: string) =>
562 serviceFields(Railway.serviceUpdate({ id, input: { name } })),
563 );
564
565 const serviceInstanceUpdate = Query.fn(
566 (environmentId: string, serviceId: string, input: ServiceInstanceUpdateInput) =>
567 Railway.serviceInstanceUpdate({ environmentId, serviceId, input }),
568 );
569
570 const volumeCreate = Query.fn((input: VolumeCreateInput) => {
571 const volume = Railway.volumeCreate({ input });
572 return { id: volume.id, name: volume.name };
573 });
574
575 const volumeInstanceUpdate = Query.fn(
576 (volumeId: string, environmentId: string, input: VolumeInstanceUpdateInput) =>
577 Railway.volumeInstanceUpdate({ volumeId, environmentId, input }),
578 );
579
580 const tcpProxyCreate = Query.fn((input: TCPProxyCreateInput) =>
581 proxyFields(Railway.tcpProxyCreate({ input })),
582 );
583
584 const serviceInstanceDeploy = Query.fn((environmentId: string, serviceId: string) =>
585 Railway.serviceInstanceDeployV2({ environmentId, serviceId }),
586 );
587
588 const deploymentCancel = Query.fn((id: string) => Railway.deploymentCancel({ id }));
589
590 const serviceDelete = Query.fn((id: string) => Railway.serviceDelete({ id }));
591
592 const volumeDelete = Query.fn((volumeId: string) => Railway.volumeDelete({ volumeId }));
593
594 const liveEnvironmentIds = (projectId: string) =>
595 Query.items(
596 Railway.environments({ projectId, first: 50 }).pipe(
597 Query.map((env) => ({ id: env.id, deletedAt: env.deletedAt })),
598 ),
599 );
600
601 const getById = (serviceId: string) =>
602 readService(serviceId).pipe(
603 Effect.map((service) => (isGoneService(service) ? undefined : service)),
604 Effect.catchTag("RailwayNotFound", () => Effect.undefined),
605 );
606
607 const getInstance = (environmentId: string, serviceId: string) =>
608 readInstance(environmentId, serviceId).pipe(
609 Effect.map((instance) => (isGoneInstance(instance) ? undefined : instance)),
610 Effect.catchTag("RailwayNotFound", () => Effect.undefined),
611 );
612
613 const listProjectServices = (projectId: string) =>
614 projectServices(projectId, serviceFields).pipe(
615 Effect.map((services) => services.filter((node) => !isGoneService(node))),
616 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudService[])),
617 );
618
619 const findByName = (projectId: string, name: string) =>
620 listProjectServices(projectId).pipe(
621 Effect.map((services) => services.find((service) => service.name === name)),
622 );
623
624 const getVolumeByInstanceId = (volumeInstanceId: string) =>
625 readVolumeInstance(volumeInstanceId).pipe(
626 Effect.map((instance) => (isGoneVolume(instance) ? undefined : instance)),
627 Effect.catchTag("RailwayNotFound", () => Effect.undefined),
628 );
629
630 const listVolumeInstances = (environmentId: string, projectId: string) =>
631 environmentVolumes(environmentId, projectId, volumeFields).pipe(
632 Effect.map((instances) => instances.filter((node) => !isGoneVolume(node))),
633 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudInstance[])),
634 );
635
636 const findVolume = (
637 environmentId: string,
638 projectId: string,
639 match: (instance: CloudInstance) => boolean,
640 ) =>
641 listVolumeInstances(environmentId, projectId).pipe(
642 Effect.map((instances) => instances.find(match)),
643 );
644
645 const listProxies = (environmentId: string, serviceId: string) =>
646 readProxies(environmentId, serviceId).pipe(
647 Effect.map((items) => items.filter((proxy) => !isGoneProxy(proxy))),
648 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudProxy[])),
649 );
650
651 const findProxy = (environmentId: string, serviceId: string, applicationPort: number) =>
652 listProxies(environmentId, serviceId).pipe(
653 Effect.map((items) => items.find((proxy) => proxy.applicationPort === applicationPort)),
654 );
655
656 /**
657 * Delete a TCP proxy, riding out Railway's per-environment mutation lock: a
658 * delete racing an in-flight deploy fails with "Cannot delete TCP proxy: an
659 * operation is already in progress". Already-gone proxies are a no-op.
660 */
661 const deleteProxy = (id: string) =>
662 tcpProxyDelete(id).pipe(
663 Effect.retry({
664 while: (e) => e._tag === "RailwayOperationInProgress",
665 schedule: Schedule.spaced("3 seconds"),
666 times: 10,
667 }),
668 Effect.catchTag("RailwayNotFound", () => Effect.void),
669 Effect.asVoid,
670 );
671
672 const asVariableMap = (value: unknown): Record<string, string> => {
673 if (value === null || typeof value !== "object" || Array.isArray(value)) {
674 return {};
675 }
676 const out: Record<string, string> = {};
677 for (const [key, item] of Object.entries(value)) {
678 if (typeof item === "string") {
679 out[key] = item;
680 }
681 }
682 return out;
683 };
684
685 const listVariableMap = (projectId: string, environmentId: string, serviceId: string) =>
686 readVariables(projectId, environmentId, serviceId).pipe(
687 Effect.map(asVariableMap),
688 Effect.catchTag("RailwayNotFound", () => Effect.succeed({} as Record<string, string>)),
689 );
690
691 const upsertVariable = (input: {
692 projectId: string;
693 environmentId: string;
694 serviceId: string;
695 name: string;
696 value: string;
697 }) =>
698 variableUpsert({
699 projectId: input.projectId,
700 environmentId: input.environmentId,
701 serviceId: input.serviceId,
702 name: input.name,
703 value: input.value,
704 skipDeploys: true,
705 });
706
707 const syncEnv = Effect.fn(function* (input: {
708 projectId: string;
709 environmentId: string;
710 serviceId: string;
711 desired: Record<string, string>;
712 }) {
713 const observed = yield* listVariableMap(input.projectId, input.environmentId, input.serviceId);
714 let changed = false;
715 for (const [name, value] of Object.entries(input.desired)) {
716 if (observed[name] !== value) {
717 yield* upsertVariable({
718 projectId: input.projectId,
719 environmentId: input.environmentId,
720 serviceId: input.serviceId,
721 name,
722 value,
723 });
724 changed = true;
725 }
726 }
727 return changed;
728 });
729
730 const waitForInstance = (environmentId: string, serviceId: string) =>
731 getInstance(environmentId, serviceId).pipe(
732 Effect.flatMap((instance) => {
733 if (instance === undefined) {
734 return Effect.fail(new PostgresPending({ serviceId, status: "creating" }));
735 }
736 return Effect.succeed(instance);
737 }),
738 Effect.retry({
739 while: (e) => e._tag === "Railway.PostgresPending",
740 // serviceCreate fans the instance out to each environment
741 // asynchronously; wait a bounded interval for it to appear.
742 times: 10,
743 schedule: Schedule.spaced("2 seconds"),
744 }),
745 Effect.catchTag("Railway.PostgresPending", () => getInstance(environmentId, serviceId)),
746 );
747
748 const waitForDeployment = (environmentId: string, serviceId: string) =>
749 getInstance(environmentId, serviceId).pipe(
750 Effect.flatMap((instance) => {
751 const latest = instance?.latestDeployment;
752 const status = latest?.status;
753 if (instance !== undefined && deployReady(status)) {
754 return Effect.succeed(instance);
755 }
756 return Effect.fail(new PostgresDeployPending({ serviceId, status: status ?? "pending" }));
757 }),
758 Effect.retry({
759 while: (e) => e._tag === "Railway.PostgresDeployPending",
760 times: 10,
761 schedule: Schedule.spaced("5 seconds"),
762 }),
763 Effect.catchTag("Railway.PostgresDeployPending", () => getInstance(environmentId, serviceId)),
764 );
765
766 const waitForVolume = (
767 environmentId: string,
768 projectId: string,
769 volumeId: string,
770 volumeInstanceId?: string,
771 ) => {
772 const observe =
773 volumeInstanceId !== undefined && volumeInstanceId.length > 0
774 ? getVolumeByInstanceId(volumeInstanceId)
775 : findVolume(environmentId, projectId, (instance) => instance.volumeId === volumeId);
776 return observe.pipe(
777 Effect.flatMap((instance) => {
778 if (instance === undefined || transientVolumeState(instance.state)) {
779 return Effect.fail(new VolumePending({ volumeId, state: instance?.state ?? "creating" }));
780 }
781 return Effect.succeed(instance);
782 }),
783 Effect.retry({
784 while: (e) => e._tag === "Railway.PostgresVolumePending",
785 times: 10,
786 // Volume attachment can lag creation beyond 30 seconds.
787 // Keep the wait bounded and retain the volume ID/state on exhaustion.
788 schedule: Schedule.spaced("5 seconds"),
789 }),
790 );
791 };
792
793 const stampVolumeName = (volumeId: string, name: string) => volumeUpdateName(volumeId, name);
794
795 export const PostgresProvider = () =>
796 Provider.succeed(Postgres, {
797 stables: ["serviceId", "projectId", "environmentId", "volumeId"],
798 nuke: { dependsOn: ["Railway.Project"] },
799
800 diff: Effect.fn(function* ({ news, output }) {
801 if (news === undefined || !isResolved(news)) return undefined;
802 if (output === undefined) return undefined;
803 const nextProject = projectIdOf(news.project);
804 const projectChanged = nextProject !== undefined && nextProject !== output.projectId;
805 const nextEnv = environmentIdOf(news.environment);
806 const environmentChanged = nextEnv !== undefined && nextEnv !== output.environmentId;
807 const regionChanged = news.region !== undefined && news.region !== output.region;
808 if (projectChanged || environmentChanged || regionChanged) {
809 return { action: "replace" as const };
810 }
811 return undefined;
812 }),
813
814 read: Effect.fn(function* ({ id, olds, output }) {
815 const projectId =
816 output?.projectId ?? (olds !== undefined ? projectIdOf(olds.project) : undefined);
817 const environmentId =
818 output?.environmentId ??
819 (olds !== undefined
820 ? (environmentIdOf(olds.environment) ?? environmentIdOf(olds.project))
821 : undefined);
822 const name = yield* resolveName(id, olds?.name, output?.name);
823 const byId =
824 output?.serviceId !== undefined && output.serviceId.length > 0
825 ? yield* getById(output.serviceId)
826 : undefined;
827 const found =
828 byId ?? (projectId !== undefined ? yield* findByName(projectId, name) : undefined);
829 if (found === undefined) return undefined;
830 const resolvedProjectId = projectIdOf(found) ?? projectId ?? "";
831 const resolvedEnvId =
832 environmentId ?? environmentIdOf(olds?.project) ?? output?.environmentId ?? "";
833 const instance =
834 resolvedEnvId.length > 0 ? yield* getInstance(resolvedEnvId, found.id) : undefined;
835 const volume =
836 output?.volumeInstanceId !== undefined && output.volumeInstanceId.length > 0
837 ? yield* getVolumeByInstanceId(output.volumeInstanceId)
838 : resolvedEnvId.length > 0 && resolvedProjectId.length > 0
839 ? yield* findVolume(
840 resolvedEnvId,
841 resolvedProjectId,
842 (row) =>
843 (row.serviceId ?? undefined) === found.id ||
844 (output?.volumeId !== undefined && row.volumeId === output.volumeId),
845 )
846 : undefined;
847 const proxy =
848 resolvedEnvId.length > 0
849 ? yield* findProxy(resolvedEnvId, found.id, POSTGRES_PORT)
850 : undefined;
851 const vars =
852 resolvedProjectId.length > 0 && resolvedEnvId.length > 0
853 ? yield* listVariableMap(resolvedProjectId, resolvedEnvId, found.id)
854 : {};
855 const attrs = toAttrs({
856 service: found,
857 instance,
858 volume,
859 proxy,
860 projectId: resolvedProjectId,
861 environmentId: resolvedEnvId,
862 user: vars.POSTGRES_USER ?? output?.user ?? DEFAULT_POSTGRES_USER,
863 password: vars.POSTGRES_PASSWORD ?? "",
864 database: vars.POSTGRES_DB ?? output?.database ?? DEFAULT_POSTGRES_DATABASE,
865 });
866 if (output !== undefined) return attrs;
867 return matchesAlchemyPhysicalName(found.name) ? attrs : Unowned(attrs);
868 }),
869
870 list: Effect.fn(function* () {
871 const projects = yield* ownedProjects();
872 const rows = yield* Effect.forEach(projects, (project) =>
873 Effect.gen(function* () {
874 const services = new Map(
875 (yield* listProjectServices(project.projectId))
876 .filter((service) => matchesAlchemyPhysicalName(service.name))
877 .map((service) => [service.id, service]),
878 );
879 if (services.size === 0) return [];
880 const envIds = yield* liveEnvironmentIds(project.projectId).pipe(
881 Stream.filter((env) => env.deletedAt == null),
882 Stream.map((env) => env.id),
883 Stream.runCollect,
884 Effect.catchTag("RailwayNotFound", () => Effect.succeed([])),
885 );
886 const items = yield* Effect.forEach(envIds, (environmentId) =>
887 Effect.gen(function* () {
888 const instances = (yield* environmentServiceInstances(
889 environmentId,
890 project.projectId,
891 (instance) => ({
892 ...attributeInstanceFields(instance),
893 serviceId: instance.serviceId,
894 deletedAt: instance.deletedAt,
895 }),
896 )).filter(
897 (instance) =>
898 instance.deletedAt == null &&
899 services.has(instance.serviceId) &&
900 isPostgresImage(instance.source?.image),
901 );
902 if (instances.length === 0) return [];
903 const volumes = yield* listVolumeInstances(environmentId, project.projectId);
904 return instances.flatMap((instance) => {
905 const service = services.get(instance.serviceId);
906 return service === undefined
907 ? []
908 : [
909 toAttrs({
910 service,
911 instance,
912 projectId: project.projectId,
913 environmentId,
914 volume: volumes.find((row) => row.serviceId === service.id),
915 proxy: undefined,
916 user: DEFAULT_POSTGRES_USER,
917 password: "",
918 database: DEFAULT_POSTGRES_DATABASE,
919 }),
920 ];
921 });
922 }),
923 );
924 return items.flat();
925 }),
926 );
927 return rows.flat();
928 }),
929
930 reconcile: Effect.fn(function* ({ id, news, output }) {
931 const props = news ?? ({} as PostgresProps);
932 const projectId = projectIdOf(props.project) ?? output?.projectId;
933 if (projectId === undefined) {
934 return yield* new PostgresProjectRequired({
935 message: "Postgres requires a resolved Railway.Project",
936 });
937 }
938 const environmentId =
939 environmentIdOf(props.environment) ??
940 environmentIdOf(props.project) ??
941 output?.environmentId;
942 if (environmentId === undefined) {
943 return yield* new PostgresProjectRequired({
944 message:
945 "Postgres requires a Railway environment (pass environment or a Project with environmentId)",
946 });
947 }
948 const name = yield* resolveName(id, props.name, output?.name);
949 const sourceImage = props.image ?? DEFAULT_POSTGRES_IMAGE;
950 const wantPublic = props.public !== false;
951 const volumeName = yield* createRailwayName(`${id}-pgdata`);
952
953 let current: CloudService | undefined =
954 output?.serviceId !== undefined && output.serviceId.length > 0
955 ? yield* getById(output.serviceId)
956 : undefined;
957 if (current === undefined) {
958 current = yield* findByName(projectId, name);
959 }
960
961 const existingVars =
962 current !== undefined ? yield* listVariableMap(projectId, environmentId, current.id) : {};
963 const user = existingVars.POSTGRES_USER ?? props.user ?? DEFAULT_POSTGRES_USER;
964 const database = existingVars.POSTGRES_DB ?? props.database ?? DEFAULT_POSTGRES_DATABASE;
965 const password =
966 existingVars.POSTGRES_PASSWORD ??
967 (props.password !== undefined ? unwrapSecret(props.password) : undefined) ??
968 (yield* generatePassword);
969 const variables = desiredVariables({ user, password, database });
970
971 if (current === undefined) {
972 const created = yield* serviceCreate({
973 projectId,
974 environmentId,
975 name,
976 source: { image: sourceImage },
977 variables,
978 }).pipe(Effect.catchTag("RailwayValidationError", () => Effect.undefined));
979 current = created ?? (yield* findByName(projectId, name));
980 }
981
982 if (current === undefined || isGoneService(current)) {
983 return yield* new PostgresNotCreated({ resourceName: name, projectId });
984 }
985
986 if (current.name !== name) {
987 current = yield* serviceUpdateName(current.id, name);
988 }
989
990 let instance = yield* waitForInstance(environmentId, current.id);
991 let needsDeploy = false;
992
993 const observedImage = instance?.source?.image ?? undefined;
994 const imageChanged = sourceImage !== undefined && !sameImage(observedImage, sourceImage);
995 const observedRegion = instance?.region ?? undefined;
996 const regionChanged = props.region !== undefined && props.region !== observedRegion;
997 const sleepOn = instance?.sleepApplication !== false;
998 if (imageChanged || regionChanged || sleepOn) {
999 yield* serviceInstanceUpdate(environmentId, current.id, {
1000 ...(imageChanged ? { source: { image: sourceImage } } : {}),
1001 ...(regionChanged ? { region: props.region } : {}),
1002 ...(sleepOn ? { sleepApplication: false } : {}),
1003 });
1004 needsDeploy = true;
1005 instance = (yield* getInstance(environmentId, current.id)) ?? instance;
1006 }
1007
1008 const envChanged = yield* syncEnv({
1009 projectId,
1010 environmentId,
1011 serviceId: current.id,
1012 desired: variables,
1013 });
1014 if (envChanged) needsDeploy = true;
1015
1016 let volume: CloudInstance | undefined =
1017 output?.volumeInstanceId !== undefined && output.volumeInstanceId.length > 0
1018 ? yield* getVolumeByInstanceId(output.volumeInstanceId)
1019 : undefined;
1020 if (volume === undefined && output?.volumeId !== undefined) {
1021 volume = yield* findVolume(
1022 environmentId,
1023 projectId,
1024 (row) => row.volumeId === output.volumeId,
1025 );
1026 }
1027 if (volume === undefined) {
1028 volume = yield* findVolume(
1029 environmentId,
1030 projectId,
1031 (row) => (row.serviceId ?? undefined) === current!.id || row.volume.name === volumeName,
1032 );
1033 }
1034 if (volume === undefined) {
1035 const created = yield* volumeCreate({
1036 projectId,
1037 environmentId,
1038 mountPath: POSTGRES_MOUNT_PATH,
1039 serviceId: current.id,
1040 ...(props.region !== undefined ? { region: props.region } : {}),
1041 });
1042 if (created.name !== volumeName) {
1043 yield* stampVolumeName(created.id, volumeName);
1044 }
1045 volume = yield* waitForVolume(environmentId, projectId, created.id);
1046 }
1047 if (volume === undefined || isGoneVolume(volume)) {
1048 return yield* new PostgresVolumeNotCreated({
1049 resourceName: volumeName,
1050 serviceId: current.id,
1051 });
1052 }
1053 if (volume.volume.name !== volumeName) {
1054 yield* stampVolumeName(volume.volumeId, volumeName);
1055 }
1056 const observedMount = volume.mountPath;
1057 const observedServiceId = volume.serviceId ?? undefined;
1058 const mountChanged = observedMount !== POSTGRES_MOUNT_PATH;
1059 const attached = observedServiceId === current.id;
1060 if (mountChanged || !attached) {
1061 yield* volumeInstanceUpdate(volume.volumeId, environmentId, {
1062 ...(mountChanged ? { mountPath: POSTGRES_MOUNT_PATH } : {}),
1063 ...(!attached ? { serviceId: current.id } : {}),
1064 });
1065 volume = (yield* waitForVolume(environmentId, projectId, volume.volumeId)) ?? volume;
1066 needsDeploy = true;
1067 }
1068
1069 let proxy = yield* findProxy(environmentId, current.id, POSTGRES_PORT);
1070 if (wantPublic && proxy === undefined) {
1071 const created = yield* tcpProxyCreate({
1072 applicationPort: POSTGRES_PORT,
1073 environmentId,
1074 serviceId: current.id,
1075 }).pipe(Effect.catchTag("RailwayValidationError", () => Effect.undefined));
1076 proxy =
1077 created !== undefined && !isGoneProxy(created)
1078 ? created
1079 : yield* findProxy(environmentId, current.id, POSTGRES_PORT);
1080 }
1081 if (!wantPublic && proxy !== undefined) {
1082 yield* deleteProxy(proxy.id);
1083 proxy = undefined;
1084 }
1085
1086 if (needsDeploy || instance?.latestDeployment == null) {
1087 yield* serviceInstanceDeploy(environmentId, current.id).pipe(
1088 Effect.catchTag("RailwayValidationError", () => Effect.void),
1089 );
1090 }
1091
1092 instance = (yield* waitForDeployment(environmentId, current.id)) ?? instance;
1093 let finalStatus = instance?.latestDeployment?.status;
1094 // A deployment can wedge in DEPLOYING and never reach SUCCESS — the
1095 // container may serve, but Railway keeps its per-environment operation
1096 // lock and the TCP proxy's routing is not committed. Converge: cancel
1097 // the wedged deployment, redeploy once, and insist on SUCCESS.
1098 if (!deployFailed(finalStatus) && !deployReady(finalStatus)) {
1099 const wedged = instance?.latestDeployment?.id;
1100 if (wedged != null && wedged.length > 0) {
1101 yield* deploymentCancel(wedged).pipe(
1102 Effect.catchTag("RailwayNotFound", () => Effect.void),
1103 );
1104 }
1105 yield* serviceInstanceDeploy(environmentId, current.id).pipe(
1106 Effect.catchTag("RailwayValidationError", () => Effect.void),
1107 );
1108 instance = (yield* waitForDeployment(environmentId, current.id)) ?? instance;
1109 finalStatus = instance?.latestDeployment?.status;
1110 }
1111 if (deployFailed(finalStatus) || !deployReady(finalStatus)) {
1112 return yield* new PostgresDeployFailed({
1113 serviceId: current.id,
1114 status: finalStatus ?? "failed",
1115 deploymentId: instance?.latestDeployment?.id,
1116 });
1117 }
1118
1119 return toAttrs({
1120 service: current,
1121 instance,
1122 volume,
1123 proxy,
1124 projectId,
1125 environmentId,
1126 user,
1127 password,
1128 database,
1129 });
1130 }),
1131
1132 delete: Effect.fn(function* ({ output }) {
1133 const serviceId = output.serviceId;
1134 const environmentId = output.environmentId;
1135 // Cancel a still-running deployment first: it holds Railway's
1136 // per-environment operation lock ("Cannot delete TCP proxy: an
1137 // operation is already in progress") and can stall the service
1138 // teardown indefinitely. A finished deployment makes this a no-op.
1139 if (environmentId.length > 0 && serviceId.length > 0) {
1140 const instance = yield* getInstance(environmentId, serviceId);
1141 const latest = instance?.latestDeployment;
1142 if (
1143 latest?.id != null &&
1144 latest.id.length > 0 &&
1145 !deployReady(latest.status) &&
1146 !deployFailed(latest.status)
1147 ) {
1148 yield* deploymentCancel(latest.id).pipe(
1149 Effect.catchTag("RailwayNotFound", () => Effect.void),
1150 );
1151 }
1152 }
1153 // Delete the SERVICE next — its teardown cascades onto the proxies.
1154 if (serviceId.length > 0) {
1155 yield* serviceDelete(serviceId).pipe(Effect.catchTag("RailwayNotFound", () => Effect.void));
1156 yield* waitUntilDeleted(
1157 "Service",
1158 serviceId,
1159 getById(serviceId).pipe(Effect.map((service) => service === undefined)),
1160 );
1161 }
1162 // Proxies usually disappear with the service; wait for the cascade
1163 // instead of fighting the teardown's lock, then force-delete any
1164 // survivor (which no-ops on NotFound).
1165 if (environmentId.length > 0 && serviceId.length > 0) {
1166 const leftover = yield* listProxies(environmentId, serviceId).pipe(
1167 Effect.repeat({
1168 schedule: Schedule.spaced("3 seconds"),
1169 until: (rows) => rows.length === 0,
1170 times: 10,
1171 }),
1172 );
1173 yield* Effect.forEach(leftover, (proxy) => deleteProxy(proxy.id), { concurrency: 4 });
1174 yield* waitUntilDeleted(
1175 "TcpProxy",
1176 serviceId,
1177 listProxies(environmentId, serviceId).pipe(Effect.map((proxies) => proxies.length === 0)),
1178 );
1179 } else if (output.tcpProxyId !== undefined && output.tcpProxyId.length > 0) {
1180 yield* deleteProxy(output.tcpProxyId);
1181 }
1182 if (output.volumeId.length > 0) {
1183 yield* volumeDelete(output.volumeId).pipe(
1184 Effect.catchTag("RailwayNotFound", () => Effect.void),
1185 );
1186 const check =
1187 output.volumeInstanceId.length > 0
1188 ? getVolumeByInstanceId(output.volumeInstanceId).pipe(
1189 Effect.map((instance) => instance === undefined),
1190 )
1191 : Effect.succeed(true);
1192 yield* waitUntilDeleted("Volume", output.volumeId, check);
1193 }
1194 }),
1195 });