Split migration to create label priorities

This commit is contained in:
Douglas Barbosa Alexandre 2016-10-18 00:56:05 -02:00
parent d009d38ed6
commit 771d3fc3cb
4 changed files with 55 additions and 32 deletions

View file

@ -2,7 +2,7 @@ class CreateLabelPriorities < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = true
DOWNTIME_REASON = 'Prioritezed labels will not work as expected until this migration is complete.'
DOWNTIME_REASON = 'This migration adds foreign keys'
disable_ddl_transaction!
@ -15,41 +15,11 @@ class CreateLabelPriorities < ActiveRecord::Migration
t.timestamps null: false
end
execute <<-EOF.strip_heredoc
INSERT INTO label_priorities (project_id, label_id, priority, created_at, updated_at)
SELECT labels.project_id, labels.id, labels.priority, NOW(), NOW()
FROM labels
WHERE labels.project_id IS NOT NULL
AND labels.priority IS NOT NULL;
EOF
add_concurrent_index :label_priorities, [:project_id, :label_id], unique: true
add_concurrent_index :label_priorities, :priority
remove_column :labels, :priority
end
def down
add_column :labels, :priority, :integer
if Gitlab::Database.mysql?
execute <<-EOF.strip_heredoc
UPDATE labels
INNER JOIN label_priorities ON labels.id = label_priorities.label_id AND labels.project_id = label_priorities.project_id
SET labels.priority = label_priorities.priority;
EOF
else
execute <<-EOF.strip_heredoc
UPDATE labels
SET priority = label_priorities.priority
FROM label_priorities
WHERE labels.id = label_priorities.label_id
AND labels.project_id = label_priorities.project_id;
EOF
end
add_concurrent_index :labels, :priority
drop_table :label_priorities
end
end

View file

@ -0,0 +1,36 @@
class MigrateLabelsPriority < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = true
DOWNTIME_REASON = 'Prioritized labels will not work as expected until this migration is complete.'
disable_ddl_transaction!
def up
execute <<-EOF.strip_heredoc
INSERT INTO label_priorities (project_id, label_id, priority, created_at, updated_at)
SELECT labels.project_id, labels.id, labels.priority, NOW(), NOW()
FROM labels
WHERE labels.project_id IS NOT NULL
AND labels.priority IS NOT NULL;
EOF
end
def down
if Gitlab::Database.mysql?
execute <<-EOF.strip_heredoc
UPDATE labels
INNER JOIN label_priorities ON labels.id = label_priorities.label_id AND labels.project_id = label_priorities.project_id
SET labels.priority = label_priorities.priority;
EOF
else
execute <<-EOF.strip_heredoc
UPDATE labels
SET priority = label_priorities.priority
FROM label_priorities
WHERE labels.id = label_priorities.label_id
AND labels.project_id = label_priorities.project_id;
EOF
end
end
end

View file

@ -0,0 +1,17 @@
class RemovePriorityFromLabels < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = true
DOWNTIME_REASON = 'This migration removes an existing column'
disable_ddl_transaction!
def up
remove_column :labels, :priority, :integer, index: true
end
def down
add_column :labels, :priority, :integer
add_concurrent_index :labels, :priority
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161017125927) do
ActiveRecord::Schema.define(version: 20161018024550) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"