r/SQL • u/Gabicolombo_27 • Oct 24 '26
MariaDB How do you handle many-to-many database migrations without breaking existing API consumers?
/r/Backend/comments/1xen9ac/how_do_you_handle_manytomany_database_migrations/
8
Upvotes
3
u/reditandfirgetit Oct 25 '26
Your API consummers should be going through an API you've exposed and its up to you to ensure the contract is thevsame to the caller
By "you" i mean whoever in the company maintains the API
2
u/RomirBuilds Oct 25 '26
Before choosing views or dual writes, I'd pin down what the old API means once the first record has two associations.
For example, if v1 exposes one parent_id and v2 allows several parents, a view can preserve the old column shape, but it can't decide which association v1 should see. You need an explicit compatibility rule, such as a designated primary relationship, if that fits the domain. Legacy updates then need defined behavior too: does changing parent_id replace only the primary link, or the entire set?
A useful contract test is: a new client adds associations A and B; an old client reads the record, changes its single association to C, then a new client reads it again. Assert exactly which links should remain. That catches accidental deletion of links the old client never knew about.
If there isn't a meaningful single-association interpretation, I'd delay enabling multiple associations for shared records until the affected consumers migrate. Versioning the endpoint alone doesn't resolve writes to the same underlying data.
How did you define that legacy-write behavior in your migration?
AI-assisted.
3
u/Kazcandra Oct 25 '26
Views.
1
u/Significant_Tune9219 Oct 25 '26
I would not dual-write the old foreign key and the junction table for long. Two writers means a failure between them leaves a parent whose child list and whose old column disagree, and every reader has to know which one wins. Add the junction, backfill it from the old column, teach every consumer to read the junction, and only then drop the column. Versioning the API does not help a consumer you do not ship, so the old read has to keep working until those callers are gone. The junction's unique key should include the old pair, or the backfill can insert the same relationship twice while the old clients still see one.
1
u/WolfBanner 29d ago
As with any migration, it is important to prepare ahead of time. Gather a some sage, a dove, a young priest and an old priest. Brush up on your Latin, but don’t skip the phonecian or the Sumerian (Oracle will also require Ancient Greek which is not the same as modern Greek so don’t get caught off guard).
As always, never attempt this alone, make sure you and your team have purified yourselves in the waters of the Monster, and have packed 5hr energy bottles in your lunch box. And if things go sideways….well good luck