diff --git a/Guardfile b/Guardfile index ab7619ed7..31958aa77 100644 --- a/Guardfile +++ b/Guardfile @@ -9,8 +9,8 @@ group :red_green_refactor, halt_on_fail: true do # watch(%r{^(config|lib)/.*}) # end - # guard :rspec, cmd: 'spring rspec --fail-fast', notification: false do - guard :rspec, cmd: 'spring rspec', notification: false do + guard :rspec, cmd: 'spring rspec --fail-fast', notification: false do + # guard :rspec, cmd: 'spring rspec', notification: false do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } diff --git a/app/models/domain.rb b/app/models/domain.rb index ae8e24540..a094f48ae 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -33,7 +33,7 @@ class Domain < ActiveRecord::Base has_many :dnskeys, dependent: :destroy has_many :keyrelays - has_one :whois_body, dependent: :destroy + has_one :whois_record, dependent: :destroy accepts_nested_attributes_for :dnskeys, allow_destroy: true @@ -53,7 +53,7 @@ class Domain < ActiveRecord::Base self.updated_at = Time.zone.now end after_save :manage_automatic_statuses - after_save :update_whois_body + after_save :update_whois_record after_save :update_whois_server validates :name_dirty, domain_name: true, uniqueness: true @@ -125,7 +125,7 @@ class Domain < ActiveRecord::Base includes( :registrar, :nameservers, - :whois_body, + :whois_record, { tech_contacts: :registrar }, { admin_contacts: :registrar } ) @@ -243,14 +243,14 @@ class Domain < ActiveRecord::Base log end - def update_whois_body - self.whois_body = WhoisBody.create if whois_body.blank? - whois_body.update + def update_whois_record + self.whois_record = WhoisRecord.create if whois_record.blank? + whois_record.update end def update_whois_server - if whois_body.present? - whois_body.update_whois_server + if whois_record.present? + whois_record.update_whois_server else logger.info "NO WHOIS BODY for domain: #{name}" end diff --git a/app/models/whois/domain.rb b/app/models/whois/domain.rb deleted file mode 100644 index 5aa890af1..000000000 --- a/app/models/whois/domain.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Whois - class Domain < Whois::Server - self.table_name = 'domains' - end -end diff --git a/app/models/whois/record.rb b/app/models/whois/record.rb new file mode 100644 index 000000000..08a92b6e1 --- /dev/null +++ b/app/models/whois/record.rb @@ -0,0 +1,5 @@ +module Whois + class Record < Whois::Server + self.table_name = 'whois_records' + end +end diff --git a/app/models/whois_body.rb b/app/models/whois_record.rb similarity index 91% rename from app/models/whois_body.rb rename to app/models/whois_record.rb index 29e1917b9..d64cb58d2 100644 --- a/app/models/whois_body.rb +++ b/app/models/whois_record.rb @@ -1,11 +1,11 @@ -class WhoisBody < ActiveRecord::Base +class WhoisRecord < 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 + return logger.info "NO WHOIS NAME for whois record id: #{id}" if name.blank? + wd = Whois::Record.find_or_initialize_by(name: name) + wd.body = body + wd.json = json wd.save end @@ -53,12 +53,12 @@ class WhoisBody < ActiveRecord::Base end self.name = h[:name] - self.whois_body = body - self.whois_json = h + self.body = generated_body + self.json = h save end - def body + def generated_body <<-EOS Estonia .ee Top Level Domain WHOIS server diff --git a/db/migrate/20150422134243_rename_whois_body.rb b/db/migrate/20150422134243_rename_whois_body.rb new file mode 100644 index 000000000..657b1a762 --- /dev/null +++ b/db/migrate/20150422134243_rename_whois_body.rb @@ -0,0 +1,9 @@ +class RenameWhoisBody < ActiveRecord::Migration + def change + rename_column :whois_bodies, :whois_body, :body + rename_column :whois_bodies, :whois_json, :json + remove_index :whois_bodies, :domain_id + rename_table :whois_bodies, :whois_records + add_index :whois_records, :domain_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 3ecb2783b..62520c706 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150423083308) do +ActiveRecord::Schema.define(version: 20150422134243) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -910,16 +910,16 @@ 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| + create_table "whois_records", force: :cascade do |t| t.integer "domain_id" t.string "name" - t.text "whois_body" - t.json "whois_json" + t.text "body" + t.json "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 + add_index "whois_records", ["domain_id"], name: "index_whois_records_on_domain_id", using: :btree create_table "zonefile_settings", force: :cascade do |t| t.string "origin" diff --git a/db/whois_schema.rb b/db/whois_schema.rb index 0ddeb58b3..ffd951e79 100644 --- a/db/whois_schema.rb +++ b/db/whois_schema.rb @@ -14,16 +14,16 @@ ActiveRecord::Schema.define(version: 20150113113236) do # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" + # enable_extension "plpgsql" - create_table "domains", force: :cascade do |t| + create_table "whois_records", force: :cascade do |t| t.string "name" - t.text "whois_body" - t.json "whois_json" + t.text "body" + t.json "json" t.datetime "created_at", null: false t.datetime "updated_at", null: false end - add_index "domains", ["name"], name: "index_domains_on_name", using: :btree + add_index "whois_records", ["name"], name: "index_domains_on_name", using: :btree end diff --git a/lib/tasks/whois.rake b/lib/tasks/whois.rake index a0489e336..9a62dee9a 100644 --- a/lib/tasks/whois.rake +++ b/lib/tasks/whois.rake @@ -1,10 +1,10 @@ namespace :whois do - desc 'Regenerate whois_body and whois_json at Registry master database (slow)' + desc 'Regenerate whois records at Registry master database (slow)' task regenerate: :environment do start = Time.zone.now.to_f - print "-----> Regenerate whois_body and whois_json at Registry master database..." + print "-----> Regenerate whois records at Registry master database..." Domain.included.find_each(batch_size: 50000).with_index do |d, index| - d.update_whois_body + d.update_whois_record print '.' if index % 100 == 0 end puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" @@ -14,14 +14,14 @@ namespace :whois do task export: :environment do start = Time.zone.now.to_f print "-----> Delete whois database data and sync with Registry master database..." - whois_bodies = WhoisBody.pluck(:name, :whois_body, :whois_json) - Whois::Domain.delete_all - Whois::Domain.import([:name, :whois_body, :whois_json], whois_bodies) + whois_records = WhoisRecord.pluck(:name, :body, :json) + Whois::Record.delete_all + Whois::Record.import([:name, :body, :json], whois_records) puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" end namespace :schema do - desc 'Load whois schema' + desc 'Load whois schema into empty whois database' task load: [:environment] do whois_db = "whois_#{Rails.env}" begin @@ -37,5 +37,18 @@ namespace :whois do puts "\n#{e}" end end + + desc 'Force whois schema into exsisting whois database' + task force_load: [:environment] do + whois_db = "whois_#{Rails.env}" + begin + puts "\n------------------------ #{whois_db} schema loading ------------------------------\n" + ActiveRecord::Base.clear_all_connections! + ActiveRecord::Base.establish_connection(whois_db.to_sym) + load("#{Rails.root}/db/#{schema_file(whois_db)}") + rescue => e + puts "\n#{e}" + end + end end end diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 12d5a3752..d84881f14 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -35,7 +35,7 @@ describe Domain do end it 'should not have whois body' do - @domain.whois_body.should == nil + @domain.whois_record.should == nil end end @@ -74,14 +74,14 @@ describe Domain do end it 'should have whois body by default' do - @domain.whois_body.present?.should == true + @domain.whois_record.present?.should == true end it 'should have whois json by default' do - @domain.whois_body.whois_json.present?.should == true + @domain.whois_record.json.present?.should == true end - it 'should have whois_body present by default' do + it 'should have whois record present by default' do @domain.name = 'yeah.ee' @domain.updated_at = Time.zone.parse('2020.02.02 02:00') @domain.registered_at = Time.zone.parse('2000.01.01 9:00') @@ -108,8 +108,8 @@ describe Domain do ns2.updated_at = Time.zone.parse('1970.01.01') @domain.nameservers = [ns1, ns2] - @domain.update_whois_body - @domain.whois_body.whois_body.should == <<-EOS + @domain.update_whois_record + @domain.whois_record.body.should == <<-EOS Estonia .ee Top Level Domain WHOIS server Domain: