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

Migrations.ts

101 lines4.2 KB

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