Julius Marmingeeffect v4b0c9c2a8 months agoHistory
68 lines · 2.2 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_Projects.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_Projects": Migration0002,}); /** * 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 { SqliteClient } from "@effect/sql-sqlite-node" * * // Migrations run automatically when SqliteClient is provided * const AppLayer = MigrationsLive.pipe( *   Layer.provideMerge(SqliteClient.layer({ filename: "database.sqlite" })) * ) * ``` */export const MigrationsLive = Layer.effectDiscard(runMigrations);