Skip to content

Commit

Permalink
delete clients and transfer dois and prefixes for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 25, 2018
1 parent a8201f7 commit 3d47162
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/serializers/doi_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DoiSerializer
has_many :media

attribute :xml, if: Proc.new { |object, params| params && params[:detail] } do |object|
object.xml_encoded || object.current_metadata.present? && object.current_metadata.xml.to_s.start_with?('<?xml version=') ? Base64.strict_encode64(object.current_metadata.xml.force_encoding("UTF-8")) : nil
object.xml_encoded || (object.current_metadata.present? && object.current_metadata.xml.to_s.start_with?('<?xml version=') ? Base64.strict_encode64(object.current_metadata.xml.force_encoding("UTF-8")) : nil)
end

attribute :doi do |object|
Expand Down
110 changes: 110 additions & 0 deletions lib/tasks/client.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
namespace :client do
desc 'Delete client transferred to other DOI registration agency'
task :delete => :environment do
if ENV['CLIENT_ID'].nil?
puts "ENV['CLIENT_ID'] is required."
exit
end

client = Client.where(deleted_at: nil).where(symbol: ENV['CLIENT_ID']).first
if client.nil?
puts "Client not found for client ID #{ENV['CLIENT_ID']}."
exit
end

# These prefixes are used by multiple clients
prefixes_to_keep = %w(10.5072 10.4124 10.4225 10.4226 10.4227)

# delete all associated prefixes and DOIs
prefixes = client.prefixes.where.not('prefix IN (?)', prefixes_to_keep).pluck(:prefix)
prefix_ids = client.prefixes.where.not('prefix IN (?)', prefixes_to_keep).pluck(:id)

response = client.client_prefixes.destroy_all
puts "#{response.count} client prefixes deleted."

if prefix_ids.present?
response = ProviderPrefix.where('prefixes IN (?)', prefix_ids).destroy_all
puts "#{response.count} provider prefixes deleted."
end

if prefixes.present?
response = Prefix.where('prefix IN (?)', prefixes).destroy_all
puts "Prefixes #{prefixes.join(" and ")} deleted."
end

# delete DOIs in batches
puts "#{client.dois.length} DOIs will be deleted."
client.dois.find_each do |doi|
doi.destroy
puts "DOI #{doi.doi} deleted."
end

if client.update_attributes(is_active: nil, deleted_at: Time.zone.now)
client.send_delete_email unless Rails.env.test?
puts "Client with client ID #{ENV['CLIENT_ID']} deleted."
else
puts client.errors.inspect
end
end

desc 'Transfer client'
task :transfer => :environment do
if ENV['CLIENT_ID'].nil?
puts "ENV['CLIENT_ID'] is required."
exit
end

client = Client.where(deleted_at: nil).where(symbol: ENV['CLIENT_ID']).first
if client.nil?
puts "Client not found for client ID #{ENV['CLIENT_ID']}."
exit
end

if ENV['TARGET_ID'].nil?
puts "ENV['TARGET_ID'] is required."
exit
end

target = Client.where(deleted_at: nil).where(symbol: ENV['TARGET_ID']).first
if target.nil?
puts "Client not found for target ID #{ENV['TARGET_ID']}."
exit
end

# These prefixes are used by multiple clients
prefixes_to_keep = %w(10.5072 10.4124 10.4225 10.4226 10.4227)

# delete all associated prefixes
prefixes = client.prefixes.where.not('prefix IN (?)', prefixes_to_keep).pluck(:prefix)
prefix_ids = client.prefixes.where.not('prefix IN (?)', prefixes_to_keep).pluck(:id)

response = client.client_prefixes.destroy_all
puts "#{response.count} client prefixes deleted."

if prefix_ids.present?
response = ProviderPrefix.where('prefixes IN (?)', prefix_ids).destroy_all
puts "#{response.count} provider prefixes deleted."
end

# update client for DOIs in batches
puts "#{client.dois.length} DOIs will be transferred."
client.dois.find_each do |doi|
doi.update_attributes(datacentre: target.id)
puts "DOI #{doi.doi} transferred to client #{target.symbol}."
end

if client.update_attributes(is_active: nil, deleted_at: Time.zone.now)
client.send_delete_email unless Rails.env.test?
puts "Client with client ID #{ENV['CLIENT_ID']} deleted."
else
puts client.errors.inspect
end

prefixes.each do |prefix|
provider_prefix = ProviderPrefix.create(provider: target.allocator.symbol, prefix: prefix)
puts "Provider prefix for provider #{target.allocator.symbol} and prefix #{prefix} created."
client_prefix = ClientPrefix.create(client: target.symbol, prefix: prefix, provider_prefix: provider_prefix.id)
puts "Client prefix for client #{target.symbol} and prefix #{prefix} created."
end
end
end

0 comments on commit 3d47162

Please sign in to comment.