{"version":3,"file":"migrate.cjs","names":["#migrations","#ledger","#schema"],"sources":["../../../src/batteries/vector/migrate.ts"],"sourcesContent":["/**\n * Knex-style migration runner for the vector battery.\n *\n * @module @nhtio/adk/batteries/vector/migrate\n */\n\nimport { isError } from '@nhtio/adk/guards'\nimport { E_VECTOR_STORE_MIGRATION_FAILED } from './exceptions'\nimport type { VectorSchemaBuilder } from './schema'\n\n/** The context passed to each migration's `up`/`down` — exposes the schema builder. */\nexport interface VectorMigrationContext {\n  /** Schema facade for performing collection DDL within the migration. */\n  schema: VectorSchemaBuilder\n}\n\n/** A single migration module: a name plus its forward (`up`) and reverse (`down`) steps. */\nexport interface VectorMigration {\n  /** Unique migration name, used as its ledger key. */\n  name: string\n  /** Apply the migration. */\n  up: (ctx: VectorMigrationContext) => Promise<void>\n  /** Reverse the migration. */\n  down: (ctx: VectorMigrationContext) => Promise<void>\n}\n\n/** Persistence for which migrations have run; backed by a store-provided implementation. */\nexport interface MigrationLedger {\n  /** Resolve the names of migrations already applied, in application order. */\n  applied(): Promise<string[]>\n  /** Record `name` as applied. */\n  record(name: string): Promise<void>\n  /** Remove `name` from the applied set (on rollback). */\n  remove(name: string): Promise<void>\n}\n\n/** Construction options for a {@link VectorMigrator}. */\nexport interface VectorMigrateOptions {\n  /** The ordered set of migration modules. */\n  migrations: VectorMigration[]\n  /** Ledger tracking which migrations have run. */\n  ledger: MigrationLedger\n  /** Schema facade handed to each migration. */\n  schema: VectorSchemaBuilder\n}\n\n/** Knex-style migration runner: applies pending migrations forward and rolls the last one back. */\nexport class VectorMigrator {\n  readonly #migrations: VectorMigration[]\n  readonly #ledger: MigrationLedger\n  readonly #schema: VectorSchemaBuilder\n\n  /**\n   * @param opts - The migrations, ledger, and schema facade to run against.\n   */\n  constructor(opts: VectorMigrateOptions) {\n    this.#migrations = opts.migrations\n    this.#ledger = opts.ledger\n    this.#schema = opts.schema\n  }\n\n  /**\n   * Apply every not-yet-applied migration in order, recording each in the ledger.\n   *\n   * @returns The names of the migrations applied during this run.\n   * @throws {@link @nhtio/adk/batteries!E_VECTOR_STORE_MIGRATION_FAILED} when a migration's `up` throws.\n   */\n  async latest(): Promise<string[]> {\n    const applied = await this.#ledger.applied()\n    const toApply = this.#migrations.filter((m) => !applied.includes(m.name))\n\n    const appliedThisRun: string[] = []\n\n    for (const migration of toApply) {\n      try {\n        await migration.up({ schema: this.#schema })\n        await this.#ledger.record(migration.name)\n        appliedThisRun.push(migration.name)\n      } catch (err) {\n        const msg = (value: unknown): string => (isError(value) ? value.message : String(value))\n        throw new E_VECTOR_STORE_MIGRATION_FAILED([migration.name, msg(err)])\n      }\n    }\n\n    return appliedThisRun\n  }\n\n  /**\n   * Reverse the most recently applied migration and remove it from the ledger.\n   *\n   * @returns The name of the rolled-back migration, or `null` when nothing was applied.\n   * @throws {@link @nhtio/adk/batteries!E_VECTOR_STORE_MIGRATION_FAILED} when the migration module is missing or\n   *   its `down` throws.\n   */\n  async rollback(): Promise<string | null> {\n    const applied = await this.#ledger.applied()\n\n    if (applied.length === 0) {\n      return null\n    }\n\n    const lastApplied = applied[applied.length - 1]\n    const migration = this.#migrations.find((m) => m.name === lastApplied)\n\n    if (!migration) {\n      throw new E_VECTOR_STORE_MIGRATION_FAILED([lastApplied, 'migration module not found'])\n    }\n\n    try {\n      await migration.down({ schema: this.#schema })\n      await this.#ledger.remove(lastApplied)\n      return lastApplied\n    } catch (err) {\n      const msg = (value: unknown): string => (isError(value) ? value.message : String(value))\n      throw new E_VECTOR_STORE_MIGRATION_FAILED([lastApplied, msg(err)])\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;AA+CA,IAAa,iBAAb,MAA4B;CAC1B;CACA;CACA;;;;CAKA,YAAY,MAA4B;EACtC,KAAKA,cAAc,KAAK;EACxB,KAAKC,UAAU,KAAK;EACpB,KAAKC,UAAU,KAAK;CACtB;;;;;;;CAQA,MAAM,SAA4B;EAChC,MAAM,UAAU,MAAM,KAAKD,QAAQ,QAAQ;EAC3C,MAAM,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