Project

General

Profile

Bug #26733

Foreign-key violation on upgrade - AddContentConstraints

Added by Partha Aji about 1 year ago. Updated about 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Target version:
Difficulty:
Triaged:
Yes
Bugzilla link:
Fixed in Releases:
Found in Releases:

Description

Few users have hit the following error while upgrading

** Execute db:migrate
== 20190205142619 AddContentConstraints: migrating ============================
-- change_column(:katello_product_contents, :content_id, :integer, {:null=>false})
   -> 0.0038s
-- change_column(:katello_product_contents, :product_id, :integer, {:null=>false})
   -> 0.0018s
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)=(2403) is still referenced from table "katello_product_contents".
: DELETE FROM "katello_contents" WHERE "katello_contents"."id" = $1

It looks like users had dirty data before upgrading so we had to make the migration more robust. To simulate the bad data I had to do the following from the latest on rails console

$ cd <katello git dir>
$ git checkout master
$ cd <foreman git>
$ rails c
#  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 
# create duplicated data
> content = pc.content.dup
> content.save!
> dup_pc = pc.dup
> dup_pc.content = content
> pc.save!
# Now run the migration
> require '../katello/db/migrate/20190205142619_add_content_constraints.rb'
> AddContentConstraints.new.up

So this will create duplicated ProductContent and Content rows. We then run the following migration -> https://github.com/Katello/katello/blob/master/db/migrate/20190205142619_add_content_constraints.rb#L11-L25 the code should fail and report the above error.

Now check out this PR and rerun

$ rails c
> require '../katello/db/migrate/20190205142619_add_content_constraints.rb'
> AddContentConstraints.new.up

That should be successful.

Associated revisions

Revision 6f45d27e (diff)
Added by Partha Aji about 1 year ago

Fixes #26733 - Delete on bad content_constraints (#8096)

Updated add_content_constraints migration to properly delete the
associated product_content before deleting the content itself. The prior
migration code did not handle the case where 2 duplicated rows of
content pointed to the same product.

History

#1 Updated by Partha Aji about 1 year ago

  • Assignee set to Partha Aji

#2 Updated by Partha Aji about 1 year ago

  • Bugzilla link set to 1669186

#3 Updated by The Foreman Bot about 1 year ago

  • Status changed from New to Ready For Testing
  • Pull request https://github.com/Katello/katello/pull/8096 added

#4 Updated by Anonymous about 1 year ago

  • Status changed from Ready For Testing to Closed

#5 Updated by Jonathon Turel about 1 year ago

  • Triaged changed from No to Yes
  • Target version set to Katello 3.10.2

#6 Updated by Jonathon Turel about 1 year ago

  • Target version changed from Katello 3.10.2 to Katello 3.12.1

Also available in: Atom PDF