diff --git a/db/migrate/20160416182152_convert_award_note_to_emoji_award.rb b/db/migrate/20160416182152_convert_award_note_to_emoji_award.rb index b523f17417ad068f29b602eed288d3c316bd4836..3906ab79398cccac0fe42e7da5f2afcb73ec9b31 100644 --- a/db/migrate/20160416182152_convert_award_note_to_emoji_award.rb +++ b/db/migrate/20160416182152_convert_award_note_to_emoji_award.rb @@ -20,20 +20,21 @@ class ConvertAwardNoteToEmojiAward < ActiveRecord::Migration def migrate_postgresql connection.transaction do 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 def migrate_mysql - execute 'LOCK TABLES notes WRITE' - migrate_notes - ensure - execute 'UNLOCK TABLES' - end + execute <<-EOF + lock tables notes WRITE, award_emoji WRITE; + 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); + EOF - 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" remove_column :notes, :is_award, :boolean + ensure + execute 'UNLOCK TABLES' end end