Bug #4641

Deleting user with associated roles triggers PG::NotNullViolation

Added by Dominic Cleal almost 4 years ago. Updated over 3 years ago.

Status:Closed
Priority:Normal
Assigned To:Maria Nita
Category:Database
Target version:Sprint 26
Difficulty: Bugzilla link:
Found in release: Pull request:https://github.com/theforeman/foreman/pull/1371
Story points-
Velocity based estimate-
Release1.6.0Release relationshipAuto

Description

When deleting a user with associated roles, a PG::NotNullViolation is logged.

Started DELETE "/users/5-manager" for 127.0.0.1 at 2014-03-12 11:14:16 +0000
Processing by UsersController#destroy as HTML
  Parameters: {"authenticity_token"=>"wzjXWaEKXmEYUJ6ROjqfRjWmyUJzUzbcnppgD9PtJ90=", "id"=>"5-manager"}
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Setting current user thread-local variable to admin
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY firstname LIMIT 1  [["id", "5-manager"]]
   (0.2ms)  BEGIN
  TaxableTaxonomy Load (0.4ms)  SELECT "taxable_taxonomies".* FROM "taxable_taxonomies" WHERE "taxable_taxonomies"."taxable_id" = 5 AND "taxable_taxonomies"."taxable_type" = 'User'
  CACHE (0.0ms)  SELECT "taxable_taxonomies".* FROM "taxable_taxonomies" WHERE "taxable_taxonomies"."taxable_id" = 5 AND "taxable_taxonomies"."taxable_type" = 'User'
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 ORDER BY firstname LIMIT 1
   (0.5ms)  SELECT MAX("audits"."version") AS max_id FROM "audits" WHERE "audits"."auditable_id" = 5 AND "audits"."auditable_type" = 'User'
  SQL (1.4ms)  INSERT INTO "audits" ("action", "associated_id", "associated_name", "associated_type", "auditable_id", "auditable_name", "auditable_type", "audited_changes", "comment", "created_at", "remote_address", "user_id", "user_type", "username", "version") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING "id"  [["action", "destroy"], ["associated_id", nil], ["associated_name", nil], ["associated_type", nil], ["auditable_id", 5], ["auditable_name", "Manager "], ["auditable_type", "User"], ["audited_changes", "---\nlogin: manager\nfirstname: Manager\nlastname: ''\nmail: manager@localhost\nadmin: false\nauth_source_id: 1\ndomains_andor: or\nhostgroups_andor: or\nfacts_andor: or\nfilter_on_owner: false\ncompute_resources_andor: or\norganizations_andor: or\nlocations_andor: or\nsubscribe_to_all_hostgroups: \nlocale: \navatar_hash: \n"], ["comment", nil], ["created_at", Wed, 12 Mar 2014 11:14:16 UTC +00:00], ["remote_address", "127.0.0.1"], ["user_id", nil], ["user_type", nil], ["username", "Admin User"], ["version", 2]]
  Host::Managed Load (1.1ms)  SELECT "hosts".* FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."owner_id" = 5 AND "hosts"."owner_type" = 'User'
  Hostgroup Load (1.4ms)  SELECT "hostgroups".* FROM "hostgroups" INNER JOIN "user_hostgroups" ON "hostgroups"."id" = "user_hostgroups"."hostgroup_id" WHERE "user_hostgroups"."user_id" = 5 ORDER BY hostgroups.title
  UsergroupMember Load (0.3ms)  SELECT "usergroup_members".* FROM "usergroup_members" WHERE "usergroup_members"."member_id" = 5 AND "usergroup_members"."member_type" = 'User'
  UserRole Load (0.2ms)  SELECT "user_roles".* FROM "user_roles" WHERE "user_roles"."owner_id" = 5 AND "user_roles"."owner_type" = 'User'
  CachedUserRole Load (0.3ms)  SELECT "cached_user_roles".* FROM "cached_user_roles" WHERE "cached_user_roles"."user_role_id" = 61
  SQL (0.6ms)  DELETE FROM "cached_user_roles" WHERE "cached_user_roles"."id" = $1  [["id", 21]]
  SQL (0.4ms)  DELETE FROM "user_roles" WHERE "user_roles"."id" = $1  [["id", 61]]
  CachedUserRole Load (0.3ms)  SELECT "cached_user_roles".* FROM "cached_user_roles" WHERE "cached_user_roles"."user_role_id" = 63
  SQL (0.2ms)  DELETE FROM "cached_user_roles" WHERE "cached_user_roles"."id" = $1  [["id", 22]]
  SQL (0.2ms)  DELETE FROM "user_roles" WHERE "user_roles"."id" = $1  [["id", 63]]
  CachedUserRole Load (0.3ms)  SELECT "cached_user_roles".* FROM "cached_user_roles" WHERE "cached_user_roles"."user_role_id" = 64
  SQL (0.2ms)  DELETE FROM "cached_user_roles" WHERE "cached_user_roles"."id" = $1  [["id", 23]]
  SQL (0.2ms)  DELETE FROM "user_roles" WHERE "user_roles"."id" = $1  [["id", 64]]
  Role Load (0.4ms)  SELECT "roles".* FROM "roles" INNER JOIN "user_roles" ON "roles"."id" = "user_roles"."role_id" WHERE "user_roles"."owner_id" = 5 AND "user_roles"."owner_type" = 'User'
  CachedUserRole Load (0.4ms)  SELECT "cached_user_roles".* FROM "cached_user_roles" WHERE "cached_user_roles"."user_id" = 5
  UserHostgroup Load (0.3ms)  SELECT "user_hostgroups".* FROM "user_hostgroups" WHERE "user_hostgroups"."user_id" = 5
  UserFact Load (0.3ms)  SELECT "user_facts".* FROM "user_facts" WHERE "user_facts"."user_id" = 5
  Domain Load (0.4ms)  SELECT "domains".* FROM "domains" INNER JOIN "user_domains" ON "domains"."id" = "user_domains"."domain_id" WHERE "user_domains"."user_id" = 5 ORDER BY domains.name
  ComputeResource Load (0.4ms)  SELECT "compute_resources".* FROM "compute_resources" INNER JOIN "user_compute_resources" ON "compute_resources"."id" = "user_compute_resources"."compute_resource_id" WHERE "user_compute_resources"."user_id" = 5 ORDER BY compute_resources.name
  Notice Load (0.4ms)  SELECT "notices".* FROM "notices" INNER JOIN "user_notices" ON "notices"."id" = "user_notices"."notice_id" WHERE "user_notices"."user_id" = 5
  SQL (1.2ms)  DELETE FROM "users" WHERE "users"."id" = $1  [["id", 5]]
   (4.4ms)  COMMIT
  Role Load (0.4ms)  SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'Anonymous' LIMIT 1
   (0.1ms)  BEGIN
  UserRole Exists (0.4ms)  SELECT 1 AS one FROM "user_roles" WHERE ("user_roles"."owner_id" = 5 AND "user_roles"."role_id" = 8 AND "user_roles"."owner_type" = 'User') LIMIT 1
  SQL (0.8ms)  INSERT INTO "user_roles" ("owner_id", "owner_type", "role_id") VALUES ($1, $2, $3) RETURNING "id"  [["owner_id", 5], ["owner_type", "User"], ["role_id", 8]]
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 ORDER BY firstname LIMIT 1
  SQL (1.2ms)  INSERT INTO "cached_user_roles" ("created_at", "role_id", "updated_at", "user_id", "user_role_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", Wed, 12 Mar 2014 11:14:16 UTC +00:00], ["role_id", 8], ["updated_at", Wed, 12 Mar 2014 11:14:16 UTC +00:00], ["user_id", nil], ["user_role_id", 91]]
PG::NotNullViolation: ERROR:  null value in column "user_id" violates not-null constraint
: INSERT INTO "cached_user_roles" ("created_at", "role_id", "updated_at", "user_id", "user_role_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" 
   (0.1ms)  ROLLBACK
PG::NotNullViolation: ERROR:  null value in column "user_id" violates not-null constraint
: INSERT INTO "cached_user_roles" ("created_at", "role_id", "updated_at", "user_id", "user_role_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" 
Redirected to http://0.0.0.0:3000/users
Completed 302 Found in 354.6ms (ActiveRecord: 38.0ms)

(https://bugzilla.redhat.com/show_bug.cgi?id=1075550)


Related issues

Duplicated by Foreman - Bug #6361: menu item "Hosts --> All hosts" is visible to normal user... Closed 06/24/2014
Blocks Foreman - Tracker #4552: New permissions/authorization system issues New 03/05/2014

Associated revisions

Revision a0bd5bb2
Added by Maria Nita over 3 years ago

fixes #4641 - Ensure default role only after save/update. Change default role permissions

Revision 78551af8
Added by Maria Nita over 3 years ago

refs #4641 - update to functional test

History

#1 Updated by Dominic Cleal almost 4 years ago

  • Related to Tracker #4552: New permissions/authorization system issues added

#2 Updated by Dominic Cleal almost 4 years ago

  • Related to deleted (Tracker #4552: New permissions/authorization system issues)

#3 Updated by Dominic Cleal almost 4 years ago

  • Blocks Tracker #4552: New permissions/authorization system issues added

#4 Updated by Maria Nita almost 4 years ago

I tried to write a regression test case for this, but no luck.

This is the test https://github.com/marianitadn/foreman/blob/4641-delete-user/test/unit/user_test.rb#L341-L351, but it doesn't fail.

I think I have an idea for the solution, but without a test I can't be convinced.

#5 Updated by Dominic Cleal almost 4 years ago

I think this could be tricky to test, as the deletion and therefore your test are actually successful. The error seems to be coming from the "ensure_default_role" after_commit hook on the User model (thanks to Marek for pointing to this). Errors in here seem to be swallowed and are only hitting the logs. It looks like the test_after_commit gem would enable testing of it, if you want to try it out.

Probably setting the after_commit hook to run on create and update only would fix the issue.

#6 Updated by Dominic Cleal almost 4 years ago

  • Status changed from New to Ready For Testing
  • Assigned To set to Maria Nita
  • Target version set to Sprint 22

#7 Updated by Dmitri Dolguikh almost 4 years ago

  • Target version changed from Sprint 22 to Sprint 23

#8 Updated by Dmitri Dolguikh almost 4 years ago

  • Target version changed from Sprint 23 to Sprint 22

#9 Updated by Dmitri Dolguikh almost 4 years ago

  • Target version changed from Sprint 22 to Sprint 23

#10 Updated by Dmitri Dolguikh over 3 years ago

  • Target version changed from Sprint 23 to Sprint 24

#11 Updated by Dmitri Dolguikh over 3 years ago

  • Target version changed from Sprint 24 to Sprint 25

#12 Updated by Dmitri Dolguikh over 3 years ago

  • Target version changed from Sprint 25 to Sprint 26

#13 Updated by The Foreman Bot over 3 years ago

  • Pull request https://github.com/theforeman/foreman/pull/1371 added

#14 Updated by Dominic Cleal over 3 years ago

  • Release set to 1.6.0

#15 Updated by Dominic Cleal over 3 years ago

  • Duplicated by Bug #6361: menu item "Hosts --> All hosts" is visible to normal user from anonymous role by default added

#16 Updated by Maria Nita over 3 years ago

  • Status changed from Ready For Testing to Closed
  • % Done changed from 0 to 100

Also available in: Atom PDF