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

Migrations.ts

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