Story#108521790 Domain save it's changes history to paper_trail

This commit is contained in:
Vladimir Krylov 2015-12-04 17:43:49 +02:00
parent beee0a0ad8
commit 5b273f0df2
2 changed files with 45 additions and 21 deletions

View file

@ -13,7 +13,8 @@ module Legacy
x = self
{
name: SimpleIDN.to_unicode(x.object_registry.name.try(:strip)),
registrar_id: ::Registrar.find_by(legacy_id: x.object.try(:clid)).try(:id),
registrar_id: ::Registrar.find_by(legacy_id: x.object_history.try(:clid)).try(:id),
registrant_id: ::Contact.find_by(legacy_id: x.registrant).try(:id),
registered_at: x.object_registry.try(:crdate),
valid_from: x.object_registry.try(:crdate),
valid_to: x.exdate,
@ -27,7 +28,7 @@ module Legacy
creator_str: x.object_registry.try(:registrar).try(:name),
updator_str: x.object.try(:registrar).try(:name) ? x.object.try(:registrar).try(:name) : x.object_registry.try(:registrar).try(:name),
legacy_id: x.id,
legacy_registrar_id: x.object_registry.try(:crid),
legacy_registrar_id: x.object_history.try(:clid),
legacy_registrant_id: x.registrant,
statuses: Legacy::ObjectState.states_for_domain_at(x.id, time)
}
@ -52,6 +53,14 @@ module Legacy
where dh.id=#{domain_id} and dh.historyid = #{rec_id} ;}
find_by_sql(sql).first
end
# def last_history_action domain_id
# sql = %Q{SELECT dh.*, h.valid_from, h.valid_to
# from domain_history dh JOIN history h ON dh.historyid=h.id
# where dh.id=#{domain_id} order by dh.historyid desc limit 1;}
# find_by_sql(sql).first
# end
end
end
end

View file

@ -755,20 +755,7 @@ namespace :import do
end
desc 'Import history'
task history: :environment do
# {"id"=>83215, "name"=>"gssb-dsf0pf.ee", "registrar_id"=>17,
# "registered_at"=>Thu, 18 Sep 2014 10:17:13 EEST +03:00,
# "status"=>nil, "valid_from"=>Thu, 18 Sep 2014 10:17:13 EEST +03:00,
# "valid_to"=>Fri, 18 Sep 2015 00:00:00 EEST +03:00, "registrant_id"=>262841,
# "auth_info"=>"authinfopw", "created_at"=>Thu, 18 Sep 2014 10:17:13 EEST +03:00,
# "updated_at"=>Thu, 18 Sep 2014 10:17:13 EEST +03:00, "name_dirty"=>"gssb-dsf0pf.ee",
# "name_puny"=>"gssb-dsf0pf.ee", "period"=>1, "period_unit"=>"y", "creator_str"=>"Elkdata OÜ",
# "updator_str"=>"Elkdata OÜ", "legacy_id"=>778106, "legacy_registrar_id"=>13, "legacy_registrant_id"=>778104,
# "outzone_at"=>nil, "delete_at"=>nil, "registrant_verification_asked_at"=>nil,
# "registrant_verification_token"=>nil,
# "pending_json"=>{}, "force_delete_at"=>nil, "statuses"=>["ok"],
# "reserved"=>false, "status_notes"=>{}, "statuses_backup"=>[]}
task history_domains: :environment do
Legacy::DomainHistory.uniq.where(id: 294516).pluck(:id).each do |legacy_domain_id|
next if Domain.find_by(legacy_id: legacy_domain_id).versions.where(event: :create).any?
# add here to skip domains whith create history
@ -777,10 +764,13 @@ namespace :import do
# 2. add states
# compose hash of change time -> Object changes
last_changes = nil
domain = Domain.find_by(legacy_id: legacy_domain_id)
history = Legacy::ObjectState.changes_dates_for(legacy_domain_id)
p history.keys
p Legacy::DomainHistory.changes_dates_for(legacy_domain_id).keys
Legacy::DomainHistory.changes_dates_for(legacy_domain_id).each do |time, klasses|
dom_his = Legacy::DomainHistory.changes_dates_for(legacy_domain_id)
last_domain_action = dom_his.sort.last[1].last # need to identify if we delete
# merging changes together
dom_his.each do |time, klasses|
if history.has_key?(time)
history[time] = history[time] | klasses
else
@ -792,11 +782,36 @@ namespace :import do
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_domain_id, orig_history_klass[:id])
new_attrs = responder.get_current_domain_object(time, orig_history_klass[:param])
event = :update
event = :create if i == 0
responder = orig_history_klass[:klass].get_record_at(legacy_domain_id, orig_history_klass[:id])
p responder.get_current_domain_object(time, orig_history_klass[:param])
if orig_history_klass == last_domain_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?
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)
DomainVersion.create!(
item_type: domain.class,
item_id: domain.id,
event: event,
whodunnit: user.try(:id),
object: last_changes,
object_changes: changes,
created_at: time,
children: {}
)
last_changes = new_attrs
i += 1
end
end