Story#104525318 - in domain history import match change records in historical order

This commit is contained in:
Vladimir Krylov 2015-11-24 16:55:17 +02:00
parent df6e254cc6
commit 3e8309f818
3 changed files with 106 additions and 30 deletions

View file

@ -5,5 +5,33 @@ module Legacy
belongs_to :domain, foreign_key: :id
belongs_to :history, foreign_key: :historyid
has_one :object_history, foreign_key: :historyid, primary_key: :historyid
def get_current_domain_object(param)
p "not implemented #{__method__}"
end
def get_current_changes(param)
p "not implemented #{__method__}"
end
class << self
def changes_dates_for domain_id
sql = %Q{SELECT dh.*, valid_from--, extract(epoch from h.valid_from) valid_from_unix, extract(epoch from h.valid_to) valid_to_unix
FROM domain_history dh JOIN history h ON dh.historyid=h.id where dh.id=#{domain_id};}
# find_by_sql(sql).map{|e| e.attributes.values_at("valid_from") }.flatten.each_with_object({}){|e,h|h[e.try(:to_f)] = [self]}
hash = {}
find_by_sql(sql).each do |rec|
hash[rec.valid_from.try(:to_time)] = [{id: rec.historyid, klass: self, param: :valid_from}] if rec.valid_from
end
hash
end
def get_record_at domain_id, rec_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} and dh.historyid = #{rec_id} ;}
find_by_sql(sql).first
end
end
end
end

View file

@ -80,18 +80,40 @@ module Legacy
map[state_id]
end
def get_current_domain_object(param)
p "not implemented #{__method__}"
end
class << self
def get_current_changes(param)
p "not implemented #{__method__}"
end
class << self
def changes_dates_for domain_id
sql = %Q{SELECT t_2.id, state.*
FROM object_history t_2
JOIN object_state state ON (t_2.historyid >= state.ohid_from
AND (t_2.historyid <= state.ohid_to OR state.ohid_to IS NULL))
AND t_2.id = state.object_id
WHERE state.object_id=#{domain_id}
ORDER BY t_2.historyid;}
find_by_sql(sql).map{|e| e.attributes.values_at("valid_from", "valid_to") }.flatten.each_with_object({}){|e,h|h[e] = [self]}
sql = %Q{SELECT distinct t_2.id, state.id state_dot_id, state.*,
extract(epoch from valid_from) valid_from_unix, extract(epoch from valid_to) valid_to_unix
FROM object_history t_2
JOIN object_state state ON (t_2.historyid >= state.ohid_from
AND (t_2.historyid <= state.ohid_to OR state.ohid_to IS NULL))
AND t_2.id = state.object_id
WHERE state.object_id=#{domain_id};}
hash = {}
find_by_sql(sql).each do |rec|
hash[rec.valid_from.try(:to_time)] = [{id: rec.state_dot_id, klass: self, param: :valid_from}] if rec.valid_from
hash[rec.valid_to.try(:to_time)] = [{id: rec.state_dot_id, klass: self, param: :valid_to}] if rec.valid_to
end
hash
end
end
def get_record_at domain_id, rec_id
sql = %Q{SELECT distinct t_2.id, state.*
FROM object_history t_2
JOIN object_state state ON (t_2.historyid >= state.ohid_from
AND (t_2.historyid <= state.ohid_to OR state.ohid_to IS NULL))
AND t_2.id = state.object_id
WHERE state.object_id=#{domain_id} AND state.id = #{rec_id};}
find_by_sql(sql).first
end
end
end
end

View file

@ -769,34 +769,60 @@ namespace :import do
# "pending_json"=>{}, "force_delete_at"=>nil, "statuses"=>["ok"],
# "reserved"=>false, "status_notes"=>{}, "statuses_backup"=>[]}
Legacy::DomainHistory.uniq.pluck(:id).each do |legacy_domain_id|
Legacy::DomainHistory.uniq.where(id: 294516).pluck(:id).each do |legacy_domain_id|
# add here to skip domains whith create history
# 1. add domain changes
# 2. add states
# compose hash of change time -> Object changes
history = Legacy::ObjectState.changes_dates_for(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|
if history.has_key?(time)
history[time] = history[time] | klasses
else
history[time] = klasses
end
end
end
keys = history.keys.compact.sort
i = 0
keys.each_with_index do |time|
Domain.where.not(legacy_id: nil).find_each do |domain|
next if domain.versions.where(action: :create).any?
p time
history[time].each do |orig_history_klass|
event = :update
event = :create if i == 0
responder = orig_history_klass[:klass].get_record_at(legacy_domain_id, orig_history_klass[:id])
responder.get_current_domain_object(orig_history_klass[:param])
responder.get_current_changes(orig_history_klass[:param])
history = Legacy::DomainHistory.where(id: domain.legacy_id).order("valid_from ASC").to_a
history.each_with_index do |his, i|
event = :update
event = :create if i == 0
event = :destroy if i + 1 == history.size && his.history.valid_to.present?
{
item_type: domain.class,
item_id: domain.id,
event: event,
whodunnit: Registrar.find_by(legacy_id: his.object_history.upid || his.object_history.clid),
object: {},
object_changes: {"id" => [nil, 1111] },
created_at: his.object_history.try(:update),
}
i += 1
end
end
end
# Domain.where.not(legacy_id: nil).find_each do |domain|
# next if domain.versions.where(action: :create).any?
#
# history = Legacy::DomainHistory.where(id: domain.legacy_id).order("valid_from ASC").to_a
# history.each_with_index do |his, i|
# event = :update
# event = :create if i == 0
# event = :destroy if i + 1 == history.size && his.history.valid_to.present?
#
# {
# item_type: domain.class,
# item_id: domain.id,
# event: event,
# whodunnit: Registrar.find_by(legacy_id: his.object_history.upid || his.object_history.clid),
# object: {},
# object_changes: {"id" => [nil, 1111] },
# created_at: his.object_history.try(:update),
# }
# end
# end
end
end