-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rake tasks for db cleanup and pg dump by tenant
- Loading branch information
1 parent
80d4502
commit a4aa654
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace :search do | ||
desc 'Delete old unsaved searches for multiple tenants in batches' | ||
task delete_old_searches: :environment do | ||
days_old = ENV['DAYS_OLD'].to_i | ||
batch_size = ENV['BATCH_SIZE'].to_i | ||
tenant_ids = Account.pluck(:id) | ||
|
||
if days_old <= 0 || batch_size <= 0 | ||
puts 'Valid DAYS_OLD and BATCH_SIZE are required.' | ||
exit 1 | ||
end | ||
|
||
tenant_ids.each do |tenant_id| | ||
account = Account.find(tenant_id) | ||
next if account.nil? | ||
|
||
# Switch tenant context | ||
switch!(account) | ||
|
||
# Run the delete_old_searches method in batches | ||
loop do | ||
old_searches = Search.where(['created_at < ? AND user_id IS NULL', Time.zone.today - days_old]) | ||
.limit(batch_size) | ||
|
||
break if old_searches.empty? | ||
|
||
old_searches.delete_all | ||
puts "Deleted #{old_searches.size} searches in batch for tenant #{tenant_id}." | ||
sleep(1) # Optional: add a short delay to reduce load | ||
end | ||
|
||
# Perform VACUUM FULL on the searches table in the specified schema | ||
schema_name = account.tenant | ||
ActiveRecord::Base.connection.execute("VACUUM FULL \"#{schema_name}\".searches;") | ||
puts "Performed VACUUM FULL on searches table for tenant #{tenant_id}." | ||
|
||
puts "Completed deletion of old searches older than #{days_old} days for tenant #{tenant_id}." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace :db do | ||
desc 'Dump each tenant schema to a separate file in the root directory after checking schema size' | ||
task dump_schemas: :environment do | ||
# Fetch database configuration from Rails environment | ||
db_config = Rails.configuration.database_configuration[Rails.env] | ||
|
||
pg_password = db_config['password'] | ||
pg_user = db_config['username'] | ||
pg_host = db_config['host'] | ||
pg_port = db_config['port'] || '5432' | ||
db_name = db_config['database'] | ||
|
||
# Set the PGPASSWORD environment variable | ||
ENV['PGPASSWORD'] = pg_password | ||
|
||
# Print the values for debugging | ||
puts "PGPASSWORD: #{ENV['PGPASSWORD']}" | ||
puts "PGUSER: #{pg_user}" | ||
puts "PGHOST: #{pg_host}" | ||
puts "PGPORT: #{pg_port}" | ||
puts "DB_NAME: #{db_name}" | ||
|
||
# Iterate over each account | ||
Account.find_each do |account| | ||
schema_name = account.tenant | ||
tenant_name = account.name | ||
tenant_id = account.id | ||
next if schema_name.blank? # Skip if the tenant is blank | ||
|
||
# Check the size of the schema | ||
result = ActiveRecord::Base.connection.execute(" | ||
SELECT pg_size_pretty(sum(pg_total_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename)))) AS total_size | ||
FROM pg_tables | ||
WHERE schemaname = '#{schema_name}'; | ||
") | ||
|
||
# Get the size from the result | ||
schema_size = result[0]['total_size'] | ||
puts "tenant_id #{tenant_id}, tenant_name: #{tenant_name}, schema_name: #{schema_name} schema_size: #{schema_size}" | ||
|
||
# Define the output file path | ||
dump_file = "/app/samvera/hyrax-webapp/tmp/#{schema_name}_dump.sql" | ||
|
||
# Construct the pg_dump command | ||
command = "pg_dump -U #{pg_user} -h #{pg_host} -p #{pg_port} -d #{db_name} --schema='#{schema_name}' -f #{dump_file}" | ||
|
||
# Run the command | ||
puts "Dumping schema #{schema_name} to #{dump_file}" | ||
system(command) | ||
|
||
# Check if the command was successful | ||
if $?.exitstatus == 0 | ||
puts "Successfully dumped schema #{schema_name}" | ||
else | ||
puts "Error dumping schema #{schema_name}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters