Infos:
- Used Zammad version: 3.1.x
- Used Zammad installation source: https://dl.packager.io/srv/deb/zammad/zammad/stable/ubuntu 18.04 main
- Operating system: ubuntu server 18.04
- Browser + version: firefox 68.0.2 (32-Bit)
Expected behavior:
- i want to delete all inactive user with this:
# Actual deletion, requires overview run before
User.where(active: false).where.not(id: 1).find_each do |user|
puts "Customer #{user.login}/#{user.email}/#{user.id} has #{Ticket.where(customer_id: user.id).count} tickets"
puts " Removing references for user with E-Mail #{user.email}..."
ActivityStream.where(created_by_id: user.id).update_all(created_by_id: 1)
History.where(created_by_id: user.id).update_all(created_by_id: 1)
Ticket::Article.where(created_by_id: user.id).update_all(created_by_id: 1)
Ticket::Article.where(updated_by_id: user.id).update_all(updated_by_id: 1)
Store.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)
Tag.where(created_by_id: user.id).update_all(created_by_id: 1)
if OnlineNotification.find_by(user_id: user.id)==""
OnlineNotification.find_by(user_id: user.id).destroy!
end
puts " Deleting user #{user.login}/#{user.email}..."
user.destroy
end
worked fine for all inactive users except for one.
Actual behavior:
irb(main):021:0> User.where(active: false).where.not(id: 1).find_each do |user|
irb(main):022:1* puts "Customer #{user.login}/#{user.email}/#{user.id} has #{Ticket.where(customer_id: user.id).count} tickets"
irb(main):023:1>
irb(main):024:1> puts " Removing references for user with E-Mail #{user.email}..."
irb(main):025:1> ActivityStream.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):026:1> History.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):027:1> Ticket::Article.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):028:1> Ticket::Article.where(updated_by_id: user.id).update_all(updated_by_id: 1)
irb(main):029:1> Store.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):030:1> StatsStore.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):031:1> Tag.where(created_by_id: user.id).update_all(created_by_id: 1)
irb(main):032:1> if OnlineNotification.find_by(user_id: user.id)==""
irb(main):033:2> OnlineNotification.find_by(user_id: user.id).destroy!
irb(main):034:2> end
irb(main):035:1>
irb(main):036:1> puts " Deleting user #{user.login}/#{user.email}..."
irb(main):037:1> user.destroy
irb(main):038:1> end
Customer render.rodriguez@de/render.rodriguez@de/13 has 0 tickets
Removing references for user with E-Mail render.rodriguez@de...
Deleting user render.rodriguez@.de/render.rodriguez@de...
Traceback (most recent call last):
2: from (irb):21
1: from (irb):37:in `block in irb_binding'
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_f694a04b71" on table "tickets")
DETAIL: Key (id)=(13) is still referenced from table "tickets".
: DELETE FROM "users" WHERE "users"."id" = $1
The user has no tickets but the script claims that there are references to the ticket-table.
How to resolve this?
Thank you in advance.