Story#116761157 - update logic of destroy_orphans to include 6 months delay

This commit is contained in:
Vladimir Krylov 2016-05-27 14:18:45 +03:00
parent f2c96ba145
commit a7be49203c
3 changed files with 23 additions and 10 deletions

View file

@ -35,7 +35,7 @@ module Repp
error! I18n.t('errors.messages.epp_authorization_error'), 401 unless domain.auth_info.eql? request.headers['Auth-Code']
contact_repp_json = proc{|contact|
contact.attributes.slice("code", "name", "ident", "ident_type", "ident_country_code", "phone", "email", "street", "city", "zip","country_code", "statuses")
contact.as_json.slice("code", "name", "ident", "ident_type", "ident_country_code", "phone", "email", "street", "city", "zip","country_code", "statuses")
}
@response = {

View file

@ -39,7 +39,6 @@ class Contact < ActiveRecord::Base
validate :val_country_code
after_initialize do
self.statuses = [] if statuses.nil?
self.status_notes = {} if status_notes.nil?
self.ident_updated_at = Time.zone.now if new_record? && ident_updated_at.blank?
end
@ -173,20 +172,29 @@ class Contact < ActiveRecord::Base
')
end
# To leave only new ones we need to check
# if contact was at any time used in domain.
# This can be checked by domain history.
# This can be checked by saved relations in children attribute
def destroy_orphans
STDOUT << "#{Time.zone.now.utc} - Destroying orphaned contacts\n" unless Rails.env.test?
orphans = find_orphans
unless Rails.env.test?
orphans.each do |m|
STDOUT << "#{Time.zone.now.utc} Contact.destroy_orphans: ##{m.id} (#{m.name})\n"
counter = Counter.new
find_orphans.find_each do |contact|
ver_scope = []
%w(admin_contacts tech_contacts registrant).each do |type|
ver_scope << "(children->'#{type}')::jsonb <@ json_build_array(#{contact.id})::jsonb"
end
next if DomainVersion.where("created_at > ?", Time.now - 6.months).where(ver_scope.join(" OR ")).any?
next if contact.domains_present?
# contact.destroy
counter.next
STDOUT << "#{Time.zone.now.utc} Contact.destroy_orphans: ##{contact.id} (#{contact.name})\n"
p "#{Time.zone.now.utc} Contact.destroy_orphans: ##{contact.id} (#{contact.name})\n"
end
count = orphans.destroy_all.count
STDOUT << "#{Time.zone.now.utc} - Successfully destroyed #{count} orphaned contacts\n" unless Rails.env.test?
STDOUT << "#{Time.zone.now.utc} - Successfully destroyed #{counter} orphaned contacts\n" unless Rails.env.test?
end
def privs

View file

@ -0,0 +1,5 @@
class ChangeContactStatusesDefault < ActiveRecord::Migration
def change
change_column_default :contacts, :statuses, []
end
end