[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 <mollylbs@gmail.com>

Co-authored-by: Molly Struve <mollylbs@gmail.com>
This commit is contained in:
rhymes 2020-08-24 19:35:02 +02:00 committed by GitHub
parent 682278ed75
commit 66b5cb513f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 121 additions and 16 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) }