golang-migrate / migrate

Database migrations. CLI and Golang library.
Other
15.64k stars 1.42k forks source link

Best practises how to use it on production #673

Open nawa opened 2 years ago

nawa commented 2 years ago

Hi

Could someone share an article/guidance how to deliver and run migration on production environment?

Thanks in advance

dhui commented 2 years ago

There's no one size fits all as a lot of this depends on what your existing setup is, what your team's expertise is, and how this would integrate with the rest of your tech stack.

Could someone share an article/guidance how to deliver and run migration on production environment?

* how to package and deliver artifacts with migration scripts?

  * should scripts be embedded into the main application using database?
  * is it ok to distribute them as archive or cloning repository?

Various methods are supported via migration sources

* which component should run migrations?

  * should it be the main application itself before its execution?
  * should it be a separate "runner" controlled by a deployment manager such as kubernetes, docker-compose, cloudformation etc

This is up to you, but in general, migrations should run before before your application starts so the db schema is ready when your application starts. I find it easier to run migrations in the application but then you need to make sure your DB driver supports distributed locking if you have a distributed application. It may be easier to hand this off to an orchestrator if your tech stack already uses one (or plans to in the future).

See also: https://github.com/golang-migrate/migrate/issues/668