Why I don't use down migrations
Freek Van der Herten explains why his team at Spatie avoids using down migrations in their projects. Although down migrations can appear to be a helpful safety net, they often provide a false sense of security and can introduce more issues than they resolve.
Down Migrations are Rarely Tested:
- Down migrations are typically the least tested parts of Laravel applications. While other aspects such as features and business logic undergo regular testing, down methods are often neglected. This results in untested rollback scenarios that are challenging to verify.
Challenges with New Data:
- After deployment, user interactions create new data. Rolling back migrations can lead to this data becoming inaccessible or lost entirely. Complex data transformations pose additional difficulties, as reversing them is not straightforward, especially when original data formats are no longer available.
Maintaining Code and Database Harmony:
- Applications depend on specific database structures. Changes in the schema without corresponding code updates can cause disruptions. Such issues are exacerbated by modern deployment strategies, like container orchestration and blue-green deployments, where varying code versions might be running simultaneously.
Adopting Forward-Only Migrations:
- At Spatie, a forward-only migration strategy is used. When reversals are necessary, they are approached with careful consideration, crafting new migrations to address specific situations rather than attempting to roll back changes.
Conclusion:
- Before writing a down method, it’s important to evaluate its necessity and effectiveness. Prioritizing the robustness of up migrations and new code can be a more efficient use of development time.