revert_to_migration
Undo applied schema changes by reverting migrations up to a specified target. Requires the target migration name and an array of already applied migration filenames. Set target to empty to revert all.
Instructions
Revert migrations up to a specific target.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| targetMigration | Yes | Name of the migration to revert to (exclusive). Use empty string to revert all. | |
| appliedMigrations | No | Array of already applied migration filenames. |
Implementation Reference
- src/tools/migration-tools.ts:158-176 (registration)Registers the 'revert_to_migration' tool with its name, description, and input schema (targetMigration required, appliedMigrations optional).
{ name: 'revert_to_migration', description: 'Revert migrations up to a specific target.', inputSchema: { type: 'object', properties: { targetMigration: { type: 'string', description: 'Name of the migration to revert to (exclusive). Use empty string to revert all.' }, appliedMigrations: { type: 'array', items: { type: 'string' }, description: 'Array of already applied migration filenames.' }, }, required: ['targetMigration'], }, }, - src/tools/migration-tools.ts:50-53 (schema)TypeScript interface defining the arguments for revert_to_migration: targetMigration (string) and appliedMigrations (optional string array).
interface RevertToMigrationArgs { targetMigration: string; appliedMigrations?: string[]; } - src/tools/migration-tools.ts:184-207 (registration)Route handler that dispatches 'revert_to_migration' tool calls to handleRevertToMigration.
export async function handleMigrationToolCall(name: string, args: any, pb: PocketBase): Promise<ToolResult> { switch (name) { case 'set_migrations_directory': return handleSetMigrationsDirectory(args as SetMigrationsDirectoryArgs); case 'create_migration': return handleCreateMigration(args as CreateMigrationArgs); case 'create_collection_migration': return handleCreateCollectionMigration(args as CreateCollectionMigrationArgs); case 'add_field_migration': return handleAddFieldMigration(args as AddFieldMigrationArgs); case 'list_migrations': return handleListMigrations(args as ListMigrationsArgs); case 'apply_migration': return handleApplyMigration(args as ApplyMigrationArgs, pb); case 'revert_migration': return handleRevertMigration(args as RevertMigrationArgs, pb); case 'apply_all_migrations': return handleApplyAllMigrations(args as ApplyAllMigrationsArgs, pb); case 'revert_to_migration': return handleRevertToMigration(args as RevertToMigrationArgs, pb); default: throw new Error(`Unknown migration tool: ${name}`); } } - src/tools/migration-tools.ts:318-339 (handler)Handler function that validates args, calls the core revertToMigration logic, and returns results or errors.
async function handleRevertToMigration(args: RevertToMigrationArgs, pb: PocketBase): Promise<ToolResult> { if (args.targetMigration === undefined) { throw invalidParamsError("Missing required argument: targetMigration"); } try { const appliedMigrations = args.appliedMigrations || []; const result = await revertToMigration(args.targetMigration, pb, appliedMigrations); if (result.length === 0) { return { content: [{ type: 'text', text: 'No migrations to revert.' }], }; } return { content: [{ type: 'text', text: `Reverted migrations:\n${result.join('\n')}` }], }; } catch (error: any) { throw new Error(`Failed to revert migrations: ${error.message}`); } } - src/migrations/execution.ts:158-200 (helper)Core execution logic: sorts applied migrations in reverse chronological order, determines which to revert (up to targetMigration exclusive), and reverts each via revertMigration.
export async function revertToMigration( targetMigration: string, pb: PocketBase, migrationsDir: string, appliedMigrations: string[] = [] ): Promise<string[]> { try { // If no migrations have been applied, nothing to revert if (appliedMigrations.length === 0) { return []; } // Sort applied migrations in reverse chronological order const sortedMigrations = [...appliedMigrations].sort((a, b) => { const tsA = parseInt(a.split('_')[0], 10); const tsB = parseInt(b.split('_')[0], 10); return tsB - tsA; // Descending order }); const targetIndex = sortedMigrations.indexOf(targetMigration); if (targetIndex === -1 && targetMigration !== '') { throw new Error(`Target migration not found: ${targetMigration}`); } // Determine which migrations to revert const migrationsToRevert = targetMigration === '' ? sortedMigrations // Revert all if target is empty : sortedMigrations.slice(0, targetIndex); const reverted: string[] = []; // Revert each migration for (const migration of migrationsToRevert) { await revertMigration(migration, pb, migrationsDir); reverted.push(migration); } return reverted; } catch (error: any) { console.error('Error reverting migrations:', error); throw new Error(`Failed to revert migrations: ${error.message}`); } }