Skip to content

Instantly share code, notes, and snippets.

@royalgiant
Last active September 9, 2020 20:04
Show Gist options
  • Save royalgiant/a3bb7063c299fcb035bbb6c2c4d11cd9 to your computer and use it in GitHub Desktop.
Save royalgiant/a3bb7063c299fcb035bbb6c2c4d11cd9 to your computer and use it in GitHub Desktop.
STC-36947 - Remove ServiceConfigurationSport and StudentServiceCategorySport with a sport ID that no longer exists
# TO USE THIS SCRIPT
# Copy and Paste into a new file in campus/script/remove_sport_dependent_services.rb
# DRY RUN (test): ruby script/remove_sport_dependent_services.rb
# RUN for real: ruby script/remove_sport_dependent_services.rb --dry-run=false
class RemoveSportDependentServices
attr_reader :dry_run
def initialize(options = {})
@dry_run = true
@dry_run = options.fetch(:dry_run)
end
def execute
Rails.logger.level = :debug
ActiveRecord::Base.logger.level = 1
ss_cat_sport_columns = StudentServiceCategorySport.columns.map(&:name)
service_config_sport_columns = ServiceConfigurationSport.columns.map(&:name)
tenants = Tenant.all
tenants.each_with_context do |t|
ss_cat_sports = StudentServiceCategorySport.where(["sport_id NOT IN (?) AND db_id = (?)", Sport.pluck("id"), t.db_id])
service_config_sports = ServiceConfigurationSport.where(["sport_id NOT IN (?) AND db_id = (?)", Sport.pluck("id"), t.db_id])
if @dry_run
puts "Tenant Subdomain: #{t.subdomain} and Tenant ID: #{t.id}"
puts "The Orphaned StudentServiceCategorySport to be deleted:"
puts ss_cat_sports.present? ? ss_cat_sports.inspect : "None."
puts "The Orphaned ServiceConfigurationSport to be deleted:"
puts service_config_sports.present? ? service_config_sports.inspect : "None."
else
if ss_cat_sports.present? || service_config_sports.present?
puts "Tenant Subdomain: #{t.subdomain} and Tenant ID: #{t.id}"
end
#create back-up CSV file for Student Service Category Sport
if ss_cat_sports.present?
puts "The Orphaned StudentServiceCategorySport to be deleted:"
puts ss_cat_sports.inspect
CSV.open("#{t.subdomain}_student_service_category_sport_backup.csv", "wb") do |csv|
csv << ss_cat_sport_columns.map(&:titleize)
ss_cat_sports.pluck(*ss_cat_sport_columns).each do |ss_cat_sport|
csv << ss_cat_sport
end
end
ss_cat_sports.destroy_all
end
#create back-up CSV file for Service Configuration Sport
if service_config_sports.present?
puts "The Orphaned ServiceConfigurationSport to be deleted:"
puts service_config_sports.inspect
CSV.open("#{t.subdomain}_service_configuration_backup.csv", "wb") do |csv|
csv << service_config_sport_columns.map(&:titleize)
service_config_sports.pluck(*service_config_sport_columns).each do |service_config_sport|
csv << service_config_sport
end
end
service_config_sports.destroy_all
end
end
end
end
end
$stderr.sync = true
require 'optparse'
usage = %Q{
Usage:
Dry Run:
ruby script/remote_sport_dependent_services.rb
Execute:
ruby script/remote_sport_dependent_services.rb --dry-run=false
Remove ServiceConfigurationSport and StudentServiceCategorySport with a sport ID that no longer exists.
Options:
--dry-run Should execute
-h, --help Shows usage information
}
dry_run = true
ARGV.options do |opts|
opts.on("--dry-run=val") { |val|
dry_run = false if val == 'false'
}
opts.on_tail("-h", "--help") { warn usage; exit 1 }
opts.parse!
end
require 'bundler/setup'
require File.expand_path('../../config/environment', __FILE__)
RemoveSportDependentServices.new(dry_run: dry_run).execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment