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

Migrations.ts

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