Commit fc5b3a8f authored by Z.J. van de Weg's avatar Z.J. van de Weg Committed by Yorick Peterse

Fix MySQL migration, obtain lock the right way

As suggested by @yorrickpeterse in
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/4581#note_12373882
the locking of the MySQL database wasn't correct.
parent f8290c28
...@@ -20,20 +20,21 @@ class ConvertAwardNoteToEmojiAward < ActiveRecord::Migration ...@@ -20,20 +20,21 @@ class ConvertAwardNoteToEmojiAward < ActiveRecord::Migration
def migrate_postgresql def migrate_postgresql
connection.transaction do connection.transaction do
execute 'LOCK notes IN EXCLUSIVE' execute 'LOCK notes IN EXCLUSIVE'
migrate_notes execute "INSERT INTO award_emoji (awardable_type, awardable_id, user_id, name, created_at, updated_at) (SELECT noteable_type, noteable_id, author_id, note, created_at, updated_at FROM notes WHERE is_award = true)"
execute "DELETE FROM notes WHERE is_award = true"
remove_column :notes, :is_award, :boolean
end end
end end
def migrate_mysql def migrate_mysql
execute 'LOCK TABLES notes WRITE' execute <<-EOF
migrate_notes lock tables notes WRITE, award_emoji WRITE;
ensure INSERT INTO award_emoji (awardable_type, awardable_id, user_id, name, created_at, updated_at) (SELECT noteable_type, noteable_id, author_id, note, created_at, updated_at FROM notes WHERE is_award = true);
execute 'UNLOCK TABLES' EOF
end
def migrate_notes
execute "INSERT INTO award_emoji (awardable_type, awardable_id, user_id, name, created_at, updated_at) (SELECT noteable_type, noteable_id, author_id, note, created_at, updated_at FROM notes WHERE is_award = true)"
execute "DELETE FROM notes WHERE is_award = true" execute "DELETE FROM notes WHERE is_award = true"
remove_column :notes, :is_award, :boolean remove_column :notes, :is_award, :boolean
ensure
execute 'UNLOCK TABLES'
end end
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