Bug 1762346

Summary: Upgrade from 6.4 to 6.5 fails at db:migrate Due to 20190205142619_add_content_constraints Migration
Product: Red Hat Satellite Reporter: roarora
Component: Content ManagementAssignee: Partha Aji <paji>
Status: CLOSED ERRATA QA Contact: Lai <ltran>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 6.5.0CC: inecas, mbacovsk, mmccune
Target Milestone: 6.9.0Keywords: Triaged, Upgrades
Target Release: Unused   
Hardware: All   
OS: All   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2021-04-21 13:12:13 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description roarora 2019-10-16 15:10:23 UTC
Description of problem:

This was earlier faced during 6.3 to 6.4 upgrades and was fixed in Bugzilla https://bugzilla.redhat.com/show_bug.cgi?id=1684703 for satellite 6.4.3 
https://projects.theforeman.org/issues/26733

The change is not there in 6.5 and it is again affected in 6.4 to 6.5 upgrades probably when upgraded from 6.4.2 or lower 

Error : 

foreman-rake db:migrate
API controllers newer than Apipie cache! Run apipie:cache rake task to regenerate cache.
== 20190205142619 AddContentConstraints: migrating ============================
-- change_column(:katello_product_contents, :content_id, :integer, {:null=>false})
   -> 0.0066s
-- change_column(:katello_product_contents, :product_id, :integer, {:null=>false})
   -> 0.0016s
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::ForeignKeyViolation: ERROR:  update or delete on table "katello_contents" violates foreign key constraint "katello_product_content_content_id_fk" on table "katello_product_contents"
DETAIL:  Key (id)=(1457) is still referenced from table "katello_product_contents".
: DELETE FROM "katello_contents" WHERE "katello_contents"."id" = $1
 

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

The change needs to be there in the 6.5's katello too so that upgrades are not impacted

Comment 3 Bryan Kearney 2019-10-21 14:04:03 UTC
Upstream bug assigned to paji

Comment 4 Bryan Kearney 2019-10-21 14:04:05 UTC
Moving this bug to POST for triage into Satellite 6 since the upstream issue https://projects.theforeman.org/issues/26733 has been resolved.

Comment 6 Mike McCune 2020-10-29 21:01:39 UTC
.

Comment 8 Lai 2021-01-20 21:40:17 UTC
Steps to retest:

1. ssh into your satellite box
2. run: foreman-rake console
3. In foreman-rake console, run the following: (we are manipulating duplicating bad data and then running migration)
   a. #remove unique constraints since we are going to create duplicated data.
      >ActiveRecord::Migration.remove_index :katello_contents, :name => :katello_contents_cpcid_orgid_uniq
      >pc = Katello::ProdiuctContent.last
   b. # create duplicated data
      > content = pc.content.dup
      > content.save!
      > dup_pc = pc.dup
      > dup_pc.content = content
      > pc.save!
   c. # run migration
      > require '/opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.18.1.2/db/migrate/20190205142619_add_content_constraints.rb'
      > AddContentConstraints.new
      > AddContentConstraints.new.up

Expected result:
Should be successful and does not return error.

Actual:
Is successful and there's no error:

=> #<AddContentConstraints:0x000000001274df08 @name="AddContentConstraints", @version=nil, @connection=nil>
irb(main):014:0> AddContentConstraints.new.up
-- change_column(:katello_product_contents, :content_id, :integer, {:null=>false})
   -> 0.0239s
-- change_column(:katello_product_contents, :product_id, :integer, {:null=>false})
   -> 0.0049s
-- add_index(:katello_contents, [:cp_content_id, :organization_id], {:unique=>true, :name=>:katello_contents_cpcid_orgid_uniq})
   -> 0.0294s
=> #<PG::Result:0x00000000124e8218 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>
irb(main):015:0> require '/opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.18.1.2/db/migrate/20190205142619_add_content_constraints.rb'

Verified on 6.9.0_09

Comment 11 errata-xmlrpc 2021-04-21 13:12:13 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.9 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:1313