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";
21import Migration0007 from "./Migrations/007_ProjectionThreadMessageAttachments.ts";
22
23/**
24 * Migration loader with all migrations defined inline.
25 *
26 * Key format: "{id}_{name}" where:
27 * - id: numeric migration ID (determines execution order)
28 * - name: descriptive name for the migration
29 *
30 * Uses Migrator.fromRecord which parses the key format and
31 * returns migrations sorted by ID.
32 */
33const loader = Migrator.fromRecord({
34 "1_OrchestrationEvents": Migration0001,
35 "2_OrchestrationCommandReceipts": Migration0002,
36 "3_CheckpointDiffBlobs": Migration0003,
37 "4_ProviderSessionRuntime": Migration0004,
38 "5_Projections": Migration0005,
39 "6_ProjectionThreadSessionRuntimeModeColumns": Migration0006,
40 "7_ProjectionThreadMessageAttachments": Migration0007,
41});
42
43/**
44 * Migrator run function - no schema dumping needed
45 * Uses the base Migrator.make without platform dependencies
46 */
47const run = Migrator.make({});
48
49/**
50 * Run all pending migrations.
51 *
52 * Creates the migrations tracking table (effect_sql_migrations) if it doesn't exist,
53 * then runs any migrations with ID greater than the latest recorded migration.
54 *
55 * Returns array of [id, name] tuples for migrations that were run.
56 *
57 * @returns Effect containing array of executed migrations
58 */
59export const runMigrations = run({ loader });
60
61/**
62 * Layer that runs migrations when the layer is built.
63 *
64 * Use this to ensure migrations run before your application starts.
65 * Migrations are run automatically - no separate script is needed.
66 *
67 * @example
68 * ```typescript
69 * import { MigrationsLive } from "@acme/db/Migrations"
70 * import * as SqliteClient from "@acme/db/SqliteClient"
71 *
72 * // Migrations run automatically when SqliteClient is provided
73 * const AppLayer = MigrationsLive.pipe(
74 * Layer.provideMerge(SqliteClient.layer({ filename: "database.sqlite" }))
75 * )
76 * ```
77 */
78export const MigrationsLive = Layer.effectDiscard(runMigrations);