From c63bf80b06cae25759ad71ec640f2fbbe28d6e94 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 11 Jul 2023 11:29:15 -0400 Subject: [PATCH] Remove ContainerGroup#old_container_project_id We use deleted_on to represent a record that is deleted. We moved over to using archived (deleted_on) from old_container_project_id Convert all_container_groups to a regular association see also: - f6fe2513486 - https://github.com/ManageIQ/manageiq/pull/16302 --- app/models/container.rb | 1 - app/models/container_group.rb | 4 --- app/models/container_project.rb | 7 ++--- spec/models/container_project/purging_spec.rb | 13 ++++---- spec/models/container_project_spec.rb | 31 +++++++++++++++++++ 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/app/models/container.rb b/app/models/container.rb index da233773e3f..3e13ff20dcc 100644 --- a/app/models/container.rb +++ b/app/models/container.rb @@ -9,7 +9,6 @@ class Container < ApplicationRecord has_one :container_node, :through => :container_group has_one :container_replicator, :through => :container_group has_one :container_project, :through => :container_group - has_one :old_container_project, :through => :container_group belongs_to :container_image has_many :container_port_configs, :dependent => :destroy has_many :container_env_vars, :dependent => :destroy diff --git a/app/models/container_group.rb b/app/models/container_group.rb index 36b0694fa30..23605e901d7 100644 --- a/app/models/container_group.rb +++ b/app/models/container_group.rb @@ -24,7 +24,6 @@ class ContainerGroup < ApplicationRecord has_and_belongs_to_many :container_services, :join_table => :container_groups_container_services belongs_to :container_replicator belongs_to :container_project - belongs_to :old_container_project, :foreign_key => "old_container_project_id", :class_name => 'ContainerProject' belongs_to :container_build_pod has_many :container_volumes, :as => :parent, :dependent => :destroy has_many :persistent_volume_claim, :through => :container_volumes @@ -105,9 +104,6 @@ def disconnect_inv self.container_services = [] self.container_replicator_id = nil self.container_build_pod_id = nil - # Keeping old_container_project_id for backwards compatibility, we will need a migration that is putting it back to - # container_project_id - self.old_container_project_id = self.container_project_id self.deleted_on = Time.now.utc save end diff --git a/app/models/container_project.rb b/app/models/container_project.rb index 901a710b9e0..78ebee4fd06 100644 --- a/app/models/container_project.rb +++ b/app/models/container_project.rb @@ -23,7 +23,8 @@ class ContainerProject < ApplicationRecord has_many :container_limit_items, :through => :container_limits has_many :container_builds has_many :container_templates - has_many :archived_container_groups, :foreign_key => "old_container_project_id", :class_name => "ContainerGroup" + has_many :all_container_groups, :class_name => "ContainerGroup", :inverse_of => :container_project + has_many :archived_container_groups, -> { archived }, :class_name => "ContainerGroup" has_many :persistent_volume_claims has_many :miq_alert_statuses, :as => :resource, :dependent => :destroy has_many :computer_systems, :through => :container_nodes @@ -51,10 +52,6 @@ class ContainerProject < ApplicationRecord delegate :my_zone, :to => :ext_management_system, :allow_nil => true - def all_container_groups - ContainerGroup.where(:container_project_id => id).or(ContainerGroup.where(:old_container_project_id => id)) - end - def event_where_clause(assoc = :ems_events) case assoc.to_sym when :ems_events, :event_streams diff --git a/spec/models/container_project/purging_spec.rb b/spec/models/container_project/purging_spec.rb index 1b116c0a308..f99b2dca201 100644 --- a/spec/models/container_project/purging_spec.rb +++ b/spec/models/container_project/purging_spec.rb @@ -22,25 +22,26 @@ context ".purge" do let(:deleted_date) { 6.months.ago } + let(:new_container_project) { FactoryBot.create(:container_project, :deleted_on => deleted_date + 1.day) } before do - @old_container_project = FactoryBot.create(:container_project, :deleted_on => deleted_date - 1.day) - @purge_date_container_project = FactoryBot.create(:container_project, :deleted_on => deleted_date) - @new_container_project = FactoryBot.create(:container_project, :deleted_on => deleted_date + 1.day) + FactoryBot.create(:container_project, :deleted_on => deleted_date - 1.day) + FactoryBot.create(:container_project, :deleted_on => deleted_date) + new_container_project end def assert_unpurged_ids(unpurged_ids) - expect(described_class.order(:id).pluck(:id)).to eq(Array(unpurged_ids).sort) + expect(described_class.order(:id).pluck(:id)).to match_array(unpurged_ids.sort) end it "purge_date and older" do described_class.purge(deleted_date) - assert_unpurged_ids(@new_container_project.id) + assert_unpurged_ids([new_container_project.id]) end it "with a window" do described_class.purge(deleted_date, 1) - assert_unpurged_ids(@new_container_project.id) + assert_unpurged_ids([new_container_project.id]) end end end diff --git a/spec/models/container_project_spec.rb b/spec/models/container_project_spec.rb index ddac59cf499..cca24282d6a 100644 --- a/spec/models/container_project_spec.rb +++ b/spec/models/container_project_spec.rb @@ -2,4 +2,35 @@ subject { FactoryBot.create(:container_project) } include_examples "MiqPolicyMixin" + + describe "#all_container_projects" do + it "returns active and archived records" do + project = FactoryBot.create(:container_project) + group_archived = FactoryBot.create(:container_group, :container_project => project, :deleted_on => 1.day.ago.utc) + group_active = FactoryBot.create(:container_group, :container_project => project) + + expect(project.all_container_groups).to match_array([group_archived, group_active]) + end + end + + describe "#archived_container_projects" do + it "returns archived records" do + project = FactoryBot.create(:container_project) + group_archived = FactoryBot.create(:container_group, :container_project => project, :deleted_on => 1.day.ago.utc) + FactoryBot.create(:container_group, :container_project => project) + + expect(project.archived_container_groups).to match_array([group_archived]) + end + end + + describe "#container_projects" do + it "returns active records" do + project = FactoryBot.create(:container_project) + FactoryBot.create(:container_group, :container_project => project, :deleted_on => 1.day.ago.utc) + group_active = FactoryBot.create(:container_group, :container_project => project) + + groups = project.container_groups.to_a + expect(groups).to match_array([group_active]) + end + end end