Open History to see the last commit
1/**
2 * MigrationsLive - Migration runner with inline loader
3 *
4 * Uses Migrator.make with fromRecord to define migrations inline.
5 * All migrations are statically imported - no dynamic file system loading.
6 *
7 * Migrations run automatically when the MigrationLayer is provided,
8 * ensuring the database schema is always up-to-date before the application starts.
9 */
10
11import * as Migrator from "effect/unstable/sql/Migrator";
12import * as Layer from "effect/Layer";
13
14// Import all migrations statically
15import Migration0001 from "./Migrations/001_OrchestrationEvents.ts";
16import Migration0002 from "./Migrations/002_OrchestrationCommandReceipts.ts";
17import Migration0003 from "./Migrations/003_CheckpointDiffBlobs.ts";
18import Migration0004 from "./Migrations/004_ProviderSessionRuntime.ts";
19import Migration0005 from "./Migrations/005_Projections.ts";
20import Migration0006 from "./Migrations/006_ProjectionThreadSessionRuntimeModeColumns.ts";
21
22/**
23 * Migration loader with all migrations defined inline.
24 *
25 * Key format: "{id}_{name}" where:
26 * - id: numeric migration ID (determines execution order)
27 * - name: descriptive name for the migration
28 *
29 * Uses Migrator.fromRecord which parses the key format and
30 * returns migrations sorted by ID.
31 */
32const loader = Migrator.fromRecord({
33 "1_OrchestrationEvents": Migration0001,
34 "2_OrchestrationCommandReceipts": Migration0002,
35 "3_CheckpointDiffBlobs": Migration0003,
36 "4_ProviderSessionRuntime": Migration0004,
37 "5_Projections": Migration0005,
38 "6_ProjectionThreadSessionRuntimeModeColumns": Migration0006,
39});
40
41/**
42 * Migrator run function - no schema dumping needed
43 * Uses the base Migrator.make without platform dependencies
44 */
45const run = Migrator.make({});
46
47/**
48 * Run all pending migrations.
49 *
50 * Creates the migrations tracking table (effect_sql_migrations) if it doesn't exist,
51 * then runs any migrations with ID greater than the latest recorded migration.
52 *
53 * Returns array of [id, name] tuples for migrations that were run.
54 *
55 * @returns Effect containing array of executed migrations
56 */
57export const runMigrations = run({ loader });
58
59/**
60 * Layer that runs migrations when the layer is built.
61 *
62 * Use this to ensure migrations run before your application starts.
63 * Migrations are run automatically - no separate script is needed.
64 *
65 * @example
66 * ```typescript
67 * import { MigrationsLive } from "@acme/db/Migrations"
68 * import * as SqliteClient from "@acme/db/SqliteClient"
69 *
70 * // Migrations run automatically when SqliteClient is provided
71 * const AppLayer = MigrationsLive.pipe(
72 * Layer.provideMerge(SqliteClient.layer({ filename: "database.sqlite" }))
73 * )
74 * ```
75 */
76export const MigrationsLive = Layer.effectDiscard(runMigrations);