diff --git a/app/models/domain.rb b/app/models/domain.rb index 01ed98484..4855ad41a 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -226,6 +226,9 @@ class Domain < ActiveRecord::Base domain_statuses.find_by(value: DomainStatus::OK).try(:destroy) end + # otherwise domain_statuses are in old state for domain object + domain_statuses.reload + # contacts.includes(:address).each(&:manage_statuses) end @@ -234,14 +237,17 @@ class Domain < ActiveRecord::Base log[:admin_contacts] = admin_contacts.map(&:attributes) log[:tech_contacts] = tech_contacts.map(&:attributes) log[:nameservers] = nameservers.map(&:attributes) - log[:registrant] = [registrant.try(:attributes)] + log[:registrant] = [registrant.try(:attributes)] log end def update_whois_body whois = Whois::Body.new(self) - self.whois_json = whois.whois_json - self.whois_body = whois.whois_body + # validations, callbacks and updated_at are skipped + update_columns( + whois_json: whois.whois_json, + whois_body: whois.whois_body + ) end def update_whois_server diff --git a/db/whois_schema.rb b/db/whois_schema.rb index 0290bd178..0ddeb58b3 100644 --- a/db/whois_schema.rb +++ b/db/whois_schema.rb @@ -19,6 +19,7 @@ ActiveRecord::Schema.define(version: 20150113113236) do create_table "domains", force: :cascade do |t| t.string "name" t.text "whois_body" + t.json "whois_json" t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index aa06aa273..cd022bcb1 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -33,7 +33,7 @@ namespace :db do task create: [:environment, :load_config] do databases.each do |name| begin - puts "\n---------------------------- Create #{name} ----------------------------------------\n" + puts "\n------------------------ Create #{name} ---------------------------------------\n" ActiveRecord::Base.clear_all_connections! conf = ActiveRecord::Base.configurations @@ -75,7 +75,7 @@ namespace :db do task load: [:environment, :load_config] do databases.each do |name| begin - puts "\n---------------------------- #{name} schema loaded ----------------------------------------\n" + puts "\n------------------------ #{name} schema loading -----------------------------\n" ActiveRecord::Base.clear_all_connections! ActiveRecord::Base.establish_connection(name.to_sym) if ActiveRecord::Base.connection.table_exists?('schema_migrations') diff --git a/lib/tasks/whois.rake b/lib/tasks/whois.rake index 29d15fcb8..359db50da 100644 --- a/lib/tasks/whois.rake +++ b/lib/tasks/whois.rake @@ -1,33 +1,41 @@ namespace :whois do - desc 'Delete whois database data and import all from Registry (fast)' - task reset: :environment do + desc 'Regenerate whois_body and whois_json at Registry master database (slow)' + task regenerate: :environment do start = Time.zone.now.to_f - print "-----> Reset whois database and sync..." - domains = Domain.pluck(:name, :whois_body) + print "-----> Regenerate whois_body and whois_json at Registry master database..." + Domain.included.find_each(batch_size: 50000).with_index do |d, index| + d.update_whois_body + print '.' if index % 100 == 0 + end + puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" + end + + desc 'Delete whois database data and sync with Registry master database (fast)' + 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::Domain.delete_all - Whois::Domain.import([:name, :whois_body], domains) + Whois::Domain.import([:name, :whois_body, :whois_json], domains) puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" end - desc 'Sync whois database without reset (slow)' - task sync: :environment do - start = Time.zone.now.to_f - print "-----> Sync whois database..." - Domain.select(:id, :name, :whois_body).find_each(batch_size: 100000).with_index do |d, index| - d.update_whois_server - print '.' if index % 100 == 0 + namespace :schema do + desc 'Load whois schema' + task 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) + if ActiveRecord::Base.connection.table_exists?('schema_migrations') + puts 'Found tables, skip schema load!' + else + load("#{Rails.root}/db/#{schema_file(whois_db)}") + end + rescue => e + puts "\n#{e}" + end end - puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" - end - - desc 'Regenerate whois_body at Registry master database (slow)' - task generate: :environment do - start = Time.zone.now.to_f - print "-----> Update Registry records..." - Domain.included.find_each(batch_size: 100000).with_index do |d, index| - d.update_columns(whois_body: d.update_whois_body) - print '.' if index % 100 == 0 - end - puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" end end