2018-02-01 10:23:32 -05:00
|
|
|
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
|
|
|
|
# for more information on how to write migrations for GitLab.
|
|
|
|
|
2018-11-13 02:27:31 -05:00
|
|
|
class MigrateRemainingIssuesClosedAt < ActiveRecord::Migration[4.2]
|
2018-02-01 10:23:32 -05:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
# Set this constant to true if this migration requires downtime.
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
class Issue < ActiveRecord::Base
|
|
|
|
self.table_name = 'issues'
|
|
|
|
include EachBatch
|
|
|
|
end
|
|
|
|
|
|
|
|
def up
|
|
|
|
Gitlab::BackgroundMigration.steal('CopyColumn')
|
|
|
|
Gitlab::BackgroundMigration.steal('CleanupConcurrentTypeChange')
|
|
|
|
|
2018-02-05 09:07:18 -05:00
|
|
|
if migrate_column_type?
|
|
|
|
if closed_at_for_type_change_exists?
|
|
|
|
migrate_remaining_rows
|
|
|
|
else
|
|
|
|
# Due to some EE merge problems some environments may not have the
|
|
|
|
# "closed_at_for_type_change" column. If this is the case we have no
|
|
|
|
# other option than to migrate the data _right now_.
|
2018-07-25 04:59:23 -04:00
|
|
|
# rubocop:disable Migration/UpdateLargeTable
|
2018-02-05 09:07:18 -05:00
|
|
|
change_column_type_concurrently(:issues, :closed_at, :datetime_with_timezone)
|
|
|
|
cleanup_concurrent_column_type_change(:issues, :closed_at)
|
|
|
|
end
|
|
|
|
end
|
2018-02-01 10:23:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
2018-02-05 09:07:18 -05:00
|
|
|
# Previous migrations already revert the changes made here.
|
2018-02-01 10:23:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_remaining_rows
|
|
|
|
Issue.where('closed_at_for_type_change IS NULL AND closed_at IS NOT NULL').each_batch do |batch|
|
|
|
|
batch.update_all('closed_at_for_type_change = closed_at')
|
|
|
|
end
|
|
|
|
|
|
|
|
cleanup_concurrent_column_type_change(:issues, :closed_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_column_type?
|
|
|
|
# Some environments may have already executed the previous version of this
|
|
|
|
# migration, thus we don't need to migrate those environments again.
|
|
|
|
column_for('issues', 'closed_at').type == :datetime # rubocop:disable Migration/Datetime
|
|
|
|
end
|
2018-02-05 09:07:18 -05:00
|
|
|
|
|
|
|
def closed_at_for_type_change_exists?
|
|
|
|
columns('issues').any? { |col| col.name == 'closed_at_for_type_change' }
|
|
|
|
end
|
2018-02-01 10:23:32 -05:00
|
|
|
end
|