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

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