Home Solutions Industries Portfolio Pricing Blog Affiliate Contact
Quote
← Back to all posts
Safe Database Migrations: Changing Your Schema Without Breaking Production
DevOps & Infrastructure

Safe Database Migrations: Changing Your Schema Without Breaking Production

BL
BlackCode Admin November 3, 2026 · 2 min read

Adding a column on a small table is instant. Adding one with a default on a large table, changing a column type or creating an index can lock the table for minutes, and every request touching it waits or fails.

Know which changes are risky

ChangeRiskSafer approach
Add nullable columnLowSafe in most databases
Add column with default on huge tableCan lock (older versions)Add nullable, backfill, then set default
Create indexLocks writesCreate concurrently
Rename columnBreaks running codeExpand-contract over several releases
Change column typeRewrites tableNew column, backfill, switch
Drop columnBreaks old codeStop using first, drop later

The expand-contract pattern

  1. Expand: add the new column or table.
  2. Deploy code that writes to old and new.
  3. Backfill existing rows in small batches.
  4. Switch reads to the new structure.
  5. Contract: stop writing to the old one, then remove it in a later release.

Tired of risky deployments and surprise downtime?

We set up CI/CD, monitoring and infrastructure that lets your team ship calmly.

WhatsApp us+254 731 209 601Book a free demo20 minutes, no costCall us+254 731 209 601

Backfills

Update large tables in batches of a few thousand rows with short pauses, outside the main deployment. This keeps locks short and load manageable, and lets you stop and resume safely.

Practical safeguards

  • Review migrations in code review like any other change.
  • Test on a production-sized copy of the data.
  • Set lock and statement timeouts so a migration fails fast instead of blocking everything.
  • Back up before major changes.
  • Keep migrations small and focused.

Key takeaways

  • Know which schema changes lock tables.
  • Use expand-contract for renames and type changes.
  • Backfill in batches.
  • Test on realistic data and set lock timeouts.

Frequently asked questions

Does Django handle this automatically?

Django generates migrations, but you must still review them for locking and plan multi-step changes.

Can migrations be rolled back?

Some can; data-destroying ones cannot. Expand-contract keeps rollback possible.

Can you review our migrations?

Yes. We review schema changes and plan safe rollouts.

Tired of risky deployments and surprise downtime?

We set up CI/CD, monitoring and infrastructure that lets your team ship calmly.

WhatsApp us+254 731 209 601Book a free demo20 minutes, no costCall us+254 731 209 601

0 comments 0 likes

Comments (0)

Be the first to comment on this post.