2016-05-09 08:14:32 -04:00
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module MigrationHelpers
|
|
|
|
# Creates a new index, concurrently when supported
|
|
|
|
#
|
|
|
|
# On PostgreSQL this method creates an index concurrently, on MySQL this
|
|
|
|
# creates a regular index.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# add_concurrent_index :users, :some_column
|
|
|
|
#
|
|
|
|
# See Rails' `add_index` for more info on the available arguments.
|
2016-06-06 10:30:17 -04:00
|
|
|
def add_concurrent_index(table_name, column_name, options = {})
|
2016-05-09 08:14:32 -04:00
|
|
|
if transaction_open?
|
|
|
|
raise 'add_concurrent_index can not be run inside a transaction, ' \
|
|
|
|
'you can disable transactions by calling disable_ddl_transaction! ' \
|
|
|
|
'in the body of your migration class'
|
|
|
|
end
|
|
|
|
|
|
|
|
if Database.postgresql?
|
2016-06-06 10:30:17 -04:00
|
|
|
options = options.merge({ algorithm: :concurrently })
|
2016-07-14 21:50:46 -04:00
|
|
|
disable_statement_timeout
|
2016-05-09 08:14:32 -04:00
|
|
|
end
|
|
|
|
|
2016-06-06 10:30:17 -04:00
|
|
|
add_index(table_name, column_name, options)
|
2016-05-09 08:14:32 -04:00
|
|
|
end
|
|
|
|
|
2016-07-14 21:39:08 -04:00
|
|
|
# Long-running migrations may take more than the timeout allowed by
|
|
|
|
# the database. Disable the session's statement timeout to ensure
|
|
|
|
# migrations don't get killed prematurely. (PostgreSQL only)
|
|
|
|
def disable_statement_timeout
|
|
|
|
ActiveRecord::Base.connection.execute('SET statement_timeout TO 0') if Database.postgresql?
|
|
|
|
end
|
|
|
|
|
2016-05-09 08:14:32 -04:00
|
|
|
# Updates the value of a column in batches.
|
|
|
|
#
|
|
|
|
# This method updates the table in batches of 5% of the total row count.
|
2016-06-15 10:42:52 -04:00
|
|
|
# This method will continue updating rows until no rows remain.
|
|
|
|
#
|
2016-06-16 06:50:11 -04:00
|
|
|
# When given a block this method will yield two values to the block:
|
2016-06-15 10:42:52 -04:00
|
|
|
#
|
|
|
|
# 1. An instance of `Arel::Table` for the table that is being updated.
|
|
|
|
# 2. The query to run as an Arel object.
|
|
|
|
#
|
|
|
|
# By supplying a block one can add extra conditions to the queries being
|
|
|
|
# executed. Note that the same block is used for _all_ queries.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# update_column_in_batches(:projects, :foo, 10) do |table, query|
|
|
|
|
# query.where(table[:some_column].eq('hello'))
|
|
|
|
# end
|
|
|
|
#
|
2016-06-16 06:50:11 -04:00
|
|
|
# This would result in this method updating only rows where
|
2016-06-15 10:42:52 -04:00
|
|
|
# `projects.some_column` equals "hello".
|
2016-05-09 08:14:32 -04:00
|
|
|
#
|
|
|
|
# table - The name of the table.
|
|
|
|
# column - The name of the column to update.
|
|
|
|
# value - The value for the column.
|
2016-06-16 06:50:11 -04:00
|
|
|
#
|
|
|
|
# Rubocop's Metrics/AbcSize metric is disabled for this method as Rubocop
|
|
|
|
# determines this method to be too complex while there's no way to make it
|
|
|
|
# less "complex" without introducing extra methods (which actually will
|
|
|
|
# make things _more_ complex).
|
|
|
|
#
|
|
|
|
# rubocop: disable Metrics/AbcSize
|
2016-05-09 08:14:32 -04:00
|
|
|
def update_column_in_batches(table, column, value)
|
2016-06-15 10:42:52 -04:00
|
|
|
table = Arel::Table.new(table)
|
2016-05-09 08:14:32 -04:00
|
|
|
|
2016-06-15 10:42:52 -04:00
|
|
|
count_arel = table.project(Arel.star.count.as('count'))
|
|
|
|
count_arel = yield table, count_arel if block_given?
|
|
|
|
|
|
|
|
total = exec_query(count_arel.to_sql).to_hash.first['count'].to_i
|
2016-05-09 08:14:32 -04:00
|
|
|
|
2016-06-16 06:50:11 -04:00
|
|
|
return if total == 0
|
|
|
|
|
2016-06-13 05:38:57 -04:00
|
|
|
# Update in batches of 5% until we run out of any rows to update.
|
2016-05-09 08:14:32 -04:00
|
|
|
batch_size = ((total / 100.0) * 5.0).ceil
|
|
|
|
|
2016-06-16 06:50:11 -04:00
|
|
|
start_arel = table.project(table[:id]).order(table[:id].asc).take(1)
|
|
|
|
start_arel = yield table, start_arel if block_given?
|
|
|
|
start_id = exec_query(start_arel.to_sql).to_hash.first['id'].to_i
|
2016-06-13 05:38:57 -04:00
|
|
|
|
2016-06-16 06:50:11 -04:00
|
|
|
loop do
|
2016-06-15 10:42:52 -04:00
|
|
|
stop_arel = table.project(table[:id]).
|
2016-06-16 06:50:11 -04:00
|
|
|
where(table[:id].gteq(start_id)).
|
2016-06-15 10:42:52 -04:00
|
|
|
order(table[:id].asc).
|
|
|
|
take(1).
|
2016-06-16 06:50:11 -04:00
|
|
|
skip(batch_size)
|
2016-06-15 10:42:52 -04:00
|
|
|
|
|
|
|
stop_arel = yield table, stop_arel if block_given?
|
|
|
|
stop_row = exec_query(stop_arel.to_sql).to_hash.first
|
2016-05-09 08:14:32 -04:00
|
|
|
|
2016-06-16 06:50:11 -04:00
|
|
|
update_arel = Arel::UpdateManager.new(ActiveRecord::Base).
|
|
|
|
table(table).
|
2016-06-15 10:42:52 -04:00
|
|
|
set([[table[column], value]]).
|
2016-06-16 06:50:11 -04:00
|
|
|
where(table[:id].gteq(start_id))
|
2016-05-09 08:14:32 -04:00
|
|
|
|
|
|
|
if stop_row
|
2016-06-16 06:50:11 -04:00
|
|
|
stop_id = stop_row['id'].to_i
|
|
|
|
start_id = stop_id
|
|
|
|
update_arel = update_arel.where(table[:id].lt(stop_id))
|
2016-05-09 08:14:32 -04:00
|
|
|
end
|
|
|
|
|
2016-06-16 06:50:11 -04:00
|
|
|
update_arel = yield table, update_arel if block_given?
|
|
|
|
|
2016-06-15 10:42:52 -04:00
|
|
|
execute(update_arel.to_sql)
|
2016-05-09 08:14:32 -04:00
|
|
|
|
2016-06-16 06:50:11 -04:00
|
|
|
# There are no more rows left to update.
|
|
|
|
break unless stop_row
|
2016-05-09 08:14:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Adds a column with a default value without locking an entire table.
|
|
|
|
#
|
|
|
|
# This method runs the following steps:
|
|
|
|
#
|
|
|
|
# 1. Add the column with a default value of NULL.
|
2016-06-15 10:42:52 -04:00
|
|
|
# 2. Change the default value of the column to the specified value.
|
|
|
|
# 3. Update all existing rows in batches.
|
|
|
|
# 4. Set a `NOT NULL` constraint on the column if desired (the default).
|
2016-05-09 08:14:32 -04:00
|
|
|
#
|
|
|
|
# These steps ensure a column can be added to a large and commonly used
|
|
|
|
# table without locking the entire table for the duration of the table
|
|
|
|
# modification.
|
|
|
|
#
|
|
|
|
# table - The name of the table to update.
|
|
|
|
# column - The name of the column to add.
|
|
|
|
# type - The column type (e.g. `:integer`).
|
|
|
|
# default - The default value for the column.
|
2016-09-13 18:15:14 -04:00
|
|
|
# limit - Sets a column limit. For example, for :integer, the default is
|
|
|
|
# 4-bytes. Set `limit: 8` to allow 8-byte integers.
|
2016-05-09 08:14:32 -04:00
|
|
|
# allow_null - When set to `true` the column will allow NULL values, the
|
|
|
|
# default is to not allow NULL values.
|
2016-06-15 10:42:52 -04:00
|
|
|
#
|
|
|
|
# This method can also take a block which is passed directly to the
|
|
|
|
# `update_column_in_batches` method.
|
2016-09-13 18:15:14 -04:00
|
|
|
def add_column_with_default(table, column, type, default:, limit: nil, allow_null: false, &block)
|
2016-05-09 08:14:32 -04:00
|
|
|
if transaction_open?
|
|
|
|
raise 'add_column_with_default can not be run inside a transaction, ' \
|
|
|
|
'you can disable transactions by calling disable_ddl_transaction! ' \
|
|
|
|
'in the body of your migration class'
|
|
|
|
end
|
|
|
|
|
2016-07-14 21:50:46 -04:00
|
|
|
disable_statement_timeout
|
|
|
|
|
2016-05-09 08:14:32 -04:00
|
|
|
transaction do
|
2016-09-13 18:15:14 -04:00
|
|
|
if limit
|
|
|
|
add_column(table, column, type, default: nil, limit: limit)
|
|
|
|
else
|
|
|
|
add_column(table, column, type, default: nil)
|
|
|
|
end
|
2016-05-09 08:14:32 -04:00
|
|
|
|
|
|
|
# Changing the default before the update ensures any newly inserted
|
|
|
|
# rows already use the proper default value.
|
|
|
|
change_column_default(table, column, default)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2016-06-15 11:04:07 -04:00
|
|
|
update_column_in_batches(table, column, default, &block)
|
2016-06-13 05:22:58 -04:00
|
|
|
|
2016-06-15 11:04:07 -04:00
|
|
|
change_column_null(table, column, false) unless allow_null
|
2016-05-11 08:11:04 -04:00
|
|
|
# We want to rescue _all_ exceptions here, even those that don't inherit
|
|
|
|
# from StandardError.
|
|
|
|
rescue Exception => error # rubocop: disable all
|
2016-05-09 08:14:32 -04:00
|
|
|
remove_column(table, column)
|
|
|
|
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|