Skip to content

Commit

Permalink
updated rake task for all tables check
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-soni1104 committed Apr 8, 2024
1 parent 740c3f1 commit 89a470f
Showing 1 changed file with 9 additions and 54 deletions.
63 changes: 9 additions & 54 deletions lib/tasks/database_cleanup.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,27 @@ namespace :database do
puts 'Checking and removing orphaned objects...'

# Tables to exclude from orphaned objects check
excluded_tables = ['accounts', 'audits', 'categories', 'category_types', 'cms_templates', 'legal_term_acceptances', 'legal_term_bindings',
'legal_term_versions', 'proxy_logs', 'schema_migrations', 'service_cubert_infos', 'slugs', 'taggings', 'tags']
excluded_tables = ['cms_templates']

ids = Account.where(provider: true).pluck(:id)
# Iterate over tables with tenant_id field
tables = ActiveRecord::Base.connection.tables.select { |t| ActiveRecord::Base.connection.column_exists?(t, 'tenant_id') }
provider_account_ids = Account.where(provider: true).pluck(:id)

tables.each do |table|
next if excluded_tables.include?(table)
ActiveRecord::Base.descendants.each do |model|
next unless model.table_exists? && model.column_names.include?('tenant_id')
next if excluded_tables.include?(model.table_name)

class_name = convert_table_to_class(table)
orphaned_objects = class_name.where.not(tenant_id: ids)
orphaned_objects = model.where.not(tenant_id: provider_account_ids)

if orphaned_objects.exists?
puts "Found orphaned objects in #{table}:"
puts "Found orphaned objects in #{model.table_name}:"
orphaned_objects.each { |obj| puts "- ID: #{obj.id}, Tenant ID: #{obj.tenant_id}" }

# Uncomment the line below if you want to delete orphaned objects
# orphaned_objects.destroy_all
else
puts "No orphaned objects found in #{table}."
puts "No orphaned objects found in #{model.table_name}."
end
end
puts 'Orphaned objects check completed.'
end

private

def convert_table_to_class(table)
case table
when 'api_docs_services'
'ApiDocs::Service'.constantize
when 'billing_strategies'
'Finance::BillingStrategy'.constantize
when 'cms_files'
'CMS::File'.constantize
when 'cms_group_sections'
'CMS::GroupSection'.constantize
when 'cms_groups'
'CMS::Group'.constantize
when 'cms_permissions'
'CMS::Permission'.constantize
when 'cms_redirects'
'CMS::Redirect'.constantize
when 'cms_sections'
'CMS::Section'.constantize
when 'cms_templates_versions'
'CMS::Template::Version'.constantize
when 'cms_legal_term'
'CMS::LegalTerm'.constantize
when 'configuration_values'
'Configuration::Value'.constantize
when 'event_store_events'
'EventStore::Event'.constantize
when 'legal_terms'
'CMS::LegalTerm'.constantize
when 'mail_dispatch_rules'
'MailDispatchRule'.constantize
when 'notification_preferences'
'NotificationPreferences'.constantize
when 'provider_constraints'
'ProviderConstraints'.constantize
when 'settings'
'Settings'.constantize
else
table.classify.safe_constantize || table.singularize.camelize.constantize
end
puts 'Orphaned objects check completed.'
end
end

0 comments on commit 89a470f

Please sign in to comment.