2020-10-30 14:08:56 -04:00
---
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2016-10-27 08:36:53 -04:00
# Post Deployment Migrations
Post deployment migrations are regular Rails migrations that can optionally be
executed after a deployment. By default these migrations are executed alongside
2020-12-07 10:09:49 -05:00
the other migrations. To skip these migrations you must set the
2016-10-27 08:36:53 -04:00
environment variable `SKIP_POST_DEPLOYMENT_MIGRATIONS` to a non-empty value
when running `rake db:migrate` .
For example, this would run all migrations including any post deployment
migrations:
2020-01-30 10:09:15 -05:00
```shell
2016-10-27 08:36:53 -04:00
bundle exec rake db:migrate
```
2020-12-07 10:09:49 -05:00
This however skips post deployment migrations:
2016-10-27 08:36:53 -04:00
2020-01-30 10:09:15 -05:00
```shell
2016-10-27 08:36:53 -04:00
SKIP_POST_DEPLOYMENT_MIGRATIONS=true bundle exec rake db:migrate
```
## Deployment Integration
Say you're using Chef for deploying new versions of GitLab and you'd like to run
post deployment migrations after deploying a new version. Let's assume you
normally use the command `chef-client` to do so. To make use of this feature
you'd have to run this command as follows:
2020-01-30 10:09:15 -05:00
```shell
2016-10-27 08:36:53 -04:00
SKIP_POST_DEPLOYMENT_MIGRATIONS=true sudo chef-client
```
Once all servers have been updated you can run `chef-client` again on a single
server _without_ the environment variable.
The process is similar for other deployment techniques: first you would deploy
2020-12-07 10:09:49 -05:00
with the environment variable set, then you re-deploy a single
2016-10-27 08:36:53 -04:00
server but with the variable _unset_ .
## Creating Migrations
To create a post deployment migration you can use the following Rails generator:
2020-01-30 10:09:15 -05:00
```shell
2016-10-27 08:36:53 -04:00
bundle exec rails g post_deployment_migration migration_name_here
```
2020-12-07 10:09:49 -05:00
This generates the migration file in `db/post_migrate` . These migrations
2016-10-27 08:36:53 -04:00
behave exactly like regular Rails migrations.
## Use Cases
Post deployment migrations can be used to perform migrations that mutate state
that an existing version of GitLab depends on. For example, say you want to
remove a column from a table. This requires downtime as a GitLab instance
depends on this column being present while it's running. Normally you'd follow
these steps in such a case:
1. Stop the GitLab instance
2018-11-12 19:39:21 -05:00
1. Run the migration removing the column
1. Start the GitLab instance again
2016-10-27 08:36:53 -04:00
Using post deployment migrations we can instead follow these steps:
1. Deploy a new version of GitLab while ignoring post deployment migrations
2018-11-12 19:39:21 -05:00
1. Re-run `rake db:migrate` but without the environment variable set
2016-10-27 08:36:53 -04:00
Here we don't need any downtime as the migration takes place _after_ a new
version (which doesn't depend on the column anymore) has been deployed.
Some other examples where these migrations are useful:
2018-11-13 01:07:16 -05:00
- Cleaning up data generated due to a bug in GitLab
- Removing tables
- Migrating jobs from one Sidekiq queue to another