Skip to main content
Glama
mabeldata

PocketBase MCP Server

by mabeldata

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

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

Implementation Reference

  • 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'],
        },
    },
  • TypeScript interface defining the arguments for revert_to_migration: targetMigration (string) and appliedMigrations (optional string array).
    interface RevertToMigrationArgs {
        targetMigration: string;
        appliedMigrations?: string[];
    }
  • 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}`);
        }
    }
  • 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}`);
        }
    }
  • 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}`);
        }
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description states it reverts migrations but omits critical details: the target is exclusive (per schema), no mention of destructive nature, safety, or prerequisites. With no annotations, the description fails to fully disclose behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence with no waste. However, its brevity sacrifices clarity on details like exclusivity, earning a 4 rather than 5.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations or output schema, the description should provide more context. It omits the exclusive semantics of the target, how to interpret the appliedMigrations parameter, and the tool's relationship to sibling tools.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Input schema coverage is 100%, so baseline is 3. The description adds minimal context ('up to a specific target') but does not explain how parameters like 'appliedMigrations' are used or the exclusive nature of 'targetMigration'.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses a verb+resource structure ('Revert migrations') and mentions 'up to a specific target', which clarifies scope. However, it does not differentiate from the sibling 'revert_migration' tool, which could cause confusion.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives. The description does not explain that it reverts multiple migrations to a target, unlike 'revert_migration' which reverts a single one.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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