2016-06-09 12:39:16 +00:00
|
|
|
# rubocop:disable all
|
2016-04-16 19:09:08 +00:00
|
|
|
class ConvertAwardNoteToEmojiAward < ActiveRecord::Migration
|
2016-06-10 08:00:32 +00:00
|
|
|
disable_ddl_transaction!
|
2016-06-01 09:23:09 +00:00
|
|
|
|
2016-06-10 08:00:32 +00:00
|
|
|
def up
|
|
|
|
if Gitlab::Database.postgresql?
|
|
|
|
migrate_postgresql
|
|
|
|
else
|
|
|
|
migrate_mysql
|
2016-04-16 19:09:08 +00:00
|
|
|
end
|
|
|
|
end
|
2016-06-10 08:00:32 +00:00
|
|
|
|
|
|
|
def down
|
|
|
|
add_column :notes, :is_award, :boolean
|
|
|
|
|
|
|
|
# This migration does NOT move the awards on notes, if the table is dropped in another migration, these notes will be lost.
|
|
|
|
execute "INSERT INTO notes (noteable_type, noteable_id, author_id, note, created_at, updated_at, is_award) (SELECT awardable_type, awardable_id, user_id, name, created_at, updated_at, TRUE FROM award_emoji)"
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_postgresql
|
|
|
|
connection.transaction do
|
2016-06-14 10:17:41 +00:00
|
|
|
execute 'LOCK notes IN EXCLUSIVE MODE'
|
2016-06-10 15:33:23 +00:00
|
|
|
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
|
2016-06-10 08:00:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_mysql
|
2016-06-13 12:15:11 +00:00
|
|
|
execute 'LOCK TABLES notes WRITE, award_emoji WRITE;'
|
|
|
|
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);'
|
2016-06-10 08:00:32 +00:00
|
|
|
execute "DELETE FROM notes WHERE is_award = true"
|
|
|
|
remove_column :notes, :is_award, :boolean
|
2016-06-10 15:33:23 +00:00
|
|
|
ensure
|
|
|
|
execute 'UNLOCK TABLES'
|
2016-06-10 08:00:32 +00:00
|
|
|
end
|
2016-04-16 19:09:08 +00:00
|
|
|
end
|