diff --git a/Changelog.md b/Changelog.md index ad9048d7a..3cf1fa48e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -20,6 +20,7 @@ * Prevent users from zooming in IE Mobile [#7589](https://github.com/diaspora/diaspora/pull/7589) * Fix recipient prefill on contacts and profile page [#7599](https://github.com/diaspora/diaspora/pull/7599) * Display likes and reshares without login [#7583](https://github.com/diaspora/diaspora/pull/7583) +* Fix invalid data in the database for user data export [#7614](https://github.com/diaspora/diaspora/pull/7614) ## Features * Ask for confirmation when leaving a submittable comment field [#7530](https://github.com/diaspora/diaspora/pull/7530) diff --git a/app/models/poll.rb b/app/models/poll.rb index 3a72b88b3..40e079062 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -5,8 +5,8 @@ class Poll < ApplicationRecord include Diaspora::Fields::Guid belongs_to :status_message - has_many :poll_answers, -> { order 'id ASC' } - has_many :poll_participations + has_many :poll_answers, -> { order "id ASC" }, dependent: :destroy + has_many :poll_participations, dependent: :destroy has_one :author, through: :status_message #forward some requests to status message, because a poll is just attached to a status message and is not sharable itself diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 8ea4bbeba..43ae1697d 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -20,7 +20,7 @@ class StatusMessage < Post has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid has_one :location - has_one :poll, autosave: true + has_one :poll, autosave: true, dependent: :destroy has_many :poll_participations, through: :poll attr_accessor :oembed_url diff --git a/db/migrate/20170914212336_cleanup_invalid_polls.rb b/db/migrate/20170914212336_cleanup_invalid_polls.rb new file mode 100644 index 000000000..7d7cda97f --- /dev/null +++ b/db/migrate/20170914212336_cleanup_invalid_polls.rb @@ -0,0 +1,19 @@ +class CleanupInvalidPolls < ActiveRecord::Migration[5.1] + class Poll < ApplicationRecord + has_many :poll_answers, dependent: :destroy + has_many :poll_participations, dependent: :destroy + end + class PollAnswer < ApplicationRecord + belongs_to :poll + has_many :poll_participations + end + class PollParticipation < ApplicationRecord + belongs_to :poll + belongs_to :poll_answer + end + + def up + Poll.joins("LEFT OUTER JOIN posts ON posts.id = polls.status_message_id") + .where("posts.id IS NULL").destroy_all + end +end diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index 531a602e6..8c1165a21 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -233,6 +233,23 @@ describe StatusMessage, type: :model do end end + describe "poll" do + it "destroys the poll (with all answers and participations) when the status message is destroyed" do + poll = FactoryGirl.create(:poll_participation).poll + status_message = poll.status_message + + poll_id = poll.id + poll_answers = poll.poll_answers.map(&:id) + poll_participations = poll.poll_participations.map(&:id) + + status_message.destroy + + expect(Poll.where(id: poll_id)).not_to exist + poll_answers.each {|id| expect(PollAnswer.where(id: id)).not_to exist } + poll_participations.each {|id| expect(PollParticipation.where(id: id)).not_to exist } + end + end + describe "validation" do let(:status_message) { build(:status_message, text: @message_text) }