1import * as Context from "effect/Context";
2import * as Layer from "effect/Layer";
3import { makeEntrypointLayer } from "../../Runtime.ts";
4import { Self } from "../../Self.ts";
5import type { MigrationRecord } from "../../SQL/Migrations/Format.ts";
6
7/** SQL files captured during construction and embedded in the Worker bundle. */
8export interface SqlMigrationSnapshot {
9 readonly _tag: "Cloudflare.SqlMigrations";
10 /** The per-instance applied-migrations table. */
11 readonly table: string;
12 /** Ordered SQL records, including their content hashes. */
13 readonly records: ReadonlyArray<MigrationRecord>;
14}
15
16/** @internal */
17export interface SqlMigrationsExport {
18 readonly kind: "sqlMigrations";
19 readonly snapshot: SqlMigrationSnapshot;
20}
21
22export class SqlMigrationsRuntime extends Context.Service<
23 SqlMigrationsRuntime,
24 Readonly<Record<string, SqlMigrationSnapshot>>
25>()("Cloudflare.SqlMigrationsRuntime") {}
26
27/** Supply the generated migration records to every entrypoint and object activation. */
28export const withSqlMigrations = (
29 entrypoint: unknown,
30 migrations: Readonly<Record<string, SqlMigrationSnapshot>>,
31) =>
32 makeEntrypointLayer(Self, entrypoint).pipe(
33 Layer.provideMerge(Layer.succeed(SqlMigrationsRuntime, migrations)),
34 );