add_field_migration
Generate a migration file to add a new field to an existing collection in PocketBase. Specify the collection name or ID and define the field schema for automated integration.
Instructions
Create a migration file for adding a field to an existing collection.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionNameOrId | Yes | The name or ID of the collection to update. | |
| description | No | Optional description override for the filename. | |
| fieldDefinition | Yes | The schema definition for the new field. |
Implementation Reference
- src/tools/migration-tools.ts:234-249 (handler)The handler function that executes the 'add_field_migration' tool. Validates input arguments and invokes createAddFieldMigration to generate the migration file.async function handleAddFieldMigration(args: AddFieldMigrationArgs): Promise<ToolResult> { if (!args.collectionNameOrId) { throw invalidParamsError("Missing required argument: collectionNameOrId"); } if (!args.fieldDefinition) { throw invalidParamsError("Missing required argument: fieldDefinition"); } if (!args.fieldDefinition.name || !args.fieldDefinition.type) { throw invalidParamsError("fieldDefinition must include 'name' and 'type' properties."); } const filePath = await createAddFieldMigration(args.collectionNameOrId, args.fieldDefinition, args.description); return { content: [{ type: 'text', text: `Created field migration file: ${filePath}` }], }; }
- src/tools/migration-tools.ts:98-111 (schema)The input schema definition for the 'add_field_migration' tool, specifying parameters and validation rules.inputSchema: { type: 'object', properties: { collectionNameOrId: { type: 'string', description: 'The name or ID of the collection to update.' }, fieldDefinition: { type: 'object', description: 'The schema definition for the new field.', additionalProperties: true, required: ['name', 'type'] }, description: { type: 'string', description: 'Optional description override for the filename.' }, }, required: ['collectionNameOrId', 'fieldDefinition'], },
- src/tools/migration-tools.ts:95-112 (registration)The tool registration entry in the migrationToolInfo array, defining name, description, and input schema.{ name: 'add_field_migration', description: 'Create a migration file for adding a field to an existing collection.', inputSchema: { type: 'object', properties: { collectionNameOrId: { type: 'string', description: 'The name or ID of the collection to update.' }, fieldDefinition: { type: 'object', description: 'The schema definition for the new field.', additionalProperties: true, required: ['name', 'type'] }, description: { type: 'string', description: 'Optional description override for the filename.' }, }, required: ['collectionNameOrId', 'fieldDefinition'], }, },
- src/tools/index.ts:51-52 (registration)Routing logic in the central handleToolCall that directs 'add_field_migration' calls to handleMigrationToolCall.} else if (name === 'create_migration' || name === 'create_collection_migration' || name === 'add_field_migration' || name === 'list_migrations') { return handleMigrationToolCall(name, toolArgs, pb);
- src/migrations/index.ts:86-113 (helper)Helper function that generates the actual migration file content and saves it for adding a field to a collection.export async function createAddFieldMigration( collectionNameOrId: string, fieldDefinition: Record<string, any>, description?: string ): Promise<string> { const timestamp = helpers.generateTimestamp(); if (!fieldDefinition.name || !fieldDefinition.type) { throw new Error("Field definition must include 'name' and 'type' properties."); } const fieldName = fieldDefinition.name; const desc = description || `update_${collectionNameOrId}_add_${fieldName}`; const sanitizedDescription = desc .toLowerCase() .replace(/[^a-z0-9_]+/g, '_') .replace(/^_+|_+$/g, ''); const filename = `${timestamp}_${sanitizedDescription}.js`; // Generate specific up/down queries const upQuery = helpers.generateAddFieldQuery(collectionNameOrId, fieldDefinition); const downQuery = helpers.generateRemoveFieldQuery(collectionNameOrId, fieldName); const content = helpers.generateMigrationTemplate(upQuery, downQuery); return helpers.createMigrationFile(filename, content); }