7

I'm trying to introduce GitOps in our company. Mostly we have microservices written in Django (However, it could be any other web framework). I have a conceptual problem with the database migrations. Forward migrations are easy: you run them as a job or an init container or hooks in helm. However what about backward migrations? Let's say I want to make rollback from v1.1 to v1.0 and I have some migrations to unapply. And now what? I wanted to use argo cd or flux.

There is a really nice article: https://www.weave.works/blog/how-to-correctly-handle-db-schemas-during-kubernetes-rollouts. However, it requires some discipline which you don't have in Django

sacherus
  • 1,614
  • 2
  • 20
  • 27
  • 3
    I'm facing the same problem as you were, and my solution for now is simply not having automated rollback of migrations. The application developers will have to be careful and not introduce breaking changes between the DB schema and the application. This way, if we have to rollback the application to the last version, it will still be compatible with the new DB schema, and the DB doesn't have to be rolled back. This also solves the problem of having the two versions of the app running at the same time during a deploy. Did you do anything different from this to solve your problem? – luislhl May 30 '20 at 18:09
  • @sacherus Did you solve such problem? I'm very interesting in your approach! – Mikhail_Sam Jul 21 '22 at 09:23

1 Answers1

3

One strategy you can apply is that any database migration should be backward compatible(especially the DDLs) so that when you rollback to older version db state is still compatible with older version. This should be made a design/architecture guideline.

Ex:

  1. Never drop a table as part of migration
  2. Never drop a column
  3. Never change type of a column etc
Kapil Raju
  • 685
  • 1
  • 8
  • 12
  • Fully agree that this is the best approach when dealing with databases migrations. With that said, there are some players with some really good solutions for this problem, like PlanetScale: https://planetscale.com/features/revert – Guilherme Pellizzetti Nov 11 '22 at 19:23