Bug 2063190 - Upgrading from Satellite 6.9 to Satellite 6.10.3 fails with error "undefined method operatingsystems' for nil:NilClass" during the db:migrate step
Summary: Upgrading from Satellite 6.9 to Satellite 6.10.3 fails with error "undefined ...
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Satellite
Classification: Red Hat
Component: Registration
Version: 6.10.3
Hardware: All
OS: Unspecified
medium
medium
Target Milestone: 6.11.0
Assignee: satellite6-bugs
QA Contact: Stephen Wadeley
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2022-03-11 13:50 UTC by Sayan Das
Modified: 2022-07-06 14:39 UTC (History)
5 users (show)

Fixed In Version: foreman-3.1.3,foreman-3.1.1.10-1
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 2070985 (view as bug list)
Environment:
Last Closed: 2022-07-05 14:34:27 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Foreman Issue Tracker 34661 0 Normal Closed Registration migration with nil template 2022-03-23 15:03:05 UTC
Red Hat Product Errata RHSA-2022:5498 0 None None None 2022-07-05 14:34:38 UTC

Description Sayan Das 2022-03-11 13:50:07 UTC
Description of problem:

Upgrading from Satellite 6.9 to Satellite 6.10.3 fails with error "undefined method operatingsystems' for nil:NilClass" during the db:migrate step i.e. while applying the "20210115124508 TemplateKindRegistration" migration.


Version-Release number of selected component (if applicable):

Red Hat Satellite 6.10.3

foreman-2.5.2.19-1


How reproducible:

In Customer's environment and hence no definite reproducer present


Steps to Reproduce:
1. Try to upgrade a Satellite 6.9 to 6.10.3 
2.
3.

Actual results:


** Attemp to upgrade fails on this stage:

2022-03-10 11:20:18 [NOTICE] [configure] 1000 configuration steps out of 2118 steps complete.
2022-03-10 11:20:19 [NOTICE] [configure] 1250 configuration steps out of 2147 steps complete.
2022-03-10 11:20:35 [NOTICE] [configure] 1500 configuration steps out of 2148 steps complete.
2022-03-10 11:21:33 [ERROR ] [configure] '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]
2022-03-10 11:21:33 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: change from 'notrun' to ['0'] failed: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]
2022-03-10 11:22:22 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]: Failed to call refresh: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]
2022-03-10 11:22:22 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]: '/usr/sbin/foreman-rake db:migrate' returned 1 instead of one of [0]
2022-03-10 11:22:47 [NOTICE] [configure] 1750 configuration steps out of 2148 steps complete.
2022-03-10 11:23:04 [NOTICE] [configure] 2000 configuration steps out of 2148 steps complete.
2022-03-10 11:23:59 [NOTICE] [configure] System configuration has finished.



** db:migrate specifically fails to apply  the "20210115124508 TemplateKindRegistration" migration. 

[root@nsatellite ~]# foreman-rake db:migrate --trace --verbose
'ErbParser' is ignored.
'RubyParser' is ignored.
** Invoke db:migrate (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
API controllers newer than Apipie cache! Run apipie:cache rake task to regenerate cache.
** Execute db:load_config
** Invoke plugin:refresh_migrations (first_time)
** Invoke environment
** Execute plugin:refresh_migrations
** Execute db:migrate
== 20210115124508 TemplateKindRegistration: migrating =========================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
 
undefined method operatingsystems' for nil:NilClass 
/usr/share/foreman/db/migrate/20210115124508_template_kind_registration.rb:26:in block in up'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/relation/delegation.rb:87:in each' /opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/relation/delegation.rb:87:in each'
/usr/share/foreman/db/migrate/20210115124508_template_kind_registration.rb:25:in up' /opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:831:in exec_migration'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:812:in block (2 levels) in migrate' /opt/rh/rh-ruby27/root/usr/share/ruby/benchmark.rb:293:in measure'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:811:in block in migrate' /opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:471:in with_connection'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:810:in migrate' /opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:1002:in migrate'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:1310:in block in execute_migration_in_transaction' /opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.7/lib/active_record/migration.rb:1361:in block in ddl_transaction'

/opt/rh/rh-ruby27/root/usr/bin/rake:23:in `<main>'


Expected results:

No such errors.


Additional info:

* It seems related to the latest review done on the PR https://github.com/theforeman/foreman/pull/8256#discussion_r824099694

* It was worked around by the following change (i.e insert "return unless template" after line 24 ) and retrying the db:migrate again.

# git diff
diff --git a/20210115124508_template_kind_registration.rb b/20210115124508_template_kind_registration.rb
index 5a298c0..dbdc9a4 100644
--- a/20210115124508_template_kind_registration.rb
+++ b/20210115124508_template_kind_registration.rb
@@ -22,6 +22,7 @@ class TemplateKindRegistration < ActiveRecord::Migration[6.0]
     # Assign default host_init_config template to all operating systems
     # and change registration association to the host_init_config
     template = ProvisioningTemplate.unscoped.find_by_name(Setting[:default_host_init_config_template])
+    return unless template
     Operatingsystem.all.each do |os|
       template.operatingsystems << os unless template.operatingsystems.include?(os)

Comment 3 Leos Stejskal 2022-03-21 13:05:31 UTC
Yes,
I'll take a look at it

Comment 4 Leos Stejskal 2022-03-22 08:29:38 UTC
Created redmine issue https://projects.theforeman.org/issues/34661 from this bug

Comment 6 Bryan Kearney 2022-03-22 16:05:19 UTC
Moving this bug to POST for triage into Satellite since the upstream issue https://projects.theforeman.org/issues/34661 has been resolved.

Comment 16 errata-xmlrpc 2022-07-05 14:34:27 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: Satellite 6.11 Release), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2022:5498


Note You need to log in before you can comment on or make changes to this bug.