๐——๐—ฎ๐˜๐—ฎ๐—ฏ๐—ฎ๐˜€๐—ฒ ๐— ๐—ถ๐—ด๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฆ๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐—ถ๐—ฒ๐˜€

Bad migrations kill apps. You risk data loss and downtime. Follow a system to stay safe.

Write idempotent migrations. The result stays the same if you run it ten times. Use IF NOT EXISTS. This makes retries safe.

Test on a copy of production data. Dev data is too clean. Production clones show real edge cases.

Keep changes backward compatible. Never delete a column in one go. Add the new one first. Update your code to write to both. Delete the old one later. This stops downtime.

Use the expand-migrate-contract pattern for big changes.

Avoid locking tables. Adding columns with defaults locks PostgreSQL. Add the column without a default. Update data in batches. Set the default last.

Run migrations outside app startup. Use a dedicated runner. This keeps your app available.

Plan for failure. Write a tested rollback script for every change. Document the time and lock duration.

Build for today. Do not build for scale you do not have. Start with a simple core path. Add observability early. Use boring tech your team knows.

Next steps:

Source: https://dev.to/therizwansaleem/database-migration-strategies-zero-downtime-rollback-and-testing-3pdj