Julius Marmingemigrate to single turn store1d975ad8 months agoHistory
74 lines · 2.5 KB · TypeScript
RawDownload
/** * MigrationsLive - Migration runner with inline loader * * Uses Migrator.make with fromRecord to define migrations inline. * All migrations are statically imported - no dynamic file system loading. * * Migrations run automatically when the MigrationLayer is provided, * ensuring the database schema is always up-to-date before the application starts. */ import * as Migrator from "effect/unstable/sql/Migrator";import * as Layer from "effect/Layer"; // Import all migrations staticallyimport Migration0001 from "./Migrations/001_OrchestrationEvents.ts";import Migration0002 from "./Migrations/002_OrchestrationCommandReceipts.ts";import Migration0003 from "./Migrations/003_CheckpointDiffBlobs.ts";import Migration0004 from "./Migrations/004_ProviderSessionRuntime.ts";import Migration0005 from "./Migrations/005_Projections.ts"; /** * Migration loader with all migrations defined inline. * * Key format: "{id}_{name}" where: * - id: numeric migration ID (determines execution order) * - name: descriptive name for the migration * * Uses Migrator.fromRecord which parses the key format and * returns migrations sorted by ID. */const loader = Migrator.fromRecord({  "1_OrchestrationEvents": Migration0001,  "2_OrchestrationCommandReceipts": Migration0002,  "3_CheckpointDiffBlobs": Migration0003,  "4_ProviderSessionRuntime": Migration0004,  "5_Projections": Migration0005,}); /** * Migrator run function - no schema dumping needed * Uses the base Migrator.make without platform dependencies */const run = Migrator.make({}); /** * Run all pending migrations. * * Creates the migrations tracking table (effect_sql_migrations) if it doesn't exist, * then runs any migrations with ID greater than the latest recorded migration. * * Returns array of [id, name] tuples for migrations that were run. * * @returns Effect containing array of executed migrations */export const runMigrations = run({ loader }); /** * Layer that runs migrations when the layer is built. * * Use this to ensure migrations run before your application starts. * Migrations are run automatically - no separate script is needed. * * @example * ```typescript * import { MigrationsLive } from "@acme/db/Migrations" * import * as SqliteClient from "@acme/db/SqliteClient" * * // Migrations run automatically when SqliteClient is provided * const AppLayer = MigrationsLive.pipe( *   Layer.provideMerge(SqliteClient.layer({ filename: "database.sqlite" })) * ) * ``` */export const MigrationsLive = Layer.effectDiscard(runMigrations);