Bug #30536
closedupdating hosts policy using bulk action fails with sql error
Description
Description of problem:
Add same policy twice to any host using bulk action fails with sql error, here is traceback
~~~
2020-07-30T10:02:15 [I|app|7234a419] Started POST "/compliance/policies/update_multiple_hosts?host_ids%5B%5D=4&host_ids%5B%5D=1" for 127.0.0.1 at 2020-07-30 10:02:15 -0400
2020-07-30T10:02:15 [I|app|7234a419] Processing by PoliciesController#update_multiple_hosts as HTML
2020-07-30T10:02:15 [I|app|7234a419] Parameters: {"utf8"=>"✓", "authenticity_token"=>"AsoqhvaapVoYTBwMLIox+2ICKHwvMRV3jjSP0T103oDAJKw954AF0pszr5CazvCF26e9ttVLQUIplONSx6IkGA==", "policy"=>{"id"=>"1"}, "host_ids"=>["4", "1"]}
2020-07-30T10:02:17 [D|tax|7234a419] Current location set to Default Location
2020-07-30T10:02:17 [D|tax|7234a419] Current organization set to Default Organization
2020-07-30T10:02:17 [W|app|7234a419] Action failed
2020-07-30T10:02:17 [D|app|7234a419] Backtrace for 'Action failed' error (ActiveRecord::RecordNotUnique): PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_scaptimony_asset_policies_on_asset_id_and_policy_id"
DETAIL: Key (asset_id, policy_id)=(8, 1) already exists.
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql_adapter.rb:675:in `async_exec_params'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql_adapter.rb:675:in `block (2 levels) in exec_no_cache'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.1/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.1/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.3.1/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql_adapter.rb:674:in `block in exec_no_cache'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'
~~~
How reproducible: Always
Steps to Reproduce:
1. Go To "All Hosts"
2. select few Hosts -> Click on "Select Action" on top right and click on "Assign Compliance Policy"
3. Select First Policy and click on submit
4. Repeat same step i.e select same hosts and try to assign same policy
Actual results: It should not fails with sql error.
Expected results: It should ignore the host if same epolicy is already assign