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

Fix timing issues on convertion migration award emoji

This commit does two things:
1. It adds logic which prevents timing issues when running the
migration. During the migration, notes can be created which _should_
be award emoji and thus migrated. To prevent these timing issues, a
lock is obtained on the table (MySQL) or on Transaction level (PG).
2. There was no down migration before as you'd probably lose some data.
Data effected is all awards on notes. These could be migrated back, as
the noteable type would just be Note, though this would litter the DB
with data which should not be there. This down migration does not yet
delete the table.
parent 0c0ef7df
# rubocop:disable all
class ConvertAwardNoteToEmojiAward < ActiveRecord::Migration
def change
def up
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)"
disable_ddl_transaction!
execute "DELETE FROM notes WHERE is_award = true"
def up
if Gitlab::Database.postgresql?
migrate_postgresql
else
migrate_mysql
end
end
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
end
# rubocop:disable all
class RemoveNoteIsAward < ActiveRecord::Migration
def change
remove_column :notes, :is_award, :boolean
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