Skip to content

Commit

Permalink
Remove ContainerGroup#old_container_project_id
Browse files Browse the repository at this point in the history
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:
- f6fe251
- #16302
  • Loading branch information
kbrock committed Jul 11, 2023
1 parent eacae91 commit c63bf80
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
1 change: 0 additions & 1 deletion app/models/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions app/models/container_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions app/models/container_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions spec/models/container_project/purging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/models/container_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c63bf80

Please sign in to comment.