Skip to main content
Glama
mabeldata

PocketBase MCP Server

by mabeldata

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
NameRequiredDescriptionDefault
migrationFileYesName of the migration file to revert.

Implementation Reference

  • 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}`);
        }
    }
  • 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'],
        },
    },
  • TypeScript interface defining the input arguments for the revert_migration handler.
    interface RevertMigrationArgs {
        migrationFile: string;
    }
  • 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}`);
        }
    }
  • 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}`);
        }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mabeldata/pocketbase-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server