From ee9e0a8b7a3dbe6c8ba742f6649b0618b1bc0f06 Mon Sep 17 00:00:00 2001 From: Daria Mayorova Date: Fri, 16 Aug 2024 12:49:51 +0200 Subject: [PATCH] Refactor and move the task --- lib/tasks/database_cleanup.rake | 32 ------------------------------ lib/tasks/multitenant/tenants.rake | 31 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 32 deletions(-) delete mode 100644 lib/tasks/database_cleanup.rake diff --git a/lib/tasks/database_cleanup.rake b/lib/tasks/database_cleanup.rake deleted file mode 100644 index b2789dcf1e..0000000000 --- a/lib/tasks/database_cleanup.rake +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -namespace :database do - desc 'Check and remove orphaned objects' - task cleanup_orphans: :environment do - puts 'Checking and removing orphaned objects...' - - # Tables to exclude from orphaned objects check - excluded_tables = ['cms_templates'] - - provider_account_ids = Account.where(provider: true).pluck(:id) - - 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) - - orphaned_objects = model.where.not(tenant_id: provider_account_ids) - - if orphaned_objects.exists? - 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 #{model.table_name}." - end - end - - puts 'Orphaned objects check completed.' - end -end diff --git a/lib/tasks/multitenant/tenants.rake b/lib/tasks/multitenant/tenants.rake index bf9a4466e4..205d7c1680 100644 --- a/lib/tasks/multitenant/tenants.rake +++ b/lib/tasks/multitenant/tenants.rake @@ -68,6 +68,37 @@ namespace :multitenant do update_tenant_ids(proc { |object| object.account.tenant_id }, proc { account }, proc { tenant_id == nil }, args.to_hash.merge({ table_name: 'Alert' })) end + desc 'Check and remove orphaned objects (whose tenant is missing), pass "destroy" argument to delete' + task :cleanup_orphans, [:mode] => :environment do |_task, args| + puts 'Checking and removing orphaned objects...' + + destroy = args[:mode] == "destroy" + + puts "WARNING: the found orphan objects will be destroyed" if destroy + + provider_account_ids = Account.where(provider: true).pluck(:id) + [Account.master.id] + + ApplicationRecord.descendants.each do |model| + next unless model.table_exists? && model.column_names.include?('tenant_id') + + orphaned_objects = model.where.not(tenant_id: provider_account_ids) + + if orphaned_objects.exists? + puts "Found orphaned objects in #{model.table_name}:" + orphaned_objects.find_each { |obj| puts "- ID: #{obj.id}, Tenant ID: #{obj.tenant_id}" } + + if destroy + puts "Destroying orphan #{model.table_name}..." + orphaned_objects.in_batches(of: 100).destroy_all + end + else + puts "No orphaned objects found in #{model.table_name}." + end + end + + puts 'Orphaned objects check completed.' + end + def update_tenant_ids(tenant_id_block, association_block, condition, **args) query = args[:table_name].constantize.joining(&association_block).where.has(&condition) puts "------ Updating #{args[:table_name]} ------"