Project

General

Profile

Actions

Bug #26733

closed

Foreign-key violation on upgrade - AddContentConstraints

Added by Partha Aji almost 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Target version:
Difficulty:
Triaged:
Yes
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.

Actions #1

Updated by Partha Aji almost 5 years ago

  • Assignee set to Partha Aji
Actions #2

Updated by Partha Aji almost 5 years ago

  • Bugzilla link set to 1669186
Actions #3

Updated by The Foreman Bot almost 5 years ago

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

Updated by Anonymous almost 5 years ago

  • Status changed from Ready For Testing to Closed
Actions #5

Updated by Jonathon Turel almost 5 years ago

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

Updated by Jonathon Turel almost 5 years ago

  • Target version changed from Katello 3.10.2 to Katello 3.12.1
Actions

Also available in: Atom PDF