From 3e8309f81824d80965429c94f17d4f7e3bae12b9 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 24 Nov 2015 16:55:17 +0200 Subject: [PATCH] Story#104525318 - in domain history import match change records in historical order --- app/models/legacy/domain_history.rb | 28 ++++++++++++ app/models/legacy/object_state.rb | 42 +++++++++++++----- lib/tasks/import.rake | 66 ++++++++++++++++++++--------- 3 files changed, 106 insertions(+), 30 deletions(-) diff --git a/app/models/legacy/domain_history.rb b/app/models/legacy/domain_history.rb index 08ab52836..d45aa5f92 100644 --- a/app/models/legacy/domain_history.rb +++ b/app/models/legacy/domain_history.rb @@ -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 diff --git a/app/models/legacy/object_state.rb b/app/models/legacy/object_state.rb index 9c7938c51..4197276b9 100644 --- a/app/models/legacy/object_state.rb +++ b/app/models/legacy/object_state.rb @@ -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 diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 0e790bf20..abe3c2918 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -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