/** * 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/sql/Migrator"; import * as Layer from "effect/Layer"; // Import all migrations statically import Migration0001 from "./Migrations/001_OrchestrationEvents.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, }); /** * 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 { PgClient } from "@effect/sql-pg" * * // Migrations run automatically when PgClient is provided * const AppLayer = MigrationsLive.pipe( * Layer.provideMerge(PgClient.layer({ url: Redacted.make("postgresql://...") })) * ) * ``` */ export const MigrationsLive = Layer.effectDiscard(runMigrations);