mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 17:59:47 +02:00
Story#108521790 - import dnskey history
This commit is contained in:
parent
01a82a2960
commit
d41488472a
4 changed files with 80 additions and 0 deletions
|
@ -120,5 +120,9 @@ class Dnskey < ActiveRecord::Base
|
|||
def bin_to_hex(s)
|
||||
s.each_byte.map { |b| format('%02X', b) }.join
|
||||
end
|
||||
|
||||
def next_id
|
||||
self.connection.select_value("SELECT nextval('#{self.sequence_name}')")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
module Legacy
|
||||
class Dnskey < Db
|
||||
self.table_name = :dnskey
|
||||
self.primary_key = :id
|
||||
|
||||
belongs_to :object_registry, foreign_key: :id
|
||||
belongs_to :object, foreign_key: :id
|
||||
has_one :object_history, foreign_key: :historyid, primary_key: :historyid
|
||||
end
|
||||
end
|
||||
|
|
8
app/models/legacy/dnskey_history.rb
Normal file
8
app/models/legacy/dnskey_history.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
module Legacy
|
||||
class DnskeyHistory < Db
|
||||
self.table_name = :dnskey_history
|
||||
|
||||
belongs_to :object_registry, foreign_key: :id
|
||||
belongs_to :object, foreign_key: :id
|
||||
end
|
||||
end
|
|
@ -1,4 +1,12 @@
|
|||
namespace :import do
|
||||
desc 'Import all history'
|
||||
task history_all: :environment do
|
||||
Rake::Task['import:history_contacts'].invoke
|
||||
Rake::Task['import:history_dnskeys'].invoke
|
||||
Rake::Task['import:history_domains'].invoke
|
||||
end
|
||||
|
||||
|
||||
desc 'Import contact history'
|
||||
task history_contacts: :environment do
|
||||
Legacy::ContactHistory.uniq.pluck(:id).each do |legacy_contact_id|
|
||||
|
@ -70,6 +78,64 @@ namespace :import do
|
|||
end
|
||||
|
||||
|
||||
desc 'Import contact history'
|
||||
task history_dnskeys: :environment do
|
||||
Legacy::DnskeyHistory.uniq.pluck(:id).each do |legacy_dnskey_id|
|
||||
Dnskey.transaction do
|
||||
contact = Dnskey.find_by(legacy_id: legacy_dnskey_id)
|
||||
version_dns = DnskeytVersion.where("object->>'legacy_id' = '#{legacy_dnskey_id}'").select(:item_id).first
|
||||
contact ||= Dnskey.new(id: version_dns.item_id, legacy_id: legacy_dnskey_id) if version_dns
|
||||
contact ||= Dnskey.new(id: ::Contact.next_id, legacy_id: legacy_dnskey_id)
|
||||
next if contact.versions.where(event: :create).any?
|
||||
# add here to skip domains whith create history
|
||||
|
||||
last_changes = nil
|
||||
history = Legacy::ContactHistory.changes_dates_for(legacy_dnskey_id)
|
||||
last_contact_action = history.sort.last[1].last # need to identify if we delete
|
||||
|
||||
keys = history.keys.compact.sort
|
||||
i = 0
|
||||
keys.each_with_index do |time|
|
||||
history[time].each do |orig_history_klass|
|
||||
changes = {}
|
||||
responder = orig_history_klass[:klass].get_record_at(legacy_dnskey_id, orig_history_klass[:id])
|
||||
new_attrs = responder.get_current_contact_object(time, orig_history_klass[:param])
|
||||
new_attrs[:id] = contact.id
|
||||
|
||||
event = :update
|
||||
event = :create if i == 0
|
||||
if orig_history_klass == last_contact_action && responder.valid_to.present?
|
||||
event = :destroy
|
||||
new_attrs = {}
|
||||
end
|
||||
|
||||
new_attrs.each do |k, v|
|
||||
if (old_val = last_changes.to_h[k]) != v then changes[k] = [old_val, v] end
|
||||
end
|
||||
next if changes.blank? && event != :destroy
|
||||
obj_his = Legacy::ObjectHistory.find_by(historyid: responder.historyid)
|
||||
user = Registrar.find_by(legacy_id: obj_his.upid || obj_his.clid).try(:api_users).try(:first)
|
||||
|
||||
hash = {
|
||||
item_type: Dnskey.to_s,
|
||||
item_id: contact.id,
|
||||
event: event,
|
||||
whodunnit: user.try(:id),
|
||||
object: last_changes,
|
||||
object_changes: changes,
|
||||
created_at: time
|
||||
}
|
||||
DnskeyVersion.create!(hash)
|
||||
|
||||
last_changes = new_attrs
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
desc 'Import domain history'
|
||||
task history_domains: :environment do
|
||||
Domain.transaction do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue