Mongodb, Making Breaking changes to production database
Hi, I'm trying to understand what the suggested way or best practices for making breaking changes to a database in production are.
for simplicity sake, let's say I have a schema for mongodb:
where there is already data present.
let's say now I want to be able to have multiple addresses so I want the schema to be put like this:
To add the new address field is okay but the moving of existing data from the 4 separate fields to now the new address object is what I am having a hard time understanding how to do.
I'd like to understand how people are doing these types of database changes for mongodb with prisma or just in general I suppose.
thanks!
3 Replies
Hi @ImEgg 👋
One way to approach this would be to add the new address field to your schema:
Then, update your application to start writing to both the new
address
field and the old fields when creating or updating users. You can create a migration script to move the existing address
data into the new address
array for all users. This script would:
1. Query all users
2. For each user, create a new Address object with the data from the existing fields
3. Add this new Address object to the user's address array
4. Save the updated user
After running the migration script and ensuring all data has been moved, you can update your application to exclusively use the new address field. Finally, you can create another migration to remove the old address fields from your schema.See this documentation https://www.prisma.io/docs/orm/overview/databases/mongodb#how-to-migrate-existing-data-to-match-your-prisma-schema
MongoDB database connector | Prisma Documentation
How Prisma can connect to a MongoDB database using the MongoDB database connector.
I see. According to documentation, the strategy I am referring to would be "all-at-once" updates.
From what it sounds like, the best way to go about this is to have the script run in your CI/CD pipeline. if it fails, the push fails and that for every breaking change, there should be 2 separate migrations. the first one adds and moves data to new field. once we do checks and balances and are okay with results we make the second migration to remove old field / data if need be. Please correct me if I'm wrong here.
2nd question. For Prisma with SQL, there is a migration folder and migrations tracker table in the db itself. Is the recommended idea to try and mimic this solution for mongodb if the idea is to do "all-at-once" updates similar to it.
Thank you for your help. I appreciate it!