FIX: Add unique index to user_notes_count

This commit is contained in:
David Taylor 2020-01-20 14:16:44 +00:00
parent 3ed02456a6
commit 02563bdeab
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
class AddUserNotesCountIndex < ActiveRecord::Migration[5.2]
def up
execute <<~SQL
DELETE
FROM user_custom_fields a
USING user_custom_fields b
WHERE a.name = 'user_notes_count'
AND a.name = b.name
AND a.id > b.id
SQL
add_index :user_custom_fields,
[:name, :user_id],
unique: true,
name: :idx_user_custom_fields_user_notes_count,
where: "name = 'user_notes_count'"
end
def down
remove_index :user_custom_fields, name: :idx_user_custom_fields_user_notes_count
end
end