From 7fc7ec4f53dd9d1b57b5a7ccae55cf2dd0505415 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Thu, 29 Feb 2024 15:54:55 -0500 Subject: [PATCH] Remove default_value_for usage where database defaults can be used Depends on https://github.com/ManageIQ/manageiq-schema/pull/721 Some of the remaining ones have custom defaults in STI or STI-like subclasses, meaning we can't easily replace these with db defaults. Defaults set in a subclass, not base class, in STI/STI like behavior: app/models/miq_provision_request.rb: default_value_for :source_type, "VmOrTemplate" app/models/service_template_provision_request.rb: default_value_for :process, false (overrides base class default) manageiq/providers/embedded_automation_manager/configuration_script_source.rb: attribute :scm_type, :default => "git" manageiq/providers/embedded_automation_manager/configuration_script_source.rb: attribute :scm_branch, :default => "master" manageiq/providers/cloud_manager/template.rb: attribute :cloud, :default => true manageiq/providers/cloud_manager/vm.rb: attribute :cloud, :default => true manageiq/providers/infra_manager/template.rb: attribute :cloud, :default => false manageiq/providers/infra_manager/vm.rb: attribute :cloud, :default => false manageiq/providers/physical_infra_manager/vm.rb: attribute :cloud, :default => false app/models/service_retire_task.rb: default_value_for :request_type, "service_retire" app/models/vm_migrate_task.rb: default_value_for :request_type, "vm_migrate" app/models/vm_retire_task.rb: default_value_for :request_type, "vm_retire" service_reconfigure_request.rb: attribute :source_type, :default => SOURCE_CLASS_NAME service_template_provision_request.rb: attribute :source_type, :default => SOURCE_CLASS_NAME vm_retire_request.rb: attribute :source_type, :default => SOURCE_CLASS_NAME change_column_default :orchestration_stack_retire_tasks, :request_type, :from => nil, :to => "orchestration_stack_retire" Others, use block notation and isn't replaceable with database defaults. We tried attribute defaults before but they trigger "changes" when the defaults get set, which leads to unexpected changes in behavior. --- app/models/assigned_server_role.rb | 2 -- app/models/blacklisted_event.rb | 1 - app/models/classification.rb | 5 ----- app/models/cloud_database_flavor.rb | 2 -- app/models/dialog.rb | 1 - app/models/dialog_field.rb | 3 --- app/models/endpoint.rb | 1 - app/models/ext_management_system.rb | 2 -- app/models/flavor.rb | 2 -- app/models/git_repository.rb | 1 - app/models/miq_approval.rb | 2 -- app/models/miq_group.rb | 1 - app/models/miq_policy.rb | 4 ---- app/models/miq_request.rb | 4 ---- app/models/miq_request_task.rb | 3 --- app/models/miq_schedule.rb | 2 -- app/models/miq_server.rb | 1 - app/models/miq_user_role.rb | 2 -- app/models/miq_widget.rb | 3 --- app/models/notification_recipient.rb | 1 - app/models/orchestration_template.rb | 3 --- app/models/pxe_server.rb | 2 -- app/models/service.rb | 5 ----- app/models/service_resource.rb | 5 ----- app/models/service_template.rb | 2 -- app/models/share.rb | 2 -- app/models/system_console.rb | 2 -- app/models/tenant.rb | 5 ----- app/models/user.rb | 2 -- app/models/zone.rb | 1 - 30 files changed, 72 deletions(-) diff --git a/app/models/assigned_server_role.rb b/app/models/assigned_server_role.rb index 4155bed5dd4..6d3df400154 100644 --- a/app/models/assigned_server_role.rb +++ b/app/models/assigned_server_role.rb @@ -2,8 +2,6 @@ class AssignedServerRole < ApplicationRecord belongs_to :miq_server belongs_to :server_role - default_value_for :active, false - delegate :master_supported?, :name, :to => :server_role HIGH_PRIORITY = 1 diff --git a/app/models/blacklisted_event.rb b/app/models/blacklisted_event.rb index 4ead1e0717e..58f9c53af3b 100644 --- a/app/models/blacklisted_event.rb +++ b/app/models/blacklisted_event.rb @@ -1,7 +1,6 @@ class BlacklistedEvent < ApplicationRecord belongs_to :ext_management_system, :foreign_key => "ems_id" - default_value_for :enabled, true after_save :reload_all_server_settings after_destroy :reload_all_server_settings, :audit_deletion after_create :audit_creation diff --git a/app/models/classification.rb b/app/models/classification.rb index 35ccf2b4fb4..38fbfe764d5 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -35,11 +35,6 @@ class Classification < ApplicationRecord DEFAULT_NAMESPACE = "/managed".freeze - default_value_for :read_only, false - default_value_for :syntax, "string" - default_value_for :single_value, false - default_value_for :show, true - FIXTURE_FILE = FIXTURE_DIR.join("classifications.yml") def self.hash_all_by_type_and_name(conditions = {}) diff --git a/app/models/cloud_database_flavor.rb b/app/models/cloud_database_flavor.rb index db4c39c4d00..e584d9f2dce 100644 --- a/app/models/cloud_database_flavor.rb +++ b/app/models/cloud_database_flavor.rb @@ -8,7 +8,5 @@ class CloudDatabaseFlavor < ApplicationRecord virtual_total :total_cloud_databases, :cloud_databases - default_value_for :enabled, true - scope :active, -> { where(:enabled => true) } end diff --git a/app/models/dialog.rb b/app/models/dialog.rb index 854436bdb54..a42715d669c 100644 --- a/app/models/dialog.rb +++ b/app/models/dialog.rb @@ -4,7 +4,6 @@ class Dialog < ApplicationRecord # The following gets around a glob symbolic link issue YAML_FILES_PATTERN = "{,*/**/}*.{yaml,yml}".freeze - default_value_for :system, false has_many :dialog_tabs, -> { order(:position) }, :dependent => :destroy validate :validate_children diff --git a/app/models/dialog_field.rb b/app/models/dialog_field.rb index 0eed881d842..3cf6abd173a 100644 --- a/app/models/dialog_field.rb +++ b/app/models/dialog_field.rb @@ -29,10 +29,7 @@ class DialogField < ApplicationRecord validates :name, :exclusion => {:in => %w(action controller), :message => "Field Name %{value} is reserved."} - default_value_for :required, false - default_value_for(:visible) { true } validates :visible, inclusion: { in: [ true, false ] } - default_value_for :load_values_on_init, true serialize :values serialize :values_method_options, Hash diff --git a/app/models/endpoint.rb b/app/models/endpoint.rb index 18d3ce7449e..f150e7673de 100644 --- a/app/models/endpoint.rb +++ b/app/models/endpoint.rb @@ -3,7 +3,6 @@ class Endpoint < ApplicationRecord belongs_to :resource, :polymorphic => true - default_value_for :verify_ssl, OpenSSL::SSL::VERIFY_PEER validates :verify_ssl, :inclusion => {:in => [OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER]} validates :port, :numericality => {:only_integer => true, :allow_nil => true, :greater_than => 0} validates :url, :uniqueness_when_changed => true, :allow_blank => true, :unless => :allow_duplicate_url? diff --git a/app/models/ext_management_system.rb b/app/models/ext_management_system.rb index 13825b5e4eb..b3ff4c50fb6 100644 --- a/app/models/ext_management_system.rb +++ b/app/models/ext_management_system.rb @@ -306,8 +306,6 @@ def validate_zone_not_maintenance_when_ems_enabled? alias_method :clusters, :ems_clusters # Used by web-services to return clusters as the property name alias_attribute :to_s, :name - default_value_for :enabled, true - # Move ems to maintenance zone and backup current one # @param orig_zone [Integer] because of zone of child manager can be changed by parent manager's ensure_managers() callback # we need to specify original zone for children explicitly diff --git a/app/models/flavor.rb b/app/models/flavor.rb index 2830d8df38d..833a3a57361 100644 --- a/app/models/flavor.rb +++ b/app/models/flavor.rb @@ -12,8 +12,6 @@ class Flavor < ApplicationRecord virtual_total :total_vms, :vms - default_value_for :enabled, true - alias_attribute :cpus, :cpu_total_cores alias_attribute :cpu_cores, :cpu_cores_per_socket diff --git a/app/models/git_repository.rb b/app/models/git_repository.rb index f19b91c19bc..fcf11c2e407 100644 --- a/app/models/git_repository.rb +++ b/app/models/git_repository.rb @@ -11,7 +11,6 @@ class GitRepository < ApplicationRecord validates :url, :format => Regexp.union(URI.regexp(%w[http https file ssh]), /\A[-\w:.]+@.*:/), :allow_nil => false - default_value_for :verify_ssl, OpenSSL::SSL::VERIFY_PEER validates :verify_ssl, :inclusion => {:in => [OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER]} has_many :git_branches, :dependent => :destroy diff --git a/app/models/miq_approval.rb b/app/models/miq_approval.rb index b7eb94539ec..c0f3cba5bef 100644 --- a/app/models/miq_approval.rb +++ b/app/models/miq_approval.rb @@ -3,8 +3,6 @@ class MiqApproval < ApplicationRecord belongs_to :stamper, :class_name => "User" belongs_to :miq_request - default_value_for :state, "pending" - def approver=(approver) super self.approver_name = approver.try(:name) diff --git a/app/models/miq_group.rb b/app/models/miq_group.rb index 2d89f0957bc..a550fbbcd68 100644 --- a/app/models/miq_group.rb +++ b/app/models/miq_group.rb @@ -32,7 +32,6 @@ class MiqGroup < ApplicationRecord serialize :settings - default_value_for :group_type, USER_GROUP default_value_for(:sequence) { next_sequence } acts_as_miq_taggable diff --git a/app/models/miq_policy.rb b/app/models/miq_policy.rb index 6b7226d8bbf..eb4469e8921 100644 --- a/app/models/miq_policy.rb +++ b/app/models/miq_policy.rb @@ -35,10 +35,6 @@ def self.policy_modes include YamlImportExportMixin before_validation :default_name_to_guid, :on => :create - default_value_for :towhat, 'Vm' - default_value_for :active, true - default_value_for :mode, 'control' - # NOTE: If another class references MiqPolicy through an ActiveRecord association, # particularly has_one and belongs_to, calling .conditions will result in # that method being directly called on the proxy object, as opposed to the diff --git a/app/models/miq_request.rb b/app/models/miq_request.rb index a6d8949f0eb..cd5ceae7209 100644 --- a/app/models/miq_request.rb +++ b/app/models/miq_request.rb @@ -25,11 +25,7 @@ class MiqRequest < ApplicationRecord serialize :options, Hash default_value_for(:message) { |r| "#{r.class::TASK_DESCRIPTION} - Request Created" } - default_value_for :request_state, 'pending' - default_value_for(:request_type) { |r| r.request_types.first } - default_value_for :status, 'Ok' - default_value_for :process, true validates_inclusion_of :approval_state, :in => %w(pending_approval approved denied), :message => "should be 'pending_approval', 'approved' or 'denied'" validates_inclusion_of :status, :in => %w(Ok Warn Error Timeout Denied) diff --git a/app/models/miq_request_task.rb b/app/models/miq_request_task.rb index 83661a738dc..a71a862f9e2 100644 --- a/app/models/miq_request_task.rb +++ b/app/models/miq_request_task.rb @@ -13,9 +13,6 @@ class MiqRequestTask < ApplicationRecord serialize :phase_context, Hash serialize :options, Hash - default_value_for :state, 'pending' - default_value_for :status, 'Ok' - delegate :request_class, :task_description, :to => :class validates_inclusion_of :status, :in => %w( Ok Warn Error Timeout ) diff --git a/app/models/miq_schedule.rb b/app/models/miq_schedule.rb index 519a764c578..fb26599fa02 100644 --- a/app/models/miq_schedule.rb +++ b/app/models/miq_schedule.rb @@ -44,8 +44,6 @@ class MiqSchedule < ApplicationRecord ALLOWED_CLASS_METHOD_ACTIONS = %w[automation_request].freeze IMPORT_CLASS_NAMES = %w[MiqSchedule].freeze - default_value_for :userid, "system" - default_value_for :enabled, true default_value_for(:zone_id) { MiqServer.my_server.zone_id } def set_start_time_and_prod_default diff --git a/app/models/miq_server.rb b/app/models/miq_server.rb index 1e0317fbe8d..faf8a40919a 100644 --- a/app/models/miq_server.rb +++ b/app/models/miq_server.rb @@ -25,7 +25,6 @@ class MiqServer < ApplicationRecord before_destroy :validate_is_deleteable after_destroy :destroy_linked_events_queue - default_value_for(:name, "EVM") default_value_for(:zone) { Zone.default_zone } scope :active_miq_servers, -> { where(:status => STATUSES_ACTIVE) } diff --git a/app/models/miq_user_role.rb b/app/models/miq_user_role.rb index c7aca0e1654..8359df6d774 100644 --- a/app/models/miq_user_role.rb +++ b/app/models/miq_user_role.rb @@ -14,8 +14,6 @@ class MiqUserRole < ApplicationRecord serialize :settings - default_value_for :read_only, false - FIXTURE_PATH = File.join(FIXTURE_DIR, table_name) FIXTURE_YAML = "#{FIXTURE_PATH}.yml" diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index b9d19ca673b..4f7f99b444d 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -4,9 +4,6 @@ class MiqWidget < ApplicationRecord include ReadOnlyMixin - default_value_for :enabled, true - default_value_for :read_only, false - DEFAULT_ROW_COUNT = 5 IMPORT_CLASS_NAMES = %w(MiqWidget).freeze diff --git a/app/models/notification_recipient.rb b/app/models/notification_recipient.rb index 644a21302a4..723e62f28b1 100644 --- a/app/models/notification_recipient.rb +++ b/app/models/notification_recipient.rb @@ -1,7 +1,6 @@ class NotificationRecipient < ApplicationRecord belongs_to :notification belongs_to :user - default_value_for :seen, false virtual_column :details, :type => :string scope :unseen, -> { where(:seen => false) } diff --git a/app/models/orchestration_template.rb b/app/models/orchestration_template.rb index fe1c674b77e..48b51328335 100644 --- a/app/models/orchestration_template.rb +++ b/app/models/orchestration_template.rb @@ -8,9 +8,6 @@ class OrchestrationTemplate < ApplicationRecord has_many :stacks, :class_name => "OrchestrationStack" has_one :picture, :dependent => :destroy, :as => :resource, :autosave => true - default_value_for :draft, false - default_value_for :orderable, true - validates :md5, :uniqueness_when_changed => {:scope => :draft, :message => "of content already exists (content must be unique)"}, :if => :unique_md5? diff --git a/app/models/pxe_server.rb b/app/models/pxe_server.rb index 27efdb88b82..ee6581611a1 100644 --- a/app/models/pxe_server.rb +++ b/app/models/pxe_server.rb @@ -5,8 +5,6 @@ class PxeServer < ApplicationRecord alias_attribute :description, :name - default_value_for :customization_directory, "" - serialize :visibility acts_as_miq_taggable diff --git a/app/models/service.rb b/app/models/service.rb index 48286f9ba29..a78edeffb96 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -89,11 +89,6 @@ class Service < ApplicationRecord validates :name, :presence => true - default_value_for :visible, false - default_value_for :initiator, 'user' - default_value_for :lifecycle_state, 'unprovisioned' - default_value_for :retired, false - validates :visible, :inclusion => { :in => [true, false] } validates :retired, :inclusion => { :in => [true, false] } diff --git a/app/models/service_resource.rb b/app/models/service_resource.rb index 219e9bc838c..a4d62640401 100644 --- a/app/models/service_resource.rb +++ b/app/models/service_resource.rb @@ -10,11 +10,6 @@ class ServiceResource < ApplicationRecord belongs_to :resource, :polymorphic => true belongs_to :source, :polymorphic => true - default_value_for :group_idx, 0 - default_value_for :scaling_min, 1 - default_value_for :scaling_max, -1 - default_value_for :provision_index, 0 - virtual_column :resource_name, :type => :string virtual_column :resource_description, :type => :string diff --git a/app/models/service_template.rb b/app/models/service_template.rb index a29f5b28c3e..6965f6c0b23 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -59,8 +59,6 @@ class ServiceTemplate < ApplicationRecord virtual_column :archived, :type => :boolean virtual_column :active, :type => :boolean - default_value_for :internal, false - default_value_for :service_type, SERVICE_TYPE_ATOMIC default_value_for(:generic_subtype) { |st| 'custom' if st.prov_type == 'generic' } virtual_has_one :config_info, :class_name => "Hash" diff --git a/app/models/share.rb b/app/models/share.rb index 3f014a5ee10..f30f1fa80e4 100644 --- a/app/models/share.rb +++ b/app/models/share.rb @@ -11,8 +11,6 @@ class Share < ApplicationRecord validates :tenant, :presence => true validates :user, :presence => true - default_value_for :allow_tenant_inheritance, false - scope :by_tenant_inheritance, ->(tenant) do where(:tenant => tenant.accessible_tenant_ids(:ancestor_ids), :allow_tenant_inheritance => true) diff --git a/app/models/system_console.rb b/app/models/system_console.rb index 4ac3312cc9a..25b7c0b734e 100644 --- a/app/models/system_console.rb +++ b/app/models/system_console.rb @@ -2,8 +2,6 @@ class SystemConsole < ApplicationRecord belongs_to :vm belongs_to :user - default_value_for :opened, false - validates :url_secret, :uniqueness_when_changed => true def connection_params diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 92112d7c20e..cf91bee2414 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -12,11 +12,6 @@ class Tenant < ApplicationRecord acts_as_miq_taggable - default_value_for :name, "My Company" - default_value_for :description, "Tenant for My Company" - default_value_for :divisible, true - default_value_for :use_config_for_attributes, false - before_destroy :ensure_can_be_destroyed has_ancestry(:orphan_strategy => :restrict) diff --git a/app/models/user.rb b/app/models/user.rb index b87c07b39de..2670debb880 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -56,8 +56,6 @@ class User < ApplicationRecord serialize :settings, Hash # Implement settings column as a hash - default_value_for :failed_login_attempts, 0 - scope :in_all_regions, ->(id) { where(:userid => User.default_scoped.where(:id => id).select(:userid)) } def self.with_roles_excluding(identifier, allowed_ids: nil) diff --git a/app/models/zone.rb b/app/models/zone.rb index 322a85e928a..4547c16b79f 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -43,7 +43,6 @@ class Zone < ApplicationRecord include ConfigurationManagementMixin scope :visible, -> { where(:visible => true) } - default_value_for :visible, true def active_miq_servers MiqServer.active_miq_servers.where(:zone_id => id)