Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions db/migrate/20251229071811_remove_grub_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class RemoveGrubTemplate < ActiveRecord::Migration[7.0]
def up
template_kind = TemplateKind.find_by(name: 'PXEGrub')
return unless template_kind

template_kind.os_default_templates.destroy_all

templates = Template.where(template_kind_id: template_kind.id)
if templates.any?
template_ids = templates.pluck(:id)

# Delete template_combinations to bypass EnsureNotUsedBy(:hostgroups) callback
TemplateCombination.where(provisioning_template_id: template_ids).destroy_all

# Unlock templates to bypass check_if_template_is_locked callback
templates.update_all(locked: false)

templates.destroy_all
end

template_kind.destroy!
end

def down
raise ActiveRecord::IrreversibleMigration, "Cannot restore removed PXEGrub template"
end
end
109 changes: 109 additions & 0 deletions test/migrate/remove_pxe_grub_template_kind_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require 'test_helper'
require Rails.root.join('db/migrate/20251229071811_remove_grub_template.rb')

class RemoveGrubTemplateTest < ActiveSupport::TestCase
let(:migration) { RemoveGrubTemplate.new }

setup do
@pxe_grub_kind = TemplateKind.find_or_create_by!(name: 'PXEGrub')

@template = ProvisioningTemplate.create!(
name: 'Test PXEGrub Template',
template: 'test template content',
template_kind: @pxe_grub_kind,
snippet: false,
locked: true
)
end

test "removes PXEGrub template kind and all templates" do
assert TemplateKind.exists?(name: 'PXEGrub')
assert Template.exists?(id: @template.id)

migration.up

assert_not TemplateKind.exists?(name: 'PXEGrub')
assert_not Template.exists?(id: @template.id)
end

test "removes locked templates" do
assert @template.locked?

migration.up

assert_not Template.exists?(id: @template.id)
end

test "removes os_default_templates associated with PXEGrub kind" do
os = FactoryBot.create(:operatingsystem)
os_default = OsDefaultTemplate.create!(
operatingsystem: os,
template_kind: @pxe_grub_kind,
provisioning_template: @template
)

assert OsDefaultTemplate.exists?(id: os_default.id)

migration.up

assert_not OsDefaultTemplate.exists?(id: os_default.id)
assert Operatingsystem.exists?(id: os.id)
end

test "removes template_combinations (hostgroup associations)" do
hostgroup = FactoryBot.create(:hostgroup)
combination = TemplateCombination.create!(
provisioning_template: @template,
hostgroup: hostgroup
)

assert TemplateCombination.exists?(id: combination.id)

migration.up

assert_not TemplateCombination.exists?(id: combination.id)
assert_not Template.exists?(id: @template.id)
assert Hostgroup.exists?(id: hostgroup.id)
end

test "templates with hostgroup associations cannot be deleted without bypass" do
hostgroup = FactoryBot.create(:hostgroup)
combination = TemplateCombination.create!(
provisioning_template: @template,
hostgroup: hostgroup
)

assert TemplateCombination.exists?(id: combination.id)

assert_raises(ActiveRecord::RecordNotDestroyed) do
@template.destroy!
end

assert Template.exists?(id: @template.id)
assert TemplateCombination.exists?(id: combination.id)
end

test "removes multiple PXEGrub templates" do
template2 = ProvisioningTemplate.create!(
name: 'Test PXEGrub Template 2',
template: 'test content 2',
template_kind: @pxe_grub_kind,
snippet: false,
locked: true
)

assert_equal 2, Template.where(template_kind_id: @pxe_grub_kind.id).count

migration.up

assert_not Template.exists?(id: @template.id)
assert_not Template.exists?(id: template2.id)
assert_not TemplateKind.exists?(name: 'PXEGrub')
end

test "migration is irreversible" do
assert_raises(ActiveRecord::IrreversibleMigration) do
migration.down
end
end
end
Loading