diff --git a/app/models/domain.rb b/app/models/domain.rb index 4855ad41a..ae8e24540 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -33,6 +33,7 @@ class Domain < ActiveRecord::Base has_many :dnskeys, dependent: :destroy has_many :keyrelays + has_one :whois_body, dependent: :destroy accepts_nested_attributes_for :dnskeys, allow_destroy: true @@ -124,6 +125,7 @@ class Domain < ActiveRecord::Base includes( :registrar, :nameservers, + :whois_body, { tech_contacts: :registrar }, { admin_contacts: :registrar } ) @@ -242,17 +244,15 @@ class Domain < ActiveRecord::Base end def update_whois_body - whois = Whois::Body.new(self) - # validations, callbacks and updated_at are skipped - update_columns( - whois_json: whois.whois_json, - whois_body: whois.whois_body - ) + self.whois_body = WhoisBody.create if whois_body.blank? + whois_body.update end def update_whois_server - wd = Whois::Domain.find_or_initialize_by(name: name) - wd.whois_body = whois_body - wd.save + if whois_body.present? + whois_body.update_whois_server + else + logger.info "NO WHOIS BODY for domain: #{name}" + end end end diff --git a/app/models/whois/body.rb b/app/models/whois_body.rb similarity index 88% rename from app/models/whois/body.rb rename to app/models/whois_body.rb index 2483b6c60..29e1917b9 100644 --- a/app/models/whois/body.rb +++ b/app/models/whois_body.rb @@ -1,10 +1,20 @@ -class Whois::Body +class WhoisBody < ActiveRecord::Base + belongs_to :domain + + def update_whois_server + return logger.info "NO WHOIS NAME for whois_body id: #{id}" if name.blank? + wd = Whois::Domain.find_or_initialize_by(name: name) + wd.whois_body = whois_body + wd.whois_json = whois_json + wd.save + end + # rubocop:disable Metrics/MethodLength def h @h ||= HashWithIndifferentAccess.new end - def initialize(domain) + def update h[:name] = domain.name h[:registrant] = domain.registrant.name h[:status] = domain.domain_statuses.map(&:human_value).join(', ') @@ -41,13 +51,14 @@ class Whois::Body updated_at: ns.updated_at.to_s(:db) } end + + self.name = h[:name] + self.whois_body = body + self.whois_json = h + save end - def whois_json - h - end - - def whois_body + def body <<-EOS Estonia .ee Top Level Domain WHOIS server diff --git a/db/migrate/20150422092514_add_whois_body_to_registry.rb b/db/migrate/20150422092514_add_whois_body_to_registry.rb new file mode 100644 index 000000000..6791bbcf9 --- /dev/null +++ b/db/migrate/20150422092514_add_whois_body_to_registry.rb @@ -0,0 +1,15 @@ +class AddWhoisBodyToRegistry < ActiveRecord::Migration + def change + create_table :whois_bodies, force: :cascade do |t| + t.integer :domain_id + t.string :name + t.text :whois_body + t.json :whois_json + t.datetime :created_at, null: false + t.datetime :updated_at, null: false + end + add_index :whois_bodies, :domain_id + remove_column :domains, :whois_body, :text + remove_column :domains, :whois_json, :json + end +end diff --git a/db/schema.rb b/db/schema.rb index 21b95105b..3ecb2783b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -294,13 +294,15 @@ ActiveRecord::Schema.define(version: 20150423083308) do t.string "period_unit", limit: 1 t.string "creator_str" t.string "updator_str" - t.text "whois_body" t.integer "legacy_id" t.integer "legacy_registrar_id" t.integer "legacy_registrant_id" - t.json "whois_json" + t.datetime "outzone_at" + t.datetime "delete_at" end + add_index "domains", ["delete_at"], name: "index_domains_on_delete_at", using: :btree + add_index "domains", ["outzone_at"], name: "index_domains_on_outzone_at", using: :btree add_index "domains", ["registrant_id"], name: "index_domains_on_registrant_id", using: :btree add_index "domains", ["registrar_id"], name: "index_domains_on_registrar_id", using: :btree @@ -908,6 +910,17 @@ ActiveRecord::Schema.define(version: 20150423083308) do t.text "depricated_table_but_somehow_paper_trail_tests_fails_without_it" end + create_table "whois_bodies", force: :cascade do |t| + t.integer "domain_id" + t.string "name" + t.text "whois_body" + t.json "whois_json" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "whois_bodies", ["domain_id"], name: "index_whois_bodies_on_domain_id", using: :btree + create_table "zonefile_settings", force: :cascade do |t| t.string "origin" t.integer "ttl" diff --git a/lib/tasks/whois.rake b/lib/tasks/whois.rake index 359db50da..a0489e336 100644 --- a/lib/tasks/whois.rake +++ b/lib/tasks/whois.rake @@ -14,9 +14,9 @@ namespace :whois do task export: :environment do start = Time.zone.now.to_f print "-----> Delete whois database data and sync with Registry master database..." - domains = Domain.pluck(:name, :whois_body, :whois_json) + whois_bodies = WhoisBody.pluck(:name, :whois_body, :whois_json) Whois::Domain.delete_all - Whois::Domain.import([:name, :whois_body, :whois_json], domains) + Whois::Domain.import([:name, :whois_body, :whois_json], whois_bodies) puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" end diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 90f1a156e..12d5a3752 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -34,13 +34,9 @@ describe Domain do @domain.versions.should == [] end - it 'should not have whois_body' do + it 'should not have whois body' do @domain.whois_body.should == nil end - - it 'should not have whois json' do - @domain.whois_json.should == nil - end end context 'with valid attributes' do @@ -82,7 +78,7 @@ describe Domain do end it 'should have whois json by default' do - @domain.whois_json.present?.should == true + @domain.whois_body.whois_json.present?.should == true end it 'should have whois_body present by default' do @@ -113,7 +109,7 @@ describe Domain do @domain.nameservers = [ns1, ns2] @domain.update_whois_body - @domain.whois_body.should == <<-EOS + @domain.whois_body.whois_body.should == <<-EOS Estonia .ee Top Level Domain WHOIS server Domain: