Create action to migrate settings to new model

This commit is contained in:
Karl Erik Õunapuu 2020-08-11 14:57:05 +03:00
parent 950bdc6256
commit b3941a373c
5 changed files with 589 additions and 418 deletions

View file

@ -0,0 +1,92 @@
# frozen_string_literal: true
module Concerns
module Settings
module Migratable
extend ActiveSupport::Concern
VALIDATION_SETTINGS = %w[admin_contacts_min_count admin_contacts_max_count tech_contacts_min_count tech_contacts_max_count orphans_contacts_in_months ds_data_allowed key_data_allowed dnskeys_min_count dnskeys_max_count nameserver_required ns_min_count ns_max_count expire_pending_confirmation legal_document_is_mandatory].freeze
EXPIRATION_SETTINGS = %w[days_to_renew_domain_before_expire expire_warning_period redemption_grace_period expiration_reminder_mail].freeze
BILLING_SETTINGS = %w[invoice_number_min invoice_number_max directo_monthly_number_min directo_monthly_number_max directo_monthly_number_last days_to_keep_invoices_active days_to_keep_overdue_invoices_active minimum_deposit directo_receipt_payment_term directo_receipt_product_name directo_sales_agent registry_billing_email registry_invoice_contact registry_vat_no registry_vat_prc registry_bank registry_bank_code registry_iban registry_swift].freeze
CONTACTS_SETTINGS = %w[registry_juridical_name registry_reg_no registry_email registry_phone registry_url registry_street registry_city registry_state registry_zip registry_country_code registry_whois_disclaimer].freeze
INTEGER_SETTINGS =
%w[
admin_contacts_min_count
admin_contacts_max_count
tech_contacts_min_count
tech_contacts_max_count
orphans_contacts_in_months
ds_digest_type
dnskeys_min_count
dnskeys_max_count
ns_min_count
ns_max_count
transfer_wait_time
invoice_number_min
invoice_number_max
days_to_keep_invoices_active
days_to_keep_overdue_invoices_active
days_to_renew_domain_before_expire
expire_warning_period
redemption_grace_period
expire_pending_confirmation
dispute_period_in_months
].freeze
FLOAT_SETTINGS = %w[registry_vat_prc minimum_deposit].freeze
BOOLEAN_SETTINGS =
%w[
ds_data_allowed
key_data_allowed
client_side_status_editing_enabled
registrar_ip_whitelist_enabled
api_ip_whitelist_enabled
request_confrimation_on_registrant_change_enabled
request_confirmation_on_domain_deletion_enabled
nameserver_required
address_processing
legal_document_is_mandatory
].freeze
class_methods do
def copy_from_legacy
sql = 'SELECT var, value, created_at, updated_at, creator_str, updator_str FROM settings ORDER BY settings.id ASC'
old_settings = ActiveRecord::Base.connection.execute(sql)
old_settings.each do |origin|
entry = SettingEntry.find_or_initialize_by(code: origin['var'])
entry[:format] = 'string'
entry[:format] = 'boolean' if BOOLEAN_SETTINGS.include? entry.code
entry[:format] = 'float' if FLOAT_SETTINGS.include? entry.code
entry[:format] = 'integer' if INTEGER_SETTINGS.include? entry.code
entry[:group] = 'other'
if VALIDATION_SETTINGS.include? entry.code
entry[:group] = 'domain_validation'
end
if EXPIRATION_SETTINGS.include? entry.code
entry[:group] = 'domain_expiration'
end
entry[:group] = 'billing' if BILLING_SETTINGS.include? entry.code
entry[:group] = 'contacts' if CONTACTS_SETTINGS.include? entry.code
%w[value created_at updated_at creator_str updator_str].each do |field|
entry[field] = origin[field]
next if field != 'value'
entry.value = origin[field].gsub('--- ', '').strip.gsub("'", '')
end
if entry.save
logger.info "Legacy setting '#{entry.code}' successfully migrated to SettingEntry"
else
logger.error "!!! Failed to migrate setting '#{entry.code}': #{entry.errors.full_messages.join(', ')}"
end
end
end
end
end
end
end

View file

@ -1,7 +1,7 @@
class LegalDocument < ApplicationRecord
cattr_accessor :explicitly_write_file
include EppErrors
MIN_BODY_SIZE = (1.37 * 3.kilobytes).ceil
MIN_BODY_SIZE = 5
if ENV['legal_document_types'].present?
TYPES = ENV['legal_document_types'].split(',').map(&:strip)

View file

@ -4,10 +4,12 @@ class SettingEntry < ApplicationRecord
validates :format, presence: true
validates :group, presence: true
validate :valid_value_format
include Concerns::Settings::Migratable
VALUE_FORMATS = {
string: :string_format,
integer: :integer_format,
float: :float_format,
boolean: :boolean_format,
hash: :hash_format,
array: :array_format,
@ -19,11 +21,7 @@ class SettingEntry < ApplicationRecord
end
def self.with_group(group_name)
SettingEntry.where(group: group_name)
end
def self.groups
SettingEntry.all.pluck(:group).uniq
SettingEntry.order(id: :asc).where(group: group_name)
end
def self.method_missing(method, *args)
@ -48,6 +46,10 @@ class SettingEntry < ApplicationRecord
value.to_i
end
def float_format
value.to_f
end
def boolean_format
value == 'true'
end

View file

@ -2,7 +2,7 @@ class CreateSettingEntries < ActiveRecord::Migration[6.0]
def change
create_table :setting_entries do |t|
t.string :code, null: false, index: { unique: true }
t.string :value, null: false
t.string :value, null: false, default: ''
t.string :group, null: false
t.string :format, null: false

File diff suppressed because it is too large Load diff