2015-05-11 09:09:36 -04:00
# Migration Style Guide
When writing migrations for GitLab, you have to take into account that
2015-05-11 10:48:39 -04:00
these will be ran by hundreds of thousands of organizations of all sizes, some with
2015-05-11 09:09:36 -04:00
many years of data in their database.
In addition, having to take a server offline for a an upgrade small or big is
a big burden for most organizations. For this reason it is important that your
2015-05-11 10:48:39 -04:00
migrations are written carefully, can be applied online and adhere to the style guide below.
2015-05-11 09:09:36 -04:00
2016-05-09 09:05:19 -04:00
Migrations should not require GitLab installations to be taken offline unless
2016-10-02 07:32:30 -04:00
_absolutely_ necessary - see the ["What Requires Downtime?" ](what_requires_downtime.md )
page. If a migration requires downtime, this should be clearly mentioned during
the review process, as well as being documented in the monthly release post. For
more information, see the "Downtime Tagging" section below.
2015-11-02 10:14:34 -05:00
2015-05-11 09:09:36 -04:00
When writing your migrations, also consider that databases might have stale data
or inconsistencies and guard for that. Try to make as little assumptions as possible
about the state of the database.
2015-05-11 10:48:39 -04:00
Please don't depend on GitLab specific code since it can change in future versions.
2016-02-12 08:32:35 -05:00
If needed copy-paste GitLab code into the migration to make it forward compatible.
2015-05-11 10:48:39 -04:00
2016-06-24 12:29:23 -04:00
## Downtime Tagging
2015-05-11 09:09:36 -04:00
2016-06-24 12:29:23 -04:00
Every migration must specify if it requires downtime or not, and if it should
require downtime it must also specify a reason for this. To do so, add the
following two constants to the migration class' body:
2015-05-11 09:09:36 -04:00
2016-06-24 12:29:23 -04:00
* `DOWNTIME` : a boolean that when set to `true` indicates the migration requires
downtime.
* `DOWNTIME_REASON` : a String containing the reason for the migration requiring
downtime. This constant **must** be set when `DOWNTIME` is set to `true` .
2015-05-11 09:09:36 -04:00
2016-06-24 12:29:23 -04:00
For example:
2015-05-11 09:09:36 -04:00
2016-06-24 12:29:23 -04:00
```ruby
2016-06-15 17:38:12 -04:00
class MyMigration < ActiveRecord::Migration
2016-06-24 12:29:23 -04:00
DOWNTIME = true
DOWNTIME_REASON = 'This migration requires downtime because ...'
2016-06-15 17:38:12 -04:00
2016-06-24 12:29:23 -04:00
def change
...
end
end
```
2015-05-11 09:09:36 -04:00
2016-06-24 12:29:23 -04:00
It is an error (that is, CI will fail) if the `DOWNTIME` constant is missing
from a migration class.
2015-11-02 10:14:34 -05:00
2016-06-24 12:29:23 -04:00
## Reversibility
2015-05-11 09:09:36 -04:00
Your migration should be reversible. This is very important, as it should
be possible to downgrade in case of a vulnerability or bugs.
In your migration, add a comment describing how the reversibility of the
2015-05-12 04:48:18 -04:00
migration was tested.
## Removing indices
If you need to remove index, please add a condition like in following example:
2016-11-09 05:59:15 -05:00
```ruby
2015-05-12 04:48:18 -04:00
remove_index :namespaces, column: :name if index_exists?(:namespaces, :name)
```
## Adding indices
If you need to add an unique index please keep in mind there is possibility of existing duplicates. If it is possible write a separate migration for handling this situation. It can be just removing or removing with overwriting all references to these duplicates depend on situation.
2016-05-09 09:05:19 -04:00
When adding an index make sure to use the method `add_concurrent_index` instead
of the regular `add_index` method. The `add_concurrent_index` method
automatically creates concurrent indexes when using PostgreSQL, removing the
need for downtime. To use this method you must disable transactions by calling
the method `disable_ddl_transaction!` in the body of your migration class like
so:
2016-11-09 05:59:15 -05:00
```ruby
2016-05-09 09:05:19 -04:00
class MyMigration < ActiveRecord::Migration
2016-06-15 17:38:12 -04:00
include Gitlab::Database::MigrationHelpers
2016-05-09 09:05:19 -04:00
disable_ddl_transaction!
def change
end
end
```
## Adding Columns With Default Values
When adding columns with default values you should use the method
`add_column_with_default` . This method ensures the table is updated without
requiring downtime. This method is not reversible so you must manually define
the `up` and `down` methods in your migration class.
For example, to add the column `foo` to the `projects` table with a default
value of `10` you'd write the following:
2016-11-09 05:59:15 -05:00
```ruby
2016-05-09 09:05:19 -04:00
class MyMigration < ActiveRecord::Migration
2016-06-15 17:38:12 -04:00
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
2016-06-24 12:29:23 -04:00
2016-05-09 09:05:19 -04:00
def up
2016-06-15 17:38:12 -04:00
add_column_with_default(:projects, :foo, :integer, default: 10)
2016-05-09 09:05:19 -04:00
end
def down
remove_column(:projects, :foo)
end
end
```
2016-09-13 18:15:14 -04:00
## Integer column type
By default, an integer column can hold up to a 4-byte (32-bit) number. That is
a max value of 2,147,483,647. Be aware of this when creating a column that will
hold file sizes in byte units. If you are tracking file size in bytes this
restricts the maximum file size to just over 2GB.
To allow an integer column to hold up to an 8-byte (64-bit) number, explicitly
set the limit to 8-bytes. This will allow the column to hold a value up to
9,223,372,036,854,775,807.
Rails migration example:
2016-11-09 05:59:15 -05:00
```ruby
2016-09-13 18:15:14 -04:00
add_column_with_default(:projects, :foo, :integer, default: 10, limit: 8)
# or
add_column(:projects, :foo, :integer, default: 10, limit: 8)
```
2015-05-12 04:48:18 -04:00
## Testing
Make sure that your migration works with MySQL and PostgreSQL with data. An empty database does not guarantee that your migration is correct.
Make sure your migration can be reversed.
## Data migration
Please prefer Arel and plain SQL over usual ActiveRecord syntax. In case of using plain SQL you need to quote all input manually with `quote_string` helper.
Example with Arel:
2016-11-09 05:59:15 -05:00
```ruby
2015-05-12 04:48:18 -04:00
users = Arel::Table.new(:users)
users.group(users[:user_id]).having(users[:id].count.gt(5))
2016-05-30 01:31:39 -04:00
#update other tables with these results
2015-05-12 04:48:18 -04:00
```
Example with plain SQL and `quote_string` helper:
2016-11-09 05:59:15 -05:00
```ruby
2015-05-12 04:48:18 -04:00
select_all("SELECT name, COUNT(id) as cnt FROM tags GROUP BY name HAVING COUNT(id) > 1").each do |tag|
tag_name = quote_string(tag["name"])
duplicate_ids = select_all("SELECT id FROM tags WHERE name = '#{tag_name}'").map{|tag| tag["id"]}
origin_tag_id = duplicate_ids.first
duplicate_ids.delete origin_tag_id
execute("UPDATE taggings SET tag_id = #{origin_tag_id} WHERE tag_id IN(#{duplicate_ids.join(",")})")
execute("DELETE FROM tags WHERE id IN(#{duplicate_ids.join(",")})")
end
2016-05-09 09:05:19 -04:00
```