diff --git a/db/migrate/20200120140900_add_user_notes_count_index.rb b/db/migrate/20200120140900_add_user_notes_count_index.rb new file mode 100644 index 0000000..11adfe2 --- /dev/null +++ b/db/migrate/20200120140900_add_user_notes_count_index.rb @@ -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 \ No newline at end of file