From 66b5cb513fec64b0edde1fa40c1e2bfa5e45980b Mon Sep 17 00:00:00 2001 From: rhymes Date: Mon, 24 Aug 2020 19:35:02 +0200 Subject: [PATCH] [deploy] Add dependent clauses to relations for Organization and add cleanup scripts (#9932) * Add dependent clauses to has_many relations in Organization * Add cleanup scripts * Update script * Destroy organizations listings * Update lib/data_update_scripts/20200822082229_remove_orphaned_notifications_by_organization.rb Co-authored-by: Molly Struve Co-authored-by: Molly Struve --- .rubocop_todo.yml | 1 - app/models/organization.rb | 14 +++++------ ...llify_orphaned_articles_by_organization.rb | 15 ++++++++++++ ...fy_orphaned_collections_by_organization.rb | 15 ++++++++++++ ...remove_orphaned_credits_by_organization.rb | 23 +++++++++++++++++++ ...ve_orphaned_display_ads_by_organization.rb | 13 +++++++++++ ...emove_orphaned_listings_by_organization.rb | 14 +++++++++++ ..._orphaned_notifications_by_organization.rb | 13 +++++++++++ ...e_orphaned_sponsorships_by_organization.rb | 13 +++++++++++ spec/models/organization_spec.rb | 16 ++++++------- 10 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 lib/data_update_scripts/20200821103125_nullify_orphaned_articles_by_organization.rb create mode 100644 lib/data_update_scripts/20200821103305_nullify_orphaned_collections_by_organization.rb create mode 100644 lib/data_update_scripts/20200821103405_remove_orphaned_credits_by_organization.rb create mode 100644 lib/data_update_scripts/20200821103718_remove_orphaned_display_ads_by_organization.rb create mode 100644 lib/data_update_scripts/20200821103834_remove_orphaned_listings_by_organization.rb create mode 100644 lib/data_update_scripts/20200822082229_remove_orphaned_notifications_by_organization.rb create mode 100644 lib/data_update_scripts/20200822083050_remove_orphaned_sponsorships_by_organization.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a796b5c91..854486860 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -25,7 +25,6 @@ Rails/HasManyOrHasOneDependent: Exclude: - 'app/models/article.rb' - 'app/models/concerns/user_subscription_sourceable.rb' - - 'app/models/organization.rb' - 'app/models/user.rb' # Offense count: 4 diff --git a/app/models/organization.rb b/app/models/organization.rb index 0a3661a35..385340a77 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -12,15 +12,15 @@ class Organization < ApplicationRecord acts_as_followable has_many :api_secrets, through: :users - has_many :articles - has_many :listings - has_many :collections + has_many :articles, dependent: :nullify + has_many :collections, dependent: :nullify has_many :credits, dependent: :restrict_with_error - has_many :display_ads - has_many :notifications + has_many :display_ads, dependent: :destroy + has_many :listings, dependent: :destroy + has_many :notifications, dependent: :destroy has_many :organization_memberships, dependent: :delete_all - has_many :profile_pins, as: :profile, inverse_of: :profile - has_many :sponsorships + has_many :profile_pins, as: :profile, inverse_of: :profile, dependent: :destroy + has_many :sponsorships, dependent: :destroy has_many :unspent_credits, -> { where spent: false }, class_name: "Credit", inverse_of: :organization has_many :users, through: :organization_memberships diff --git a/lib/data_update_scripts/20200821103125_nullify_orphaned_articles_by_organization.rb b/lib/data_update_scripts/20200821103125_nullify_orphaned_articles_by_organization.rb new file mode 100644 index 000000000..de2e59279 --- /dev/null +++ b/lib/data_update_scripts/20200821103125_nullify_orphaned_articles_by_organization.rb @@ -0,0 +1,15 @@ +module DataUpdateScripts + class NullifyOrphanedArticlesByOrganization + def run + # Nullify organization_id for all Articles linked to a non existing Organization + ActiveRecord::Base.connection.execute( + <<~SQL, + UPDATE articles + SET organization_id = NULL + WHERE organization_id IS NOT NULL + AND organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200821103305_nullify_orphaned_collections_by_organization.rb b/lib/data_update_scripts/20200821103305_nullify_orphaned_collections_by_organization.rb new file mode 100644 index 000000000..3080cb066 --- /dev/null +++ b/lib/data_update_scripts/20200821103305_nullify_orphaned_collections_by_organization.rb @@ -0,0 +1,15 @@ +module DataUpdateScripts + class NullifyOrphanedCollectionsByOrganization + def run + # Nullify organization_id for all Collections linked to a non existing Organization + ActiveRecord::Base.connection.execute( + <<~SQL, + UPDATE collections + SET organization_id = NULL + WHERE organization_id IS NOT NULL + AND organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200821103405_remove_orphaned_credits_by_organization.rb b/lib/data_update_scripts/20200821103405_remove_orphaned_credits_by_organization.rb new file mode 100644 index 000000000..8b25352b6 --- /dev/null +++ b/lib/data_update_scripts/20200821103405_remove_orphaned_credits_by_organization.rb @@ -0,0 +1,23 @@ +module DataUpdateScripts + class RemoveOrphanedCreditsByOrganization + def run + # Apparently we have a bunch of Credits that don't belong to either user or org + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM credits + WHERE user_id IS NULL + AND organization_id IS NULL + SQL + ) + + # Delete all User less Credits belonging to Organizations that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM credits + WHERE user_id IS NULL + AND organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200821103718_remove_orphaned_display_ads_by_organization.rb b/lib/data_update_scripts/20200821103718_remove_orphaned_display_ads_by_organization.rb new file mode 100644 index 000000000..a9732e97b --- /dev/null +++ b/lib/data_update_scripts/20200821103718_remove_orphaned_display_ads_by_organization.rb @@ -0,0 +1,13 @@ +module DataUpdateScripts + class RemoveOrphanedDisplayAdsByOrganization + def run + # Delete all DisplayAds belonging to Organizations that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM display_ads + WHERE organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200821103834_remove_orphaned_listings_by_organization.rb b/lib/data_update_scripts/20200821103834_remove_orphaned_listings_by_organization.rb new file mode 100644 index 000000000..5e980cb12 --- /dev/null +++ b/lib/data_update_scripts/20200821103834_remove_orphaned_listings_by_organization.rb @@ -0,0 +1,14 @@ +module DataUpdateScripts + class RemoveOrphanedListingsByOrganization + def run + # Delete all Listings belonging to Organizations that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM classified_listings + WHERE organization_id IS NOT NULL + AND organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200822082229_remove_orphaned_notifications_by_organization.rb b/lib/data_update_scripts/20200822082229_remove_orphaned_notifications_by_organization.rb new file mode 100644 index 000000000..89c81ea29 --- /dev/null +++ b/lib/data_update_scripts/20200822082229_remove_orphaned_notifications_by_organization.rb @@ -0,0 +1,13 @@ +module DataUpdateScripts + class RemoveOrphanedNotificationsByOrganization + def run + # Delete all Notifications belonging to Organizations that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM notifications + WHERE organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/lib/data_update_scripts/20200822083050_remove_orphaned_sponsorships_by_organization.rb b/lib/data_update_scripts/20200822083050_remove_orphaned_sponsorships_by_organization.rb new file mode 100644 index 000000000..e9f173316 --- /dev/null +++ b/lib/data_update_scripts/20200822083050_remove_orphaned_sponsorships_by_organization.rb @@ -0,0 +1,13 @@ +module DataUpdateScripts + class RemoveOrphanedSponsorshipsByOrganization + def run + # Delete all Sponsorships belonging to Organizations that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL, + DELETE FROM sponsorships + WHERE organization_id NOT IN (SELECT id FROM organizations); + SQL + ) + end + end +end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 5855084cb..99b3eaa25 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -8,15 +8,15 @@ RSpec.describe Organization, type: :model do subject { organization } it { is_expected.to have_many(:api_secrets).through(:users) } - it { is_expected.to have_many(:articles) } - it { is_expected.to have_many(:listings) } - it { is_expected.to have_many(:collections) } - it { is_expected.to have_many(:credits) } - it { is_expected.to have_many(:display_ads) } - it { is_expected.to have_many(:notifications) } + it { is_expected.to have_many(:articles).dependent(:nullify) } + it { is_expected.to have_many(:collections).dependent(:nullify) } + it { is_expected.to have_many(:credits).dependent(:restrict_with_error) } + it { is_expected.to have_many(:display_ads).dependent(:destroy) } + it { is_expected.to have_many(:listings).dependent(:destroy) } + it { is_expected.to have_many(:notifications).dependent(:destroy) } it { is_expected.to have_many(:organization_memberships).dependent(:delete_all) } - it { is_expected.to have_many(:profile_pins) } - it { is_expected.to have_many(:sponsorships) } + it { is_expected.to have_many(:profile_pins).dependent(:destroy) } + it { is_expected.to have_many(:sponsorships).dependent(:destroy) } it { is_expected.to have_many(:unspent_credits).class_name("Credit") } it { is_expected.to have_many(:users).through(:organization_memberships) }