Bug #32498
closedKatello 3.16 to 3.17 upgrade fails at the db:migrate stage with error "ERROR: insert or update on table "katello_hostgroup_content_facets" violates foreign key constraint"
Description
Cloned from https://bugzilla.redhat.com/show_bug.cgi?id=1955397
Description of problem:
While attempting to upgrade Katello, It fails at the db:migrate stage with either of these two errors.
2021-04-28 11:23:10 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: PG::ForeignKeyViolation: ERROR: insert or update on table "katello_hostgroup_content_facets" violates foreign key constraint "fk_rails_9cae1dc9a3"
2021-04-28 11:23:10 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: DETAIL: Key (content_view_id)=(4) is not present in table "katello_content_views".
or,
2021-04-26 15:06:49 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: PG::ForeignKeyViolation: ERROR: insert or update on table "katello_hostgroup_content_facets" violates foreign key constraint "fk_rails_159087f51c"
2021-04-26 15:06:49 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: DETAIL: Key (lifecycle_environment_id)=(9) is not present in table "katello_environments".
Version-Release number of selected component (if applicable):
Katello 3.16 [ 3.16 to 3.17 upgrade ]
How reproducible:
Under specific circumstances i.e. the missing foreign key constraints might be the cause and somehow it bypassed the "dependent:" constraint in rails
Steps to reproduce:
1. On Katello 3.16 with CV\LCE\Hostgroups preconfigured
2. Associate a hostgroup with one CV and LCE
3. Find a way to delete that LCE or CV so that they get removed from their respective tables but the hostgroups table in DB still holds the association.
Example:
For CV --> Katello::ContentView.where(id: <cv id>).delete_all
or,
For LCE --> Katello::KTEnvironment.where(id: <lce id>).delete_all
4. Perform the upgrade to 6.9
Actual Results:
db:migrate stage will fail at following
2021-04-28 11:23:10 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: PG::ForeignKeyViolation: ERROR: insert or update on table "katello_hostgroup_content_facets" violates foreign key constraint "fk_rails_9cae1dc9a3"
2021-04-28 11:23:10 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: DETAIL: Key (content_view_id)=(4) is not present in table "katello_content_views".
or,
2021-04-26 15:06:49 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: PG::ForeignKeyViolation: ERROR: insert or update on table "katello_hostgroup_content_facets" violates foreign key constraint "fk_rails_159087f51c"
2021-04-26 15:06:49 [INFO ] [configure] /Stage[main]/Foreman::Database/Foreman::Rake[db:migrate]/Exec[foreman-rake-db:migrate]/returns: DETAIL: Key (lifecycle_environment_id)=(9) is not present in table "katello_environments".
Expected results:
Before the db:migrate happens, these inconsistencies\stale associations should be removed from hostgroups table and then the db:migrate should be executed.
Additional Information:
Manual Solution present in https://access.redhat.com/solutions/6003311