From e11c1b5d20a7bff4d8be035be5d76b73b6e3e591 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 17 Nov 2015 12:17:26 +0200 Subject: [PATCH] Prepare history import --- app/models/legacy/domain.rb | 1 + app/models/legacy/domain_history.rb | 2 ++ app/models/legacy/history.rb | 5 +++++ lib/tasks/import.rake | 24 ++++++++++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 app/models/legacy/history.rb diff --git a/app/models/legacy/domain.rb b/app/models/legacy/domain.rb index d0a05d178..64506b1ef 100644 --- a/app/models/legacy/domain.rb +++ b/app/models/legacy/domain.rb @@ -12,5 +12,6 @@ module Legacy has_many :domain_contact_maps, foreign_key: :domainid has_many :nsset_contact_maps, foreign_key: :nssetid, primary_key: :nsset has_many :domain_histories, foreign_key: :id + alias_method :history, :domain_histories end end diff --git a/app/models/legacy/domain_history.rb b/app/models/legacy/domain_history.rb index 234585ac3..08ab52836 100644 --- a/app/models/legacy/domain_history.rb +++ b/app/models/legacy/domain_history.rb @@ -3,5 +3,7 @@ module Legacy self.table_name = :domain_history belongs_to :domain, foreign_key: :id + belongs_to :history, foreign_key: :historyid + has_one :object_history, foreign_key: :historyid, primary_key: :historyid end end diff --git a/app/models/legacy/history.rb b/app/models/legacy/history.rb new file mode 100644 index 000000000..98ef464b0 --- /dev/null +++ b/app/models/legacy/history.rb @@ -0,0 +1,5 @@ +module Legacy + class History < Db + self.table_name = :history + end +end \ No newline at end of file diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 28786d278..519a3279c 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -774,6 +774,30 @@ namespace :import do puts "-----> Imported zones in #{(Time.zone.now.to_f - start).round(2)} seconds" end + + desc 'Import history' + task history: :environment do + 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 def parse_zone_ns_data(domain, zone)