Django Migrations: Pitfalls and Solutions with Benjamin Zagorsky - DjangoCon US 2022

Django migrations can be challenging, but with Benjamin Zagorsky's expert guidance, learn how to avoid common pitfalls and solutions to successfully deploy database changes without downtime.

Key takeaways

Five key takeaways:

  • Back up your database regularly, as database backups allow for time travel and can salvage data in case of issues.
  • Migrations should always include both a forwards and backwards function to ensure reversibility.
  • Backwards compatible migrations are crucial for successfully deploying changes to a database, without downtime.
  • run Python is a powerful tool in Django that can be used to refactor and optimize migrations.
  • Avoid setting null fields and instead start with nullable fields and then populate them later, as this makes reversible migrations easier to implement.

Top five recommendations:

  • Backwards compatibility is crucial for successful deployments.
  • Have a scheduled downtime for maintenance.
  • Set up a migration recorder for quick debugging.
  • Use run Python for refactoring and optimization of migrations.
  • Regularly back up your databases.

Additional points:

  • Scheduled downtime is essential for maintenance and successful deployments.
  • Migrations should be ran in a specific order to avoid conflicts with other migrations.
  • Django doesn’t guarantee that migrations work, but some of them do work.
  • run Python offers a way to rewind migrations and can fix issues with migrations.
  • Code changes should be made after database updates, not before.
  • Back up your database before making major updates.
  • run Python doesn’t guarantee success, but failure is often informative.
  • Django’s make migrations-merge command can help resolve conflicts between separate branches.
  • run Python functions can be used to reconstruct columns based on backups.
  • Django 1.7 to 1.8 and 1.8 to 1.9 are backwards compatible upgrades.
  • Django’s run.py script can be used to manage migrations recursively.
  • Django’s migrate recorder can be used to debug migrations without affecting the database.
  • Django can be used for full CI deployments to staging.