Skip to content
Source
Sign inSign up
Open History to see the last commit

MigrationRunnerHandler.ts

35 lines1.5 KB

1import * as Cause from "effect/Cause";
2import * as Effect from "effect/Effect";
3import * as Exit from "effect/Exit";
4import { withMigrationConnection, type MigrationConnectionTarget } from "./MigrationConnection.ts";
5​
6/**
7 * One SQL operation the deploying machine asks the in-VPC runner to perform.
8 * `batch` runs its statements in one transaction.
9 */
10export type MigrationRunnerRequest =
11 | { op: "query"; target: MigrationConnectionTarget; sql: string }
12 | { op: "batch"; target: MigrationConnectionTarget; statements: ReadonlyArray<string> };
13​
14export type MigrationRunnerResponse =
15 | { ok: true; rows: Array<Record<string, unknown>> }
16 | { ok: false; message: string };
17​
18/**
19 * Entry point of the `AWS.RDS.MigrationRunner` Lambda. Each invocation
20 * opens one connection, runs one query or one transactional batch, and
21 * closes the connection. Failures return `{ ok: false }` so the deploying
22 * machine reports the database's own error message.
23 */
24export const handler = async (event: MigrationRunnerRequest): Promise<MigrationRunnerResponse> => {
25 const exit = await Effect.runPromiseExit(
26 withMigrationConnection(event.target, (executor) =>
27 event.op === "query"
28 ? executor.query(event.sql)
29 : executor.batch(event.statements).pipe(Effect.as([])),
30 ),
31 );
32 if (Exit.isSuccess(exit)) return { ok: true, rows: exit.value };
33 const error = Cause.squash(exit.cause);
34 return { ok: false, message: error instanceof Error ? error.message : Cause.pretty(exit.cause) };
35};