From 8e47c6039940c70feefee81b0163c71a76442a59 Mon Sep 17 00:00:00 2001 From: Nidhi Soni Date: Mon, 8 Apr 2024 11:31:27 +0530 Subject: [PATCH] updated rake task for all tables check --- lib/tasks/database_cleanup.rake | 63 +++++---------------------------- 1 file changed, 9 insertions(+), 54 deletions(-) diff --git a/lib/tasks/database_cleanup.rake b/lib/tasks/database_cleanup.rake index 59706fb4b3..b2789dcf1e 100644 --- a/lib/tasks/database_cleanup.rake +++ b/lib/tasks/database_cleanup.rake @@ -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