mirror of
https://github.com/internetee/registry.git
synced 2025-05-18 02:09:39 +02:00
Story#104525318 - in domain history import match change records in historical order
This commit is contained in:
parent
df6e254cc6
commit
3e8309f818
3 changed files with 106 additions and 30 deletions
|
@ -5,5 +5,33 @@ module Legacy
|
||||||
belongs_to :domain, foreign_key: :id
|
belongs_to :domain, foreign_key: :id
|
||||||
belongs_to :history, foreign_key: :historyid
|
belongs_to :history, foreign_key: :historyid
|
||||||
has_one :object_history, foreign_key: :historyid, primary_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
|
||||||
end
|
end
|
||||||
|
|
|
@ -80,18 +80,40 @@ module Legacy
|
||||||
map[state_id]
|
map[state_id]
|
||||||
end
|
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
|
def changes_dates_for domain_id
|
||||||
sql = %Q{SELECT t_2.id, state.*
|
sql = %Q{SELECT distinct t_2.id, state.id state_dot_id, state.*,
|
||||||
FROM object_history t_2
|
extract(epoch from valid_from) valid_from_unix, extract(epoch from valid_to) valid_to_unix
|
||||||
JOIN object_state state ON (t_2.historyid >= state.ohid_from
|
FROM object_history t_2
|
||||||
AND (t_2.historyid <= state.ohid_to OR state.ohid_to IS NULL))
|
JOIN object_state state ON (t_2.historyid >= state.ohid_from
|
||||||
AND t_2.id = state.object_id
|
AND (t_2.historyid <= state.ohid_to OR state.ohid_to IS NULL))
|
||||||
WHERE state.object_id=#{domain_id}
|
AND t_2.id = state.object_id
|
||||||
ORDER BY t_2.historyid;}
|
WHERE state.object_id=#{domain_id};}
|
||||||
find_by_sql(sql).map{|e| e.attributes.values_at("valid_from", "valid_to") }.flatten.each_with_object({}){|e,h|h[e] = [self]}
|
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
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -769,34 +769,60 @@ namespace :import do
|
||||||
# "pending_json"=>{}, "force_delete_at"=>nil, "statuses"=>["ok"],
|
# "pending_json"=>{}, "force_delete_at"=>nil, "statuses"=>["ok"],
|
||||||
# "reserved"=>false, "status_notes"=>{}, "statuses_backup"=>[]}
|
# "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
|
# 1. add domain changes
|
||||||
# 2. add states
|
# 2. add states
|
||||||
# compose hash of change time -> Object changes
|
# 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|
|
p time
|
||||||
next if domain.versions.where(action: :create).any?
|
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
|
i += 1
|
||||||
history.each_with_index do |his, i|
|
end
|
||||||
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
|
||||||
|
|
||||||
|
# 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue