20160416182152_convert_award_note_to_emoji_award.rb 1.2 KB
Newer Older
1
# rubocop:disable all
2
class ConvertAwardNoteToEmojiAward < ActiveRecord::Migration
3
  disable_ddl_transaction!
Z.J. van de Weg's avatar
Z.J. van de Weg committed
4

5 6 7 8 9
  def up
    if Gitlab::Database.postgresql?
      migrate_postgresql
    else
      migrate_mysql
10 11
    end
  end
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

  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
      execute 'LOCK notes IN EXCLUSIVE'
      migrate_notes
    end
  end

  def migrate_mysql
    execute 'LOCK TABLES notes WRITE'
    migrate_notes
  ensure
    execute 'UNLOCK TABLES'
  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"
    remove_column :notes, :is_award, :boolean
  end
39
end