###Infos:
- Used Zammad version: 3.4
- Used Zammad installation source: package
- Operating system: Ubuntu 18.04
- Browser + version: Firefox
Expected behavior:
I only just started with Zammad recently and created a couple of dummy customers. I’m now trying to remove them with the scripts provided in your documentation.
Actual behavior:
I now came accross a customer that I can’t seem to delete. The special thing about this customer, in comparison to all others, is that this one has an invalid email address. Though I’m not sure this is the reason for the issue.
Steps to reproduce the behavior:
irb(main):103:0> User.find(5)
=> #<User id: 5, organization_id: nil, login: "max@muster.ch", firstname: "Max", lastname: "Muster", email: "max@muster.ch", image: nil, image_source: nil, web: "", password: "$argon2id$v=19$m=65536,t=2,p=1$sRt5/oO6uwCb7yE1f9P...", phone: "", fax: "", mobile: "", department: "", street: "", zip: "", city: "", country: "", address: "", vip: false, verified: false, active: true, note: "", last_login: nil, source: "signup", login_failed: 0, out_of_office: false, out_of_office_start_at: nil, out_of_office_end_at: nil, out_of_office_replacement_id: nil, preferences: {"tickets_closed"=>0, "tickets_open"=>1, "mail_delivery_failed"=>true, "mail_delivery_failed_data"=>Sun, 30 Aug 2020 13:06:15 UTC +00:00}, updated_by_id: 3, created_by_id: 1, created_at: "2020-08-27 21:55:51", updated_at: "2020-08-30 18:47:46">
irb(main):104:0> customer_emails = %w[max@muster.ch]
=> ["max@muster.ch"]
irb(main):105:0> customers = User.joins(roles: :permissions).where(email: customer_emails, roles: { active: true }, permissions: { name: 'ticket.customer', active: true }).where.not(id: 1)
=> #<ActiveRecord::Relation [#<User id: 5, organization_id: nil, login: "max@muster.ch", firstname: "Max", lastname: "Muster", email: "max@muster.ch", image: nil, image_source: nil, web: "", password: "$argon2id$v=19$m=65536,t=2,p=1$sRt5/oO6uwCb7yE1f9P...", phone: "", fax: "", mobile: "", department: "", street: "", zip: "", city: "", country: "", address: "", vip: false, verified: false, active: true, note: "", last_login: nil, source: "signup", login_failed: 0, out_of_office: false, out_of_office_start_at: nil, out_of_office_end_at: nil, out_of_office_replacement_id: nil, preferences: {"tickets_closed"=>0, "tickets_open"=>1, "mail_delivery_failed"=>true, "mail_delivery_failed_data"=>Sun, 30 Aug 2020 13:06:15 UTC +00:00}, updated_by_id: 3, created_by_id: 1, created_at: "2020-08-27 21:55:51", updated_at: "2020-08-30 18:47:46">]>
irb(main):106:0> puts customers.map { |user| <<~PREVIEW }.join("\n")
irb(main):107:1" Customer #{user.fullname}/#{user.id}/#{user.email} has #{Ticket.where(customer_id: user.id).count} tickets #{Ticket.where(customer_id: user.id).pluck(:number)}
irb(main):108:1" PREVIEW
Customer Max Muster/5/max@muster.ch has 0 tickets []
=> nil
irb(main):109:0> customers.find_each do |user|
irb(main):110:1* puts %{Preparing deletion of customer "#{user.fullname}" (and #{Ticket.where(customer_id: user.id).count} associated tickets)}
irb(main):111:1>
irb(main):112:1> Ticket.where(customer: user).find_each do |ticket|
irb(main):113:2* puts " Deleting ticket ##{ticket.number}..."
irb(main):114:2> ticket.destroy
irb(main):115:2> end
irb(main):116:1>
irb(main):117:1> puts " Removing references for user with email #{user.email}..."
irb(main):118:1> ActivityStream.where(created_by_id: user.id).update_all(created_by_id: 1)
StatsStore.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):119:1> History.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):120:1> Ticket::Article.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):121:1> Ticket::Article.where(updated_by_id: user.id).update_all(updated_by_id: 1)
irb(main):122:1> Store.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):123:1> StatsStore.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):124:1> Tag.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):125:1> OnlineNotification.find_by(user_id: user.id)&.destroy!
endirb(main):126:1>
irb(main):127:1> puts " Deleting #{user.fullname}..."
irb(main):128:1> user.destroy
irb(main):129:1> end
Preparing deletion of customer "Max Muster" (and 0 associated tickets)
Removing references for user with email max@muster.ch...
Deleting Max Muster...
Traceback (most recent call last):
2: from (irb):109
1: from (irb):128:in `block in irb_binding'
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_355a7ffe95" on table "users")
DETAIL: Key (id)=(5) is still referenced from table "users".
: DELETE FROM "users" WHERE "users"."id" = $1