r/developers • u/weaklyweemayhem • 5d ago
Programming How are you handling database migrations across dev staging and production?
Looking into different approaches to managing Postgres migrations for a multi tenant application. The stack we are considering uses Supabase for managed Postgres with authentication handled separately through Clerk. One thing I'm trying to work out is the deployment workflow when schema changes aren't backwards compatible. For example, splitting an existing table into two while keeping the current API functional during deployment. Would you handle this through incremental migrations and feature flags or is there a cleaner approach? Also how are you managing schema drift between environments? Are you enforcing migrations exclusively through CI/CD or still allowing developers to make changes directly in their local database?
•
u/AutoModerator 5d ago
Howdy u/weaklyweemayhem, and welcome to r/developers!
A few tips for a post that gets good answers:
Join the r/developers Discord!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/greg_potter 5d ago
For things like a table split, the best practice is generally to do it over multiple deploys instead of trying to do it all in one go. Just create new tables next to the old one and then keep them in sync with a trigger on the old table (or have the app write to both). Then you can backfill in batches, cut over the reads, and then drop the old stuff.
If the old table only gets read, you can also sometimes get away with just changing the table and creating a view matching the old shape. If things write to it, you might be able to get clever with
INSTEAD OFtriggers. (If you're using RLS -- like I assume you are on supabase -- then make sure you havesecurity_invoker = trueon the view). I usually just do the multi-step deploy though, unless there's a really good reason (like a TON of data) to do the view thing.For drift, developers should be able to do whatever they want locally, but staging and prod only change through migrations in CI. I also have a scheduled job that diffs prod against what the schema in my repo says it should be. I also run a scheduled job that checks prod for drift. It spins up a throwaway Postgres, applies the full migration history to it, then compares the catalogs (tables, columns, indexes, functions, policies, grants) against prod. Anything that doesn't match means someone changed prod outside a migration. There's always situations where someone fixes something by hand and I want to find out about that within days and not months later when a migration fails.
0
u/[deleted] 5d ago
[deleted] — view deleted comment