mirror of
https://github.com/internetee/registry.git
synced 2025-07-03 09:43:36 +02:00
Story#108521790 - first try to import nameservers
This commit is contained in:
parent
5f0421af38
commit
f6d16f51e9
6 changed files with 119 additions and 3 deletions
|
@ -8,6 +8,7 @@ module Legacy
|
|||
belongs_to :domain, foreign_key: :id
|
||||
belongs_to :history, foreign_key: :historyid
|
||||
has_one :object_history, foreign_key: :historyid, primary_key: :historyid
|
||||
has_many :nsset_histories, foreign_key: :id, primary_key: :nsset
|
||||
has_many :domain_contact_map_histories, foreign_key: :historyid, primary_key: :historyid
|
||||
has_many :nsset_contact_map_histories, foreign_key: :historyid, primary_key: :historyid
|
||||
|
||||
|
@ -49,6 +50,72 @@ module Legacy
|
|||
@new_registrant_id ||= ::Contact.find_by(legacy_id: registrant).try(:id)
|
||||
end
|
||||
|
||||
def user
|
||||
@user ||= Registrar.find_by(legacy_id: obj_his.upid || obj_his.clid).try(:api_users).try(:first)
|
||||
end
|
||||
|
||||
|
||||
# returns imported nameserver ids
|
||||
def import_nameservers_history(new_domain, time)
|
||||
#removing previous nameservers
|
||||
NameserverVersion.where("object->>legacy_domain_id").where(event: :create).where("created_at <= ?", time).each do |nv|
|
||||
if NameserverVersion.where(item_type: nv.item_type, item_id: nv.item_id, event: :destroy).none?
|
||||
NameserverVersion.create!(
|
||||
item_type: nv.item_type,
|
||||
item_id: nv.item_id,
|
||||
event: :destroy,
|
||||
whodunnit: user.try(:id),
|
||||
object: nv.object_changes.each_with_object({}){|(k,v),hash| hash[k] = v.last },
|
||||
object_changes: {},
|
||||
created_at: time
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
if (nssets = nsset_histories.at(time).to_a).any?
|
||||
ids = []
|
||||
nssets.each do |nsset|
|
||||
nsset.host_histories.at(time).each do |host|
|
||||
ips = {ipv4: [],ipv6: []}
|
||||
host.host_ipaddr_map_histories.at(time).each do |ip_map|
|
||||
next unless ip_map.ipaddr
|
||||
ips[:ipv4] << ip_map.ipaddr.to_s.strip if ip_map.ipaddr.ipv4?
|
||||
ips[:ipv6] << ip_map.ipaddr.to_s.strip if ip_map.ipaddr.ipv6?
|
||||
end
|
||||
|
||||
server = {
|
||||
id: Nameserver.next_id,
|
||||
hostname: host.fqdn.try(:strip),
|
||||
ipv4: ips[:ipv4],
|
||||
ipv6: ips[:ipv6],
|
||||
creator_str: object_registry.try(:registrar).try(:name),
|
||||
updator_str: object_history.try(:registrar).try(:name) || object_registry.try(:registrar).try(:name),
|
||||
legacy_domain_id: id,
|
||||
domain_id: new_domain.id,
|
||||
created_at: nsset.object_registry.try(:crdate),
|
||||
updated_at: nsset.object_registry.try(:object_history).read_attribute(:update) || nsset.object_registry.try(:crdate)
|
||||
}
|
||||
|
||||
NameserverVersion.create!(
|
||||
item_type: Nameserver.to_s,
|
||||
item_id: server[:id],
|
||||
event: :create,
|
||||
whodunnit: user.try(:id),
|
||||
object: nil,
|
||||
object_changes: server.each_with_object({}){|(k,v), h| h[k] = [nil, v]},
|
||||
created_at: time
|
||||
)
|
||||
ids << server[:id]
|
||||
end
|
||||
|
||||
end
|
||||
ids
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
def changes_dates_for domain_id
|
||||
sql = %Q{SELECT dh.*, valid_from
|
||||
|
|
15
app/models/legacy/host_history.rb
Normal file
15
app/models/legacy/host_history.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Legacy
|
||||
class HostHistory < Db
|
||||
self.table_name = :host_history
|
||||
self.primary_key = :id
|
||||
|
||||
belongs_to :history, foreign_key: :historyid
|
||||
has_many :host_ipaddr_maps, foreign_key: :hostid
|
||||
has_many :host_ipaddr_map_histories, foreign_key: :hostid, primary_key: :id
|
||||
|
||||
def self.at(time)
|
||||
joins(:history).where("(valid_from is null or valid_from <= '#{time.to_s}'::TIMESTAMPTZ)
|
||||
AND (valid_to is null or valid_to >= '#{time}'::TIMESTAMPTZ)")
|
||||
end
|
||||
end
|
||||
end
|
12
app/models/legacy/host_ipaddr_map_history.rb
Normal file
12
app/models/legacy/host_ipaddr_map_history.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
module Legacy
|
||||
class HostIpaddrMapHistory < Db
|
||||
self.table_name = :host_ipaddr_map_history
|
||||
self.primary_key = :id
|
||||
belongs_to :history, foreign_key: :historyid
|
||||
|
||||
def self.at(time)
|
||||
joins(:history).where("(valid_from is null or valid_from <= '#{time.to_s}'::TIMESTAMPTZ)
|
||||
AND (valid_to is null or valid_to >= '#{time}'::TIMESTAMPTZ)")
|
||||
end
|
||||
end
|
||||
end
|
17
app/models/legacy/nsset_history.rb
Normal file
17
app/models/legacy/nsset_history.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
module Legacy
|
||||
class NssetHistory < Db
|
||||
self.table_name = :nsset_history
|
||||
self.primary_key = :id
|
||||
|
||||
belongs_to :object, foreign_key: :id
|
||||
belongs_to :object_registry, foreign_key: :id
|
||||
belongs_to :history, foreign_key: :historyid, primary_key: :id
|
||||
has_many :hosts, foreign_key: :nssetid
|
||||
has_many :host_histories, foreign_key: :nssetid, primary_key: :id
|
||||
|
||||
def self.at(time)
|
||||
joins(:history).where("(valid_from is null or valid_from <= '#{time.to_s}'::TIMESTAMPTZ)
|
||||
AND (valid_to is null or valid_to >= '#{time}'::TIMESTAMPTZ)")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -98,5 +98,9 @@ class Nameserver < ActiveRecord::Base
|
|||
# ignoring ips
|
||||
rel
|
||||
end
|
||||
|
||||
def next_id
|
||||
self.connection.select_value("SELECT nextval('#{self.sequence_name}')")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -107,6 +107,8 @@ namespace :import do
|
|||
new_attrs = responder.get_current_domain_object(time, orig_history_klass[:param])
|
||||
new_attrs[:id] = domain.id
|
||||
new_attrs[:updated_at] = time
|
||||
p time
|
||||
p responder.import_nameservers_history(domain, time) if responder.respond_to?(:import_nameservers_history)
|
||||
|
||||
event = :update
|
||||
event = :create if i == 0
|
||||
|
@ -120,13 +122,12 @@ namespace :import do
|
|||
end
|
||||
next if changes.blank? && event != :destroy
|
||||
obj_his = Legacy::ObjectHistory.find_by(historyid: responder.historyid)
|
||||
user = Registrar.find_by(legacy_id: obj_his.upid || obj_his.clid).try(:api_users).try(:first)
|
||||
|
||||
DomainVersion.create!(
|
||||
p DomainVersion.new(
|
||||
item_type: domain.class,
|
||||
item_id: domain.id,
|
||||
event: event,
|
||||
whodunnit: user.try(:id),
|
||||
whodunnit: responder.user.try(:id),
|
||||
object: last_changes,
|
||||
object_changes: changes,
|
||||
created_at: time,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue