Actions
Bug #26733
closedForeign-key violation on upgrade - AddContentConstraints
Difficulty:
Triaged:
Yes
Bugzilla link:
Pull request:
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.
Updated by The Foreman Bot almost 6 years ago
- Status changed from New to Ready For Testing
- Pull request https://github.com/Katello/katello/pull/8096 added
Updated by Anonymous almost 6 years ago
- Status changed from Ready For Testing to Closed
Applied in changeset katello|6f45d27e46e226a07f40e0ffe2c9103114860269.
Updated by Jonathon Turel almost 6 years ago
- Target version set to Katello 3.10.2
- Triaged changed from No to Yes
Updated by Jonathon Turel over 5 years ago
- Target version changed from Katello 3.10.2 to Katello 3.12.1
Actions