Function Signature
Description
Generates a new pair of migration files (.up.sql and .down.sql) with timestamp-based naming. The files are created in the specified migration folder with boilerplate comments to guide writing the migration.
Parameters
Path to the directory where migration files should be created. The directory will be created if it doesn’t exist.Example:
"./migrations" or "./db/migrations"Descriptive name for the migration. This will be converted to lowercase with spaces replaced by underscores.Examples:
"create_users_table", "add_email_to_users", "Create Users Table"Return Value
ReturnsResult<()> which:
- Returns
Ok(())if both migration files were created successfully - Returns
Errif there was a file system error or permission issue
Usage Example
Multiple Migrations Example
With Name Normalization
Generated Files
The function creates two files with timestamp-based names:File Naming Convention
- Format:
{timestamp}_{normalized_name}.{direction}.sql - Timestamp: Unix timestamp (seconds since epoch)
- Normalized name: Lowercase with underscores replacing spaces
- Direction: Either
upordown
File Contents
.up.sql file:
.down.sql file:
Writing Migrations
After generating the files, you should edit them to add your SQL:Example: Creating a Table
1234567890_create_users_table.up.sql:
1234567890_create_users_table.down.sql:
Example: Adding a Column
1234567891_add_email_verified_to_users.up.sql:
1234567891_add_email_verified_to_users.down.sql:
Transaction Control
By default, migrations run in a transaction. To disable transactions for a specific migration, add a comment at the top of the file:- Creating indexes concurrently (PostgreSQL)
- Some DDL operations in MySQL
- Database-specific operations that require autocommit mode
Behavior
- Validates the migration path
- Creates the migration directory if it doesn’t exist
- Generates a timestamp (current Unix timestamp)
- Normalizes the migration name (lowercase, replace spaces with underscores)
- Creates both
.up.sqland.down.sqlfiles - Writes boilerplate comments to each file
- Logs the file paths of the created migrations
Error Handling
The function may return errors in these cases:- Insufficient permissions to create the migration directory
- Insufficient permissions to write files
- Disk space issues
- Invalid characters in the migration path
- File system errors
Complete Workflow Example
Best Practices
-
Use descriptive names: Choose migration names that clearly describe what the migration does
- Good:
"create_users_table","add_index_to_email","remove_deprecated_columns" - Bad:
"migration1","fix","update"
- Good:
-
One logical change per migration: Each migration should represent a single logical change
- Create separate migrations for creating different tables
- Don’t mix table creation with index creation unless they’re closely related
-
Write reversible migrations: Ensure your
.down.sqlproperly reverses the.up.sql- Test rollbacks in development
- Be aware of data loss in destructive rollbacks
- Test before committing: Test both up and down migrations before committing to version control
See Also
- migrate_database - Apply the generated migrations
- migate_down - Test rollback migrations
- status_migrations - Check migration status