Commit a53a6b80 authored by Jan Provaznik's avatar Jan Provaznik

Add epics.relative_position column if missing

If this column is missing for some reason, add it and
backfill it with epic.id (to assure it's not nil).

Changelog: added
parent c64ce000
# frozen_string_literal: true
class AddEpicsRelativePosition < Gitlab::Database::Migration[1.0]
DOWNTIME = false
def up
return unless table_exists?(:epics)
return if column_exists?(:epics, :relative_position)
add_column :epics, :relative_position, :integer
execute('UPDATE epics SET relative_position=id*500')
end
def down
# no-op - this column should normally exist if epics table exists too
end
end
ab7bb319a7099714d9863ec16b7dcf8c1aeab495b8635a01dff4a51fab876b6b
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe AddEpicsRelativePosition, :migration do
let(:groups) { table(:namespaces) }
let(:epics) { table(:epics) }
let(:users) { table(:users) }
let(:user) { users.create!(name: 'user', email: 'email@example.org', projects_limit: 100) }
let(:group) { groups.create!(name: 'gitlab', path: 'gitlab-org', type: 'Group') }
let!(:epic1) { epics.create!(title: 'epic 1', title_html: 'epic 1', author_id: user.id, group_id: group.id, iid: 1) }
let!(:epic2) { epics.create!(title: 'epic 2', title_html: 'epic 2', author_id: user.id, group_id: group.id, iid: 2) }
let!(:epic3) { epics.create!(title: 'epic 3', title_html: 'epic 3', author_id: user.id, group_id: group.id, iid: 3) }
it 'does nothing if epics table contains relative_position' do
expect { migrate! }.not_to change { epics.pluck(:relative_position) }
end
it 'adds relative_position if missing and backfills it with ID value', :aggregate_failures do
ActiveRecord::Base.connection.execute('ALTER TABLE epics DROP relative_position')
migrate!
expect(epics.pluck(:relative_position)).to match_array([epic1.id * 500, epic2.id * 500, epic3.id * 500])
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment