Add unique index to poll participations on author_id and poll_id
Previously we had only a Rails validation which ensured poll participation uniqueness but this adds uniqueness control to the database level, so that uniqueness is guaranteed even when changing data with avoiding Rails validations. closes #7798
This commit is contained in:
parent
b7cd9d6238
commit
e9f6dbdffd
3 changed files with 30 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
# 0.7.6.0
|
# 0.7.6.0
|
||||||
|
|
||||||
## Refactor
|
## Refactor
|
||||||
|
* Add unique index to poll participations on `poll_id` and `author_id` [#7798](https://github.com/diaspora/diaspora/pull/7798)
|
||||||
|
|
||||||
## Bug fixes
|
## Bug fixes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddPollParticipationsUniqueIndexOnAuthorAndPoll < ActiveRecord::Migration[5.1]
|
||||||
|
def change
|
||||||
|
reversible do |change|
|
||||||
|
change.up do
|
||||||
|
duplicate_query = "WHERE a1.poll_id = a2.poll_id AND a1.author_id = a2.author_id AND a1.id > a2.id"
|
||||||
|
if AppConfig.postgres?
|
||||||
|
execute("DELETE FROM poll_participations AS a1 USING poll_participations AS a2 #{duplicate_query}")
|
||||||
|
else
|
||||||
|
execute("DELETE a1 FROM poll_participations a1, poll_participations a2 #{duplicate_query}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :poll_participations, %i[poll_id author_id], unique: true
|
||||||
|
remove_index :poll_participations, :poll_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -24,6 +24,16 @@ describe PollParticipation, type: :model do
|
||||||
bob.participate_in_poll!(status, poll.poll_answers.first)
|
bob.participate_in_poll!(status, poll.poll_answers.first)
|
||||||
}.to_not raise_error
|
}.to_not raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has unique DB index for author-person" do
|
||||||
|
pp = FactoryGirl.create(:poll_participation)
|
||||||
|
pp2 = FactoryGirl.create(:poll_participation, author: pp.author)
|
||||||
|
expect {
|
||||||
|
# rubocop:disable Rails/SkipsModelValidations
|
||||||
|
pp2.update_attribute(:poll_id, pp.poll_id)
|
||||||
|
# rubocop:enable Rails/SkipsModelValidations
|
||||||
|
}.to raise_error ActiveRecord::RecordNotUnique
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it_behaves_like "it is relayable" do
|
it_behaves_like "it is relayable" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue