Bug 1954021 - Red Hat Satellite 6.8 minor upgrade fails with error Validation failed: Name has already been taken at db:seed stage
Summary: Red Hat Satellite 6.8 minor upgrade fails with error Validation failed: Name ...
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Satellite
Classification: Red Hat
Component: Reporting
Version: 6.8.0
Hardware: Unspecified
OS: Unspecified
high
high
Target Milestone: 6.10.0
Assignee: Tomer Brisker
QA Contact: Lukáš Hellebrandt
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2021-04-27 13:03 UTC by Satyajit Das
Modified: 2022-07-27 15:00 UTC (History)
11 users (show)

Fixed In Version: foreman-2.5.0
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2021-11-16 14:10:44 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Foreman Issue Tracker 32657 0 High New Red Hat Satellite 6.8 minor upgrade fails with error Validation failed: Name has already been taken at db:seed stage 2021-05-25 12:40:00 UTC
Red Hat Knowledge Base (Solution) 5943391 0 None None None 2021-05-28 11:16:16 UTC
Red Hat Product Errata RHSA-2021:4702 0 None None None 2021-11-16 14:10:50 UTC

Description Satyajit Das 2021-04-27 13:03:49 UTC
Description of problem:

Red Hat Satellite 6.8 minor upgrade fails with error Validation failed: Name has already been taken at db:migrate state


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

6.8.z


Steps to Reproduce:

=> Execute the command to do a minor update 

# satellite-maintain upgrade run --target-version 6.8.z

Actual results:


=> The satellite installer fails with the below error:-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2021-04-27 00:15:44 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:seed]/Exec[foreman-rake-db:seed]: Failed to call refresh: '/usr/sbin/foreman-rake db:seed' returned 1 instead of one of [0]
2021-04-27 00:15:44 [ERROR ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:seed]/Exec[foreman-rake-db:seed]: '/usr/sbin/foreman-rake db:seed' returned 1 instead of one of [0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expected results:

 The upgrade should be completed.


Additional info:

The issue seems to be with duplicate templete.

Workaround to fix the issue:-
============================
=> Execute the query to list the templates:-

# su - postgres -c "echo \"SELECT a.* FROM templates a JOIN (SELECT name, COUNT(*) FROM templates GROUP BY name HAVING count(*) > 1 ) b ON a.name = b.name ORDER BY a.name;\" | psql foreman"


=> Identify the duplicate template:-

# awk -F\| '{print $1, $2, $8, $11, $6}' '/home/sadas/Downloads/foreman.txt' | grep "\S"
 id           name           locked           type                   created_at         
-----+---------------------+------------------------------------------------------------+    
 297   Host - Last Checkin   t        ReportTemplate         2021-04-26 21:03:44.527975   ===> The issue seems to be with these duplicate templates(id 297 and 296).
 296   Host - Last Checkin   t        ReportTemplate         2021-04-26 21:03:44.508732 
  59   Jumpstart default     t        Ptable                 2017-08-08 07:47:49.520343 
  21   Jumpstart default     t        ProvisioningTemplate   2017-08-08 07:47:48.798974 
  61   Kickstart default     t        Ptable                 2017-08-08 07:47:49.549152 
 206   Kickstart default     t        ProvisioningTemplate   2020-01-29 09:00:56.526946 
  30   Preseed default       t        ProvisioningTemplate   2017-08-08 07:47:48.945963 
  62   Preseed default       t        Ptable                 2017-08-08 07:47:49.563543
------------+--------------------+----------------------------+-------------------------+   
=> Execute the command to unlock the template:-  // Take a snapshot only then proceed with the next steps.

# su - postgres -c "echo \"update templates set locked = 'f' where id='297';\" | psql foreman"     


=> Run the following and verify the output shows one of the entities of "Host - Last Checkin"

# su - postgres -c "echo \"select id, name, type from templates where id='297';\" | psql foreman"     

If looks okay, then rename the duplicate using the below query:-

# su - postgres -c "echo \"update templates set name = 'Host - Last Checkin 2' where id='297';\" | psql foreman"

Following should now have named as "Host - Last Checkin 2"

# su - postgres -c "echo \"select id, name, type from templates where id='297';\" | psql foreman"

Then rerun the satellite-installer to complete the minor update.

Comment 3 Tomer Brisker 2021-05-25 12:39:10 UTC
Looks like there is no unique constraint in the DB for template names, meaning that race condition could could duplicate templates to be created, leading to this issue on the next template seeding that attempts to save one of the duplicated templates. The same is true for other template kinds as well, not just report templates.

Comment 4 Tomer Brisker 2021-05-25 12:39:58 UTC
Created redmine issue https://projects.theforeman.org/issues/32657 from this bug

Comment 7 Bryan Kearney 2021-05-28 10:21:53 UTC
Moving this bug to POST for triage into Satellite since the upstream issue https://projects.theforeman.org/issues/32657 has been resolved.

Comment 8 Lukáš Hellebrandt 2021-06-17 15:04:38 UTC
Verified with Sat 6.10.0 snap 4.0.

# find / -name 20210525144427_enforce_unique_templates.rb
/usr/share/foreman/migrate/20210525144427_enforce_unique_templates.rb
# satellite-maintain upgrade run --target-version 6.10.z
[...]
# su - postgres -c "echo \"SELECT a.id, a.name, a.locked, a.type, a.created_at FROM templates a JOIN (SELECT name, COUNT(*) FROM templates GROUP BY name HAVING count(*) > 1 ) b ON a.name = b.name ORDER BY a.name;\" | psql foreman"
 id  |       name        | locked |         type         |         created_at         
-----+-------------------+--------+----------------------+----------------------------
  50 | Jumpstart default | t      | ProvisioningTemplate | 2021-06-10 13:32:15.625061
 117 | Jumpstart default | t      | Ptable               | 2021-06-10 13:32:17.464938
  52 | Kickstart default | t      | ProvisioningTemplate | 2021-06-10 13:32:15.704209
 121 | Kickstart default | t      | Ptable               | 2021-06-10 13:32:17.561998
  54 | Preseed default   | t      | ProvisioningTemplate | 2021-06-10 13:32:15.757862
 125 | Preseed default   | t      | Ptable               | 2021-06-10 13:32:17.656894
(6 rows)

There are some templates with duplicate name but different type, I suppose it's expected (Tomer, can you confirm?). This BZ has component Reporting and there are no duplicate report templates => Verified.

Comment 9 Tomer Brisker 2021-06-17 15:25:36 UTC
That's expected, the unique constraint is for [name, type] pairs.

Comment 13 errata-xmlrpc 2021-11-16 14:10:44 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.10 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-2021:4702


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