Add missing unique indexes to notifications (#8273)

This commit is contained in:
rhymes 2020-06-05 09:19:22 +02:00 committed by GitHub
parent dca570c87f
commit 753bdc5dc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View file

@ -82,7 +82,6 @@ Rails/UniqueValidationWithoutIndex:
- 'app/models/article.rb'
- 'app/models/comment.rb'
- 'app/models/follow.rb'
- 'app/models/notification.rb'
- 'app/models/broadcast.rb'
# Offense count: 33

View file

@ -0,0 +1,19 @@
class AddUniqueIndexToNotificationsUserIdOrganizationIdNotifiableAction < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def change
return if index_exists?(
:notifications,
%i[user_id organization_id notifiable_id notifiable_type action],
name: :index_notifications_user_id_organization_id_notifiable_action
)
add_index(
:notifications,
%i[user_id organization_id notifiable_id notifiable_type action],
unique: true,
algorithm: :concurrently,
name: :index_notifications_user_id_organization_id_notifiable_action
)
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_06_02_174329) do
ActiveRecord::Schema.define(version: 2020_06_04_133925) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -683,6 +683,7 @@ ActiveRecord::Schema.define(version: 2020_06_02_174329) do
t.index ["organization_id"], name: "index_notifications_on_organization_id"
t.index ["user_id", "notifiable_id", "notifiable_type", "action"], name: "index_notifications_on_user_notifiable_and_action_not_null", unique: true, where: "(action IS NOT NULL)"
t.index ["user_id", "notifiable_id", "notifiable_type"], name: "index_notifications_on_user_notifiable_action_is_null", unique: true, where: "(action IS NULL)"
t.index ["user_id", "organization_id", "notifiable_id", "notifiable_type", "action"], name: "index_notifications_user_id_organization_id_notifiable_action", unique: true
t.index ["user_id"], name: "index_notifications_on_user_id"
end