import { Migration, MigrationContext, MigrationStatus, MigrationRunnerConfig, MigrationOperation } from './types.js'; import { RelatedDatabase } from './type/related/database.js'; /** * 根据 up 操作自动生成 down 操作 * @internal 仅用于测试 */ export declare function generateReverseOperations(operations: MigrationOperation[]): MigrationOperation[]; /** * 迁移运行器 * 管理数据库迁移的执行、回滚和状态跟踪 * * @example * ```ts * const runner = new MigrationRunner(db); * * // 添加迁移 * runner.add({ * name: '001_create_users', * up: async (ctx) => { * await ctx.createTable('users', { * id: { type: 'integer', primary: true, autoIncrement: true }, * name: { type: 'text', nullable: false } * }); * }, * down: async (ctx) => { * await ctx.dropTable('users'); * } * }); * * // 运行所有待执行的迁移 * await runner.migrate(); * * // 回滚最后一批迁移 * await runner.rollback(); * ``` */ export declare class MigrationRunner = Record> { private readonly database; private migrations; private tableName; private currentBatch; constructor(database: RelatedDatabase, config?: MigrationRunnerConfig); /** * 添加迁移 */ add(migration: Migration): this; /** * 批量添加迁移 */ addAll(migrations: Migration[]): this; /** * 初始化迁移表 */ private ensureMigrationTable; /** * 获取已执行的迁移 */ private getExecutedMigrations; /** * 获取当前批次号 */ private getCurrentBatch; /** * 记录迁移执行 */ private recordMigration; /** * 获取迁移记录的操作 */ private getMigrationOperations; /** * 删除迁移记录 */ private removeMigrationRecord; /** * 创建迁移上下文 */ private createContext; /** * 获取迁移状态 */ status(): Promise; /** * 运行所有待执行的迁移 */ migrate(): Promise; /** * 回滚最后一批迁移 */ rollback(): Promise; /** * 回滚所有迁移 */ reset(): Promise; /** * 重新运行所有迁移(reset + migrate) */ refresh(): Promise<{ rolledBack: string[]; migrated: string[]; }>; } /** * 创建迁移辅助函数 * * @example * // 只需要定义 up,down 会自动生成 * defineMigration({ * name: '001_create_users', * up: async (ctx) => { * await ctx.createTable('users', { * id: { type: 'integer', primary: true, autoIncrement: true }, * name: { type: 'text', nullable: false } * }); * await ctx.addIndex('users', 'idx_name', ['name']); * } * // down 自动生成: dropIndex + dropTable * }); * * // 如果需要自定义 down,可以显式提供 * defineMigration({ * name: '002_custom_migration', * up: async (ctx) => { * await ctx.query('INSERT INTO settings VALUES (?, ?)', ['key', 'value']); * }, * down: async (ctx) => { * await ctx.query('DELETE FROM settings WHERE key = ?', ['key']); * } * }); */ export declare function defineMigration(config: { name: string; version?: string | number; up: (context: MigrationContext) => Promise; down?: (context: MigrationContext) => Promise; }): Migration; //# sourceMappingURL=migration.d.ts.map