Skip to main content
Glama
mabeldata

PocketBase MCP Server

by mabeldata

revert_to_migration

Reverts database schema migrations up to a specified target using applied migration filenames, allowing precise control over schema rollbacks in PocketBase MCP Server.

Instructions

Revert migrations up to a specific target.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
appliedMigrationsNoArray of already applied migration filenames.
targetMigrationYesName of the migration to revert to (exclusive). Use empty string to revert all.

Implementation Reference

  • Primary handler function for the 'revert_to_migration' MCP tool. Validates input arguments and calls the revertToMigration utility, formatting the response.
    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}`);
        }
    }
  • JSON schema definition for the 'revert_to_migration' tool inputs, used for MCP validation.
    {
        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'],
        },
    },
  • Main tools registration where migration tools (including revert_to_migration) are included via listMigrationTools().
    export function registerTools(): { tools: ToolInfo[] } { // Use ToolInfo[]
        const tools: ToolInfo[] = [ // Use ToolInfo[]
            ...listRecordTools(),
            ...listCollectionTools(),
            ...listFileTools(),
            ...listMigrationTools(), // Uncommented
            ...listLogTools(), // Add log tools
            ...listCronTools(), // Add cron tools
        ];
        return { tools };
    }
  • Routing logic in main handleToolCall that directs 'revert_to_migration' calls to the migration tools handler.
    } else if (name === 'create_migration' || name === 'create_collection_migration' || name === 'add_field_migration' || name === 'list_migrations') {
        return handleMigrationToolCall(name, toolArgs, pb);
  • Core utility function implementing the logic to revert multiple migrations up to a target migration by sorting and calling revertMigration on each.
    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}`);
        }
    }

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