Commit 4c0292a4 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Remove relative_position default value

Add migration to set existing child epics' positions
parent bc39e655
...@@ -1114,7 +1114,7 @@ ActiveRecord::Schema.define(version: 20190220150130) do ...@@ -1114,7 +1114,7 @@ ActiveRecord::Schema.define(version: 20190220150130) do
t.integer "closed_by_id" t.integer "closed_by_id"
t.datetime "closed_at" t.datetime "closed_at"
t.integer "parent_id" t.integer "parent_id"
t.integer "relative_position", default: 1073741823, null: false t.integer "relative_position"
t.index ["assignee_id"], name: "index_epics_on_assignee_id", using: :btree t.index ["assignee_id"], name: "index_epics_on_assignee_id", using: :btree
t.index ["author_id"], name: "index_epics_on_author_id", using: :btree t.index ["author_id"], name: "index_epics_on_author_id", using: :btree
t.index ["closed_by_id"], name: "index_epics_on_closed_by_id", using: :btree t.index ["closed_by_id"], name: "index_epics_on_closed_by_id", using: :btree
......
...@@ -8,11 +8,8 @@ class AddRelativePositionToEpics < ActiveRecord::Migration[5.0] ...@@ -8,11 +8,8 @@ class AddRelativePositionToEpics < ActiveRecord::Migration[5.0]
DOWNTIME = false DOWNTIME = false
disable_ddl_transaction!
def up def up
default_position = Gitlab::Database::MAX_INT_VALUE / 2 add_column :epics, :relative_position, :integer
add_column_with_default(:epics, :relative_position, :integer, default: default_position, allow_null: false)
end end
def down def down
......
# frozen_string_literal: true
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class SetDefaultPositionForChildEpics < ActiveRecord::Migration[5.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
current_position = nil
current_parent_id = nil
connection.exec_query(existing_child_epics_query.to_sql).rows.each do |id, parent_id|
if parent_id != current_parent_id
current_position = Gitlab::Database::MAX_INT_VALUE / 2
current_parent_id = parent_id
else
current_position += 500
end
update_position(id, current_position)
end
end
def down
end
private
def epics_table
@epics_table ||= Arel::Table.new(:epics)
end
def existing_child_epics_query
epics_table.project(epics_table[:id], epics_table[:parent_id])
.where(epics_table[:parent_id].not_eq(nil))
.order(epics_table[:parent_id], epics_table[:id].desc)
end
def update_position(epic_id, position)
execute "UPDATE epics SET relative_position = #{position} WHERE id = #{epic_id}"
end
end
...@@ -19,7 +19,7 @@ describe EpicLinks::CreateService, :postgresql do ...@@ -19,7 +19,7 @@ describe EpicLinks::CreateService, :postgresql do
end end
it 'moves the new child epic to the top and moves the existing ones down' do it 'moves the new child epic to the top and moves the existing ones down' do
existing_child_epic = create(:epic, group: group, parent: epic) existing_child_epic = create(:epic, group: group, parent: epic, relative_position: 1000)
subject subject
......
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