generate-migration Command
Note: This command is implemented as part of the
@pinstripe/databasepackage and needs to be included in your project for it to be available.
Interface
The command creates a new database migration file with the following signature:
pinstripe generate-migration [suffix] [--table <table_name>] [--fields <field_definitions>]
Parameters
suffix(optional) - The suffix for the migration name (converted to snake_case). If ending with "_to_tablename", the table name will be inferred--table <table_name>(optional) - The table name for the migration. Can be inferred from suffix--fields <field_definitions>(optional) - Field definitions in format "name:type name:type" (e.g., "name:string age:integer")
Examples
# Create a basic migration
pinstripe generate-migration add_users_table
# Create a migration with table name inferred from suffix
pinstripe generate-migration add_columns_to_users
# Create a migration with explicit table and fields
pinstripe generate-migration add_user_fields --table users --fields "email:string created_at:datetime"
# Create a migration with multiple fields
pinstripe generate-migration setup_posts --table posts --fields "title:string content:text published:boolean"
Description
The generate-migration command is a database scaffolding tool that creates timestamped migration files for database schema changes. It automatically:
- Normalizes naming - Converts the suffix to snake_case format
- Generates timestamped files - Creates uniquely named migration files with Unix timestamp prefix
- Creates migration directory - Sets up
lib/migrations/directory structure - Sets up file importer - Creates
_file_importer.jsif it doesn't exist - Provides starter code - Includes migration structure with field definitions when specified
Generated Files
lib/migrations/
├── _file_importer.js # Migration base class import (created if needed)
└── 1728471234_suffix_name.js # Your timestamped migration file
Generated Migration Structure
Basic Migration
export default {
async migrate(){
}
};
Migration with Table and Fields
export default {
async migrate(){
await this.database.table('users', async users => {
await users.addColumn('email', 'string');
await users.addColumn('created_at', 'datetime');
});
}
};
Usage in Application
After generating a migration, you can:
- Customize the implementation by editing the
migrate()method - Run migrations via the database migration system
- Add complex schema changes like indexes, foreign keys, and data transformations
Field Types
Supported field types include:
Text Types
string- Variable length text (VARCHAR 255)text- Long text content (LONGTEXT/TEXT)
Numeric Types
integer- Whole numbers (INT)decimal- Decimal numbers with precisionfloat- Floating point numbers
Date/Time Types
date- Date only (YYYY-MM-DD)datetime- Date and time stamps
Other Types
boolean- True/false valuesbinary- Binary data (files, images, etc.)
Key Types (Auto-managed)
primary_key- Auto-incrementing primary keyalternate_key- UUID-based alternate keyforeign_key- References to other tables
Related Commands
list-commands- List all available commands in the projectgenerate-service- Generate business logic services