diff --git a/app/models/legacy/domain_history.rb b/app/models/legacy/domain_history.rb index e5439655d..380af3907 100644 --- a/app/models/legacy/domain_history.rb +++ b/app/models/legacy/domain_history.rb @@ -9,11 +9,11 @@ module Legacy belongs_to :history, foreign_key: :historyid has_one :object_history, foreign_key: :historyid, primary_key: :historyid - def get_current_domain_object(change_param) + def get_current_domain_object(time, change_param) 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.try(:clid)).try(:id), registered_at: x.object_registry.try(:crdate), valid_from: x.object_registry.try(:crdate), valid_to: x.exdate, @@ -29,14 +29,10 @@ module Legacy legacy_id: x.id, legacy_registrar_id: x.object_registry.try(:crid), legacy_registrant_id: x.registrant, - statuses: x.states + statuses: Legacy::ObjectState.states_for_domain_at(x.id, time) } 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 diff --git a/app/models/legacy/object_state.rb b/app/models/legacy/object_state.rb index 4197276b9..f6f89da0b 100644 --- a/app/models/legacy/object_state.rb +++ b/app/models/legacy/object_state.rb @@ -80,12 +80,12 @@ module Legacy map[state_id] end - def get_current_domain_object(param) - p "not implemented #{__method__}" - end + def get_current_domain_object(time, param) + d_his = Legacy::DomainHistory.get_record_at(object_id, historyid) + hash = d_his.get_current_domain_object(time, param) + hash[:statuses] = Legacy::ObjectState.states_for_domain_at(object_id, time + 1) - def get_current_changes(param) - p "not implemented #{__method__}" + hash end class << self @@ -106,7 +106,7 @@ module Legacy end def get_record_at domain_id, rec_id - sql = %Q{SELECT distinct t_2.id, state.* + sql = %Q{SELECT distinct t_2.historyid, 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)) @@ -114,6 +114,21 @@ module Legacy WHERE state.object_id=#{domain_id} AND state.id = #{rec_id};} find_by_sql(sql).first end + + def states_for_domain_at(domain_id, time) + sql = %Q{SELECT 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 (valid_from is null or valid_from <= '#{time.to_s}'::TIMESTAMPTZ) + AND (valid_to is null or valid_to >= '#{time}'::TIMESTAMPTZ) + } + arr = find_by_sql(sql).uniq + arr.map!(&:name) if arr.any? + arr.present? ? arr : [DomainStatus::OK] + end end end end diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 63130dde9..f1deeb9a1 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -770,11 +770,13 @@ namespace :import do # "reserved"=>false, "status_notes"=>{}, "statuses_backup"=>[]} 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 # 1. add domain changes # 2. add states # compose hash of change time -> Object changes + last_changes = nil history = Legacy::ObjectState.changes_dates_for(legacy_domain_id) p history.keys p Legacy::DomainHistory.changes_dates_for(legacy_domain_id).keys @@ -793,8 +795,7 @@ namespace :import do 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]) + p responder.get_current_domain_object(time, orig_history_klass[:param]) i += 1 end