Database migration with data already in the database in production

migrationmongodbpostgresproduction

I don't know much about migrations, but this is a specific question I'm not sure how to answer.

In production, how can one change the database format/schema when there is already user data in there? (Is the answer different for our Meteor app on MongoDB vs our master Postgres database?) Basically, if you write new code that introduces a new field, and new code that expects that new field to be present, then the system will not work when pulling data from the database that was there prior to the changeover.

For Mongo at least, a possible solution is to write a script to update the database layout with the data in it, but I wonder if this is too error prone for a production database, and infeasible when you just don't have the necessary information (like if you didn't collect a certain info field from the user before.)

Best Answer

if you write new code that introduces a new field, and new code that expects that new field to be present, then the system will not work when pulling data from the database that was there prior to the changeover.

You have to take that into account when you are writing your code. You could run a script when you deploy the new changes to populate this field with a default value. You could write your code so that it is able to handle the situation where the field is no present/populated.

Exactly what you do will probably depend on the details of your situation, but you're correct: you have to do something to handle this.

...And it goes without saying that you should test this migration process thoroughly (on a copy of production, if possible) before running it in a production system. ;)

Related Topic