Move data migrations from rake tasks to gem

See #1298
This commit is contained in:
Alex Sherman 2020-02-25 14:19:24 +05:00
parent ed27152bb1
commit 4ddbb08e80
19 changed files with 111 additions and 271 deletions

View file

@ -36,10 +36,10 @@ gem 'devise', '~> 4.7'
gem 'grape'
# registry specfic
gem 'data_migrate', '~> 6.1'
gem 'isikukood' # for EE-id validation
gem 'simpleidn', '0.0.9' # For punycode
gem 'money-rails'
gem 'data_migrate'
gem 'whenever', '0.9.4', require: false
# country listing

View file

@ -462,7 +462,7 @@ DEPENDENCIES
company_register!
countries
daemons-rails (= 1.2.1)
data_migrate
data_migrate (~> 6.1)
database_cleaner
devise (~> 4.7)
digidoc_client!

View file

@ -1,16 +1,15 @@
class AddCertCommonName < ActiveRecord::Migration
class AddCertCommonName < ActiveRecord::Migration[5.1]
def self.up
Certificate.all.each do |x|
if x.crt.blank? && x.csr.present?
pc = x.parsed_csr.try(:subject).try(:to_s) || ''
cn = pc.scan(/\/CN=(.+)/).flatten.first
x.common_name = cn.split('/').first
end
x.save
end
# Certificate.all.each do |x|
# if x.crt.blank? && x.csr.present?
# pc = x.parsed_csr.try(:subject).try(:to_s) || ''
# cn = pc.scan(/\/CN=(.+)/).flatten.first
# x.common_name = cn.split('/').first
# end
# x.save
# end
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -1,28 +1,27 @@
class AddCertMd5 < ActiveRecord::Migration
class AddCertMd5 < ActiveRecord::Migration[5.1]
def self.up
Certificate.all.each do |x|
if x.crt.present? && x.csr.present?
x.interface = Certificate::REGISTRAR
x.md5 = OpenSSL::Digest::MD5.new(x.parsed_crt.to_der).to_s
pc = x.parsed_crt.try(:subject).try(:to_s) || ''
cn = pc.scan(/\/CN=(.+)/).flatten.first
x.common_name = cn.split('/').first
elsif x.crt.present? && x.csr.blank?
x.interface = Certificate::API
x.md5 = OpenSSL::Digest::MD5.new(x.parsed_crt.to_der).to_s
pc = x.parsed_crt.try(:subject).try(:to_s) || ''
cn = pc.scan(/\/CN=(.+)/).flatten.first
x.common_name = cn.split('/').first
elsif x.crt.blank? && x.csr.present?
x.interface = Certificate::REGISTRAR
end
x.save
end
# Certificate.all.each do |x|
# if x.crt.present? && x.csr.present?
# x.interface = Certificate::REGISTRAR
# x.md5 = OpenSSL::Digest::MD5.new(x.parsed_crt.to_der).to_s
#
# pc = x.parsed_crt.try(:subject).try(:to_s) || ''
# cn = pc.scan(/\/CN=(.+)/).flatten.first
# x.common_name = cn.split('/').first
# elsif x.crt.present? && x.csr.blank?
# x.interface = Certificate::API
# x.md5 = OpenSSL::Digest::MD5.new(x.parsed_crt.to_der).to_s
#
# pc = x.parsed_crt.try(:subject).try(:to_s) || ''
# cn = pc.scan(/\/CN=(.+)/).flatten.first
# x.common_name = cn.split('/').first
# elsif x.crt.blank? && x.csr.present?
# x.interface = Certificate::REGISTRAR
# end
# x.save
# end
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -1,9 +1,8 @@
class AddRenewSetting < ActiveRecord::Migration
class AddRenewSetting < ActiveRecord::Migration[5.1]
def self.up
Setting.days_to_renew_domain_before_expire = 90
# Setting.days_to_renew_domain_before_expire = 90
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -1,10 +1,9 @@
class AddExpireSettings < ActiveRecord::Migration
class AddExpireSettings < ActiveRecord::Migration[5.1]
def self.up
Setting.expire_warning_period = 15
Setting.redemption_grace_period = 30
# Setting.expire_warning_period = 15
# Setting.redemption_grace_period = 30
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -1,15 +1,14 @@
class RefactorDomainStatuses < ActiveRecord::Migration
class RefactorDomainStatuses < ActiveRecord::Migration[5.1]
def self.up
Domain.find_each do |x|
statuses = []
x.domain_statuses.each do |ds|
statuses << ds.value
end
x.update_column('statuses', statuses)
end
# Domain.find_each do |x|
# statuses = []
# x.domain_statuses.each do |ds|
# statuses << ds.value
# end
# x.update_column('statuses', statuses) if x.statuses.blank?
# end
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -1,15 +1,14 @@
class RefactorContactStatuses < ActiveRecord::Migration
class RefactorContactStatuses < ActiveRecord::Migration[5.1]
def self.up
Contact.find_each do |contact|
statuses = []
contact.depricated_statuses.each do |ds|
statuses << ds.value
end
contact.update_column('statuses', statuses)
end
# Contact.find_each do |contact|
# statuses = []
# contact.depricated_statuses.each do |ds|
# statuses << ds.value
# end
# contact.update_column('statuses', statuses)
# end
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -0,0 +1,19 @@
class ConvertDomainDeleteDate < ActiveRecord::Migration[5.1]
def up
# processed_domain_count = 0
#
# Domain.transaction do
# Domain.find_each do |domain|
# next unless domain.delete_date
#
# domain.update_columns(delete_date: domain.delete_date + 1.day)
# processed_domain_count += 1
# end
# end
#
# puts "Domains processed: #{processed_domain_count}"
end
def down
end
end

View file

@ -0,0 +1,18 @@
class DeleteOrphanedRegistrantVerifications < ActiveRecord::Migration[5.1]
def up
# orphaned_registrant_verifications = RegistrantVerification.where.not(domain_id: Domain.ids)
# orphaned_registrant_verification_count = orphaned_registrant_verifications.count
# processed_registrant_verification_count = 0
#
# orphaned_registrant_verifications.each do |registrant_verification|
# registrant_verification.destroy!
# processed_registrant_verification_count += 1
# end
#
# puts "Processed: #{processed_registrant_verification_count} out of" \
# " #{orphaned_registrant_verification_count}"
end
def down
end
end

View file

@ -0,0 +1,19 @@
class RegenerateRegistrarReferenceNumbers < ActiveRecord::Migration[5.1]
def up
# processed_registrar_count = 0
#
# Registrar.transaction do
# Registrar.all.each do |registrar|
# next unless registrar.reference_no.start_with?('RF')
#
# registrar.update_columns(reference_no: Billing::ReferenceNo.generate)
# processed_registrar_count += 1
# end
# end
#
# puts "Registrars processed: #{processed_registrar_count}"
end
def down
end
end

2
db/data_schema.rb Normal file
View file

@ -0,0 +1,2 @@
# encoding: UTF-8
DataMigrate::Data.define(version: 20150707103801)

View file

View file

@ -1,16 +0,0 @@
namespace :data_migrations do
task convert_domain_delete_date: :environment do
processed_domain_count = 0
Domain.transaction do
Domain.find_each do |domain|
next unless domain.delete_date
domain.update_columns(delete_date: domain.delete_date + 1.day)
processed_domain_count += 1
end
end
puts "Domains processed: #{processed_domain_count}"
end
end

View file

@ -1,15 +0,0 @@
namespace :data_migrations do
task delete_orphaned_registrant_verifications: :environment do
orphaned_registrant_verifications = RegistrantVerification.where.not(domain_id: Domain.ids)
orphaned_registrant_verification_count = orphaned_registrant_verifications.count
processed_registrant_verification_count = 0
orphaned_registrant_verifications.each do |registrant_verification|
registrant_verification.destroy!
processed_registrant_verification_count += 1
end
puts "Processed: #{processed_registrant_verification_count} out of" \
" #{orphaned_registrant_verification_count}"
end
end

View file

@ -1,16 +0,0 @@
namespace :data_migrations do
task regenerate_registrar_reference_numbers: [:environment] do
processed_registrar_count = 0
Registrar.transaction do
Registrar.all.each do |registrar|
next unless registrar.reference_no.start_with?('RF')
registrar.update_columns(reference_no: Billing::ReferenceNo.generate)
processed_registrar_count += 1
end
end
puts "Registrars processed: #{processed_registrar_count}"
end
end

View file

@ -1,61 +0,0 @@
require 'test_helper'
class RegenerateRegistrarReferenceNumbersTaskTest < ActiveSupport::TestCase
def test_regenerates_registrar_reference_numbers_to_estonian_format
registrar = registrars(:bestnames)
registrar.update_column(:reference_no, 'RF1111')
capture_io { run_task }
registrar.reload
assert_not registrar.reference_no.start_with?('RF')
end
def test_bypasses_registrar_validation
registrar = registrars(:invalid)
registrar.update_column(:reference_no, 'RF1111')
assert registrar.invalid?
capture_io { run_task }
registrar.reload
assert_not registrar.reference_no.start_with?('RF')
end
def test_does_not_regenerate_when_the_task_is_run_again
registrar = registrars(:bestnames)
registrar.update!(reference_no: '1111')
capture_io { run_task }
registrar.reload
assert_equal '1111', registrar.reference_no
end
def test_keeps_iso_reference_number_on_the_invoice_unchanged
registrar = registrars(:bestnames)
registrar.update_column(:reference_no, 'RF1111')
invoice = invoices(:one)
invoice.update!(reference_no: 'RF2222')
capture_io { run_task }
invoice.reload
assert_equal 'RF2222', invoice.reference_no
end
def test_output
registrar = registrars(:bestnames)
registrar.update_column(:reference_no, 'RF1111')
assert_output "Registrars processed: 1\n" do
run_task
end
end
private
def run_task
Rake::Task['data_migrations:regenerate_registrar_reference_numbers'].execute
end
end

View file

@ -1,61 +0,0 @@
require 'test_helper'
class ConvertDomainDeleteDateTaskTest < ActiveSupport::TestCase
setup do
@domain = domains(:shop)
end
def test_moves_domain_delete_date_one_day_ahead
@domain.update!(delete_date: '2010-07-05')
capture_io do
run_task
end
@domain.reload
assert_equal Date.parse('2010-07-06'), @domain.delete_date
end
def test_processes_invalid_domains
@domain = domains(:invalid)
@domain.update_columns(delete_date: '2010-07-05')
capture_io do
run_task
end
@domain.reload
assert_equal Date.parse('2010-07-06'), @domain.delete_date
end
def test_skips_non_expired_domains
@domain.update!(delete_date: nil)
assert_nothing_raised do
capture_io do
run_task
end
end
end
def test_output
eliminate_effect_of_all_domains_except(@domain)
@domain.update!(delete_date: '2010-07-05')
assert_output "Domains processed: 1\n" do
run_task
end
end
private
def eliminate_effect_of_all_domains_except(domain)
Domain.connection.disable_referential_integrity do
Domain.where("id != #{domain.id}").delete_all
end
end
def run_task
Rake::Task['data_migrations:convert_domain_delete_date'].execute
end
end

View file

@ -1,43 +0,0 @@
require 'test_helper'
class ArchiveOrphanedRegistrantVerificationsTest < ActiveSupport::TestCase
def test_deletes_orphaned_registrant_verifications
create_orphaned_registrant_verification
assert_difference 'RegistrantVerification.count', -1 do
capture_io do
run_task
end
end
end
def test_keeps_non_orphaned_registrant_verifications_intact
assert_no_difference 'RegistrantVerification.count' do
capture_io do
run_task
end
end
end
def test_output
create_orphaned_registrant_verification
assert_output "Processed: 1 out of 1\n" do
run_task
end
end
private
def create_orphaned_registrant_verification
non_existent_domain_id = 55
assert_not_includes Domain.ids, non_existent_domain_id
RegistrantVerification.connection.disable_referential_integrity do
registrant_verifications(:one).update_columns(domain_id: non_existent_domain_id)
end
end
def run_task
Rake::Task['data_migrations:delete_orphaned_registrant_verifications'].execute end
end