Bug #36753
closedPG::UniqueViolation on index_cve_cp_id during registration
Description
I hit the following when registering a host AND setting a the lifecycle environment on the "Advanced" tab of Global Registration:
2023-09-15T20:17:26 [I|app|29348cd9] ContentViewEnvironment not found for content view 'Default Organization View' and environment 'Precipitation'; creating a new one.
2023-09-15T20:17:26 [W|app|29348cd9] Action failed
2023-09-15T20:17:26 [I|app|29348cd9] Backtrace for 'Action failed' error (ActiveRecord::RecordNotUnique): PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_cve_cp_id"
29348cd9 | DETAIL: Key (cp_id)=(64c2cce3716a5bf67b0413fac95e5daa) already exists.
29348cd9 |
29348cd9 | /home/vagrant/foreman/.vendor/ruby/2.7.0/gems/activerecord-6.1.7.6/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
...
29348cd9 | /home/vagrant/foreman/.vendor/ruby/2.7.0/gems/activerecord-6.1.7.6/lib/active_record/relation.rb:99:in `create'
29348cd9 | /home/vagrant/foreman/.vendor/ruby/2.7.0/gems/activerecord-6.1.7.6/lib/active_record/relation.rb:119:in `first_or_create'
29348cd9 | /home/vagrant/katello/app/models/katello/host/content_facet.rb:117:in `assign_single_environment'
29348cd9 | /home/vagrant/katello/app/controllers/katello/concerns/api/v2/registration_controller_extensions.rb:22:in `host_setup_extension'
29348cd9 | /home/vagrant/foreman/app/controllers/api/v2/registration_controller.rb:81:in `block in host'
The line of code in question is here: https://github.com/Katello/katello/commit/c33da7a3fa9473add44a154feba4e57f34b2b289#diff-ba6065a40faacc57174e6322ee3eab19b4771a0e9cf91efdc6b60d9f8b951646R101
In my database, I see:
[2] pry(main)> ::Katello::ContentViewEnvironment.where(cp_id: '64c2cce3716a5bf67b0413fac95e5daa')
=> [#<Katello::ContentViewEnvironment:0x000055b36b48b678
id: 1,
name: "Library",
label: "Library",
cp_id: "64c2cce3716a5bf67b0413fac95e5daa",
content_view_id: 1,
created_at: Wed, 23 Aug 2023 19:53:27.763161000 UTC +00:00,
updated_at: Wed, 23 Aug 2023 19:53:27.763161000 UTC +00:00,
environment_id: 1,
content_view_version_id: 1>]
Registering the host works only if I don't specify the lifecycle environment.
Tested on master as of Sept 15, 2023