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";
22import Migration0008 from "./Migrations/008_ProjectionThreadActivitySequence.ts";
23import Migration0009 from "./Migrations/009_ProviderSessionRuntimeMode.ts";
24import Migration0010 from "./Migrations/010_ProjectionThreadsRuntimeMode.ts";
25import Migration0011 from "./Migrations/011_OrchestrationThreadCreatedRuntimeMode.ts";
26import Migration0012 from "./Migrations/012_ProjectionThreadsInteractionMode.ts";
27import Migration0013 from "./Migrations/013_ProjectionThreadProposedPlans.ts";
28import Migration0014 from "./Migrations/014_ProjectionThreadProposedPlanImplementation.ts";
29import Migration0015 from "./Migrations/015_ProjectionTurnsSourceProposedPlan.ts";
30import { Effect } from "effect";
31
32/**
33 * Migration loader with all migrations defined inline.
34 *
35 * Key format: "{id}_{name}" where:
36 * - id: numeric migration ID (determines execution order)
37 * - name: descriptive name for the migration
38 *
39 * Uses Migrator.fromRecord which parses the key format and
40 * returns migrations sorted by ID.
41 */
42const loader = Migrator.fromRecord({
43 "1_OrchestrationEvents": Migration0001,
44 "2_OrchestrationCommandReceipts": Migration0002,
45 "3_CheckpointDiffBlobs": Migration0003,
46 "4_ProviderSessionRuntime": Migration0004,
47 "5_Projections": Migration0005,
48 "6_ProjectionThreadSessionRuntimeModeColumns": Migration0006,
49 "7_ProjectionThreadMessageAttachments": Migration0007,
50 "8_ProjectionThreadActivitySequence": Migration0008,
51 "9_ProviderSessionRuntimeMode": Migration0009,
52 "10_ProjectionThreadsRuntimeMode": Migration0010,
53 "11_OrchestrationThreadCreatedRuntimeMode": Migration0011,
54 "12_ProjectionThreadsInteractionMode": Migration0012,
55 "13_ProjectionThreadProposedPlans": Migration0013,
56 "14_ProjectionThreadProposedPlanImplementation": Migration0014,
57 "15_ProjectionTurnsSourceProposedPlan": Migration0015,
58});
59
60/**
61 * Migrator run function - no schema dumping needed
62 * Uses the base Migrator.make without platform dependencies
63 */
64const run = Migrator.make({});
65
66/**
67 * Run all pending migrations.
68 *
69 * Creates the migrations tracking table (effect_sql_migrations) if it doesn't exist,
70 * then runs any migrations with ID greater than the latest recorded migration.
71 *
72 * Returns array of [id, name] tuples for migrations that were run.
73 *
74 * @returns Effect containing array of executed migrations
75 */
76export const runMigrations = Effect.gen(function* () {
77 yield* Effect.log("Running migrations...");
78 yield* run({ loader });
79 yield* Effect.log("Migrations ran successfully");
80});
81
82/**
83 * Layer that runs migrations when the layer is built.
84 *
85 * Use this to ensure migrations run before your application starts.
86 * Migrations are run automatically - no separate script is needed.
87 *
88 * @example
89 * ```typescript
90 * import { MigrationsLive } from "@acme/db/Migrations"
91 * import * as SqliteClient from "@acme/db/SqliteClient"
92 *
93 * // Migrations run automatically when SqliteClient is provided
94 * const AppLayer = MigrationsLive.pipe(
95 * Layer.provideMerge(SqliteClient.layer({ filename: "database.sqlite" }))
96 * )
97 * ```
98 */
99export const MigrationsLive = Layer.effectDiscard(runMigrations);