@@ -6,72 +6,27 @@ namespace :database do
66 puts 'Checking and removing orphaned objects...'
77
88 # Tables to exclude from orphaned objects check
9- excluded_tables = [ 'accounts' , 'audits' , 'categories' , 'category_types' , 'cms_templates' , 'legal_term_acceptances' , 'legal_term_bindings' ,
10- 'legal_term_versions' , 'proxy_logs' , 'schema_migrations' , 'service_cubert_infos' , 'slugs' , 'taggings' , 'tags' ]
9+ excluded_tables = [ 'cms_templates' ]
1110
12- ids = Account . where ( provider : true ) . pluck ( :id )
13- # Iterate over tables with tenant_id field
14- tables = ActiveRecord ::Base . connection . tables . select { |t | ActiveRecord ::Base . connection . column_exists? ( t , 'tenant_id' ) }
11+ provider_account_ids = Account . where ( provider : true ) . pluck ( :id )
1512
16- tables . each do |table |
17- next if excluded_tables . include? ( table )
13+ ActiveRecord ::Base . descendants . each do |model |
14+ next unless model . table_exists? && model . column_names . include? ( 'tenant_id' )
15+ next if excluded_tables . include? ( model . table_name )
1816
19- class_name = convert_table_to_class ( table )
20- orphaned_objects = class_name . where . not ( tenant_id : ids )
17+ orphaned_objects = model . where . not ( tenant_id : provider_account_ids )
2118
2219 if orphaned_objects . exists?
23- puts "Found orphaned objects in #{ table } :"
20+ puts "Found orphaned objects in #{ model . table_name } :"
2421 orphaned_objects . each { |obj | puts "- ID: #{ obj . id } , Tenant ID: #{ obj . tenant_id } " }
2522
2623 # Uncomment the line below if you want to delete orphaned objects
2724 # orphaned_objects.destroy_all
2825 else
29- puts "No orphaned objects found in #{ table } ."
26+ puts "No orphaned objects found in #{ model . table_name } ."
3027 end
3128 end
32- puts 'Orphaned objects check completed.'
33- end
34-
35- private
3629
37- def convert_table_to_class ( table )
38- case table
39- when 'api_docs_services'
40- 'ApiDocs::Service' . constantize
41- when 'billing_strategies'
42- 'Finance::BillingStrategy' . constantize
43- when 'cms_files'
44- 'CMS::File' . constantize
45- when 'cms_group_sections'
46- 'CMS::GroupSection' . constantize
47- when 'cms_groups'
48- 'CMS::Group' . constantize
49- when 'cms_permissions'
50- 'CMS::Permission' . constantize
51- when 'cms_redirects'
52- 'CMS::Redirect' . constantize
53- when 'cms_sections'
54- 'CMS::Section' . constantize
55- when 'cms_templates_versions'
56- 'CMS::Template::Version' . constantize
57- when 'cms_legal_term'
58- 'CMS::LegalTerm' . constantize
59- when 'configuration_values'
60- 'Configuration::Value' . constantize
61- when 'event_store_events'
62- 'EventStore::Event' . constantize
63- when 'legal_terms'
64- 'CMS::LegalTerm' . constantize
65- when 'mail_dispatch_rules'
66- 'MailDispatchRule' . constantize
67- when 'notification_preferences'
68- 'NotificationPreferences' . constantize
69- when 'provider_constraints'
70- 'ProviderConstraints' . constantize
71- when 'settings'
72- 'Settings' . constantize
73- else
74- table . classify . safe_constantize || table . singularize . camelize . constantize
75- end
30+ puts 'Orphaned objects check completed.'
7631 end
7732end
0 commit comments