diff --git a/lib/data_update_scripts/20200818170410_remove_orphaned_poll_votes.rb b/lib/data_update_scripts/20200818170410_remove_orphaned_poll_votes.rb new file mode 100644 index 000000000..3f7ebc58a --- /dev/null +++ b/lib/data_update_scripts/20200818170410_remove_orphaned_poll_votes.rb @@ -0,0 +1,29 @@ +module DataUpdateScripts + class RemoveOrphanedPollVotes + def run + # Delete all PollVotes belonging to Polls that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_votes + WHERE poll_id NOT IN (SELECT id FROM polls); + SQL + ) + + # Delete all PollVotes belonging to PollOptions that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_votes + WHERE poll_option_id NOT IN (SELECT id FROM poll_options); + SQL + ) + + # Delete all PollVotes belonging to Users that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_votes + WHERE user_id NOT IN (SELECT id FROM users); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200818170433_remove_orphaned_poll_skips.rb b/lib/data_update_scripts/20200818170433_remove_orphaned_poll_skips.rb new file mode 100644 index 000000000..1f9975b45 --- /dev/null +++ b/lib/data_update_scripts/20200818170433_remove_orphaned_poll_skips.rb @@ -0,0 +1,21 @@ +module DataUpdateScripts + class RemoveOrphanedPollSkips + def run + # Delete all PollSkips belonging to Polls that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_skips + WHERE poll_id NOT IN (SELECT id FROM polls); + SQL + ) + + # Delete all PollSkips belonging to Users that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_skips + WHERE user_id NOT IN (SELECT id FROM users); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200818170505_remove_orphaned_poll_options.rb b/lib/data_update_scripts/20200818170505_remove_orphaned_poll_options.rb new file mode 100644 index 000000000..d8ab7698b --- /dev/null +++ b/lib/data_update_scripts/20200818170505_remove_orphaned_poll_options.rb @@ -0,0 +1,21 @@ +module DataUpdateScripts + class RemoveOrphanedPollOptions + def run + # Delete all PollOptions belonging to Polls that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_options + WHERE poll_id NOT IN (SELECT id FROM polls); + SQL + ) + + # Delete all PollOptions belonging to Polls that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM poll_options + WHERE poll_id NOT IN (SELECT id FROM polls); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200818170523_remove_orphaned_polls.rb b/lib/data_update_scripts/20200818170523_remove_orphaned_polls.rb new file mode 100644 index 000000000..c024fc1fe --- /dev/null +++ b/lib/data_update_scripts/20200818170523_remove_orphaned_polls.rb @@ -0,0 +1,13 @@ +module DataUpdateScripts + class RemoveOrphanedPolls + def run + # Delete all Polls belonging to Articles that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM polls + WHERE article_id NOT IN (SELECT id FROM articles); + SQL + ) + end + end +end