revert_migration
Revert a specific migration file in PocketBase databases to undo schema changes. Specify the migration file name to execute the rollback process.
Instructions
Revert a specific migration file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| migrationFile | Yes | Name of the migration file to revert. |
Implementation Reference
- src/tools/migration-tools.ts:284-297 (handler)Handler for the 'revert_migration' MCP tool. Validates input parameters and delegates to the core revertMigration function.async function handleRevertMigration(args: RevertMigrationArgs, pb: PocketBase): Promise<ToolResult> { if (!args.migrationFile) { throw invalidParamsError("Missing required argument: migrationFile"); } try { const result = await revertMigration(args.migrationFile, pb); return { content: [{ type: 'text', text: result }], }; } catch (error: any) { throw new Error(`Failed to revert migration: ${error.message}`); } }
- src/tools/migration-tools.ts:134-143 (registration)Registration of the 'revert_migration' tool in the list of migration tools, including its name, description, and JSON schema for input validation.name: 'revert_migration', description: 'Revert a specific migration file.', inputSchema: { type: 'object', properties: { migrationFile: { type: 'string', description: 'Name of the migration file to revert.' }, }, required: ['migrationFile'], }, },
- src/tools/migration-tools.ts:42-44 (schema)TypeScript interface defining the input arguments for the revert_migration handler.interface RevertMigrationArgs { migrationFile: string; }
- src/migrations/execution.ts:86-108 (helper)Core helper function implementing the migration revert logic: loads the JS migration file and executes its down() migration.export async function revertMigration( migrationFile: string, pb: PocketBase, migrationsDir: string ): Promise<string> { try { const migrationPath = path.join(migrationsDir, migrationFile); // Check if file exists await fs.access(migrationPath, fs.constants.R_OK); // Load the migration const migration = await loadMigrationFile(migrationPath); // Execute the down function await migration.down(pb); return `Successfully reverted migration: ${migrationFile}`; } catch (error: any) { console.error(`Error reverting migration ${migrationFile}:`, error); throw new Error(`Failed to revert migration: ${error.message}`); } }
- src/migrations/execution.ts:19-46 (helper)Supporting helper that parses a PocketBase migration JS file to extract and instantiate the up/down functions using regex and new Function.async function loadMigrationFile(migrationPath: string): Promise<MigrationFunctions> { try { // Read the file content const content = await fs.readFile(migrationPath, 'utf-8'); // Extract the up and down functions using regex const migrateMatch = content.match(/migrate\(\s*\(\s*app\s*\)\s*=>\s*\{([\s\S]*?)}\s*,\s*\(\s*app\s*\)\s*=>\s*\{([\s\S]*?)}\s*\)/); if (!migrateMatch || migrateMatch.length < 3) { throw new Error(`Invalid migration format in file: ${migrationPath}`); } const upBody = migrateMatch[1].trim(); const downBody = migrateMatch[2].trim(); // Create the up and down functions const up = new Function('app', `${upBody}`); const down = new Function('app', `${downBody}`); return { up: async (app: any) => await up(app), down: async (app: any) => await down(app) }; } catch (error: any) { console.error(`Error loading migration file ${migrationPath}:`, error); throw new Error(`Failed to load migration file: ${error.message}`); } }