Command Line Tools
dbmigrate down
dbmigrate down
Section titled “dbmigrate down”Rollback the last executed database migration.
Synopsis
Section titled “Synopsis”wheels dbmigrate downAlias: wheels db down
Description
Section titled “Description”The dbmigrate down command reverses the last executed migration by running its down() method. This is useful for undoing database changes when issues are discovered or when you need to modify a migration. The command ensures safe rollback of schema changes while maintaining database integrity.
Parameters
Section titled “Parameters”None.
Examples
Section titled “Examples”Rollback the last migration
Section titled “Rollback the last migration”wheels dbmigrate downThis will execute the down() method of the most recently applied migration, reverting the database changes.
Use Cases
Section titled “Use Cases”Fixing Migration Errors
Section titled “Fixing Migration Errors”When a migration contains errors or needs modification:
# Run the migrationwheels dbmigrate up
# Discover an issue# Rollback the migrationwheels dbmigrate down
# Edit the migration file# Re-run the migrationwheels dbmigrate upDevelopment Iteration
Section titled “Development Iteration”During development when refining migrations:
# Apply migrationwheels dbmigrate up
# Test the changes# Need to modify? Rollbackwheels dbmigrate down
# Make changes to migration# Apply againwheels dbmigrate upEmergency Rollback
Section titled “Emergency Rollback”When a migration causes issues:
# Check current migration statuswheels dbmigrate info
# Rollback the problematic migrationwheels dbmigrate down
# Verify rollbackwheels dbmigrate infoImportant Considerations
Section titled “Important Considerations”Data Loss Warning
Section titled “Data Loss Warning”Rolling back migrations that drop columns or tables will result in data loss. Always ensure you have backups before rolling back destructive migrations.
Down Method Requirements
Section titled “Down Method Requirements”For a migration to be rolled back, it must have a properly implemented down() method that reverses the changes made in the up() method.
Migration Dependencies
Section titled “Migration Dependencies”Be cautious when rolling back migrations that other migrations depend on. This can break the migration chain.
Best Practices
Section titled “Best Practices”- Always implement down() methods: Even if you think you’ll never need to rollback
- Test rollbacks: In development, always test that your down() method works correctly
- Backup before rollback: Especially in production environments
- Document destructive operations: Clearly indicate when rollbacks will cause data loss
- Only the last executed migration can be rolled back with this command
- To rollback multiple migrations, run the command multiple times
- If already at version 0, displays: “We’re already on zero! No migrations to go to”
- Automatically runs
dbmigrate infoafter successful rollback - The migration version is removed from the database tracking table upon successful rollback
- Some operations (like dropping columns with data) cannot be fully reversed
- When migrating to version 0, displays: “Database should now be empty.”
Related Commands
Section titled “Related Commands”wheels dbmigrate up- Run the next migrationwheels dbmigrate reset- Reset all migrationswheels dbmigrate info- View migration statuswheels dbmigrate exec- Run a specific migration