Search code examples
pythondatabasewebdatabase-migrationweb-frameworks

Schema migrations in Production database?


I have started learning web development(Flask and Django to be specific), and anywhere I see, the topic of databases always starts with migrations.

From what I've understood for updating databases one should

  1. Run a "something to generate migrate script" to generate a migration script which would diff your current models file and the current database.
  2. Test the migration script on your local database.
  3. Commit the migration script so that it reaches your production environment, wherein run the script again to update your production databases.

But then reading Schema Migrations on Wikipedia from this link Schema Migration I came upon the following text:

schema migration is typically only used when the data held in the database is not real nor valuable, such as in software development, where developers work only with (possibly generated) test data.[citation needed] Programmatic schema migrations are almost never performed in production for the same reason.

It says that one should avoid migrations in prodution, then how are you supposed to update your databases?


Solution

  • I don't see where the author of the Wikipedia article got that idea from; according to the history, it was already in the first revision and for me, this arbitrary limitation doesn't make sense.

    Migrating databases alongside with program versions is something that is often necessary and that means it's necessary for production databases. I don't make a distinction here between the data in the rows and the schema since that's a bit arbitrary. From a code point of view, the database is a way to encode data and the encoding scheme directly influences both the schema and the encoding of the data in the rows.

    Maybe it would make sense to talk about the risks of such migrations (production databases sometimes contain surprises like corrupt data, data that has been modified by running an SQL query manually) or the complexity (like adding and deleting foreign keys). But I've seen many products which migrate data and schema when a new version is released.

    Update: I've updated the Wikipedia page, let's see how long the edit lasts :-)