2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-05-08 10:10:19 +00:00
|
|
|
class Issue1977RemoveInvalidUserForeignKeys < ActiveRecord::Migration[5.1]
|
|
|
|
def change
|
|
|
|
|
|
|
|
# return if it's a new setup
|
2020-08-03 08:35:43 +00:00
|
|
|
return if !Setting.exists?(name: 'system_init_done')
|
2018-05-08 10:10:19 +00:00
|
|
|
|
|
|
|
# cleanup
|
|
|
|
OnlineNotification.joins('LEFT OUTER JOIN users ON online_notifications.user_id = users.id')
|
2020-12-10 09:13:57 +00:00
|
|
|
.where('users.id' => nil)
|
2018-05-08 10:10:19 +00:00
|
|
|
.destroy_all
|
|
|
|
|
|
|
|
RecentView.joins('LEFT OUTER JOIN users ON recent_views.created_by_id = users.id')
|
2020-12-10 09:13:57 +00:00
|
|
|
.where('users.id' => nil)
|
2018-05-08 10:10:19 +00:00
|
|
|
.destroy_all
|
|
|
|
|
|
|
|
Avatar.joins('LEFT OUTER JOIN users ON avatars.o_id = users.id')
|
2020-12-10 09:13:57 +00:00
|
|
|
.where('users.id' => nil)
|
2018-05-08 11:29:43 +00:00
|
|
|
.where(object_lookup_id: ObjectLookup.by_name('User'))
|
2018-05-08 10:10:19 +00:00
|
|
|
.destroy_all
|
|
|
|
|
|
|
|
# add (possibly) missing foreign_key
|
|
|
|
foreign_keys = [
|
|
|
|
%i[online_notifications users],
|
|
|
|
]
|
|
|
|
|
|
|
|
foreign_keys.each do |args|
|
2019-06-27 18:26:28 +00:00
|
|
|
|
|
|
|
add_foreign_key(*args)
|
|
|
|
rescue ActiveRecord::StatementInvalid => e
|
|
|
|
Rails.logger.info "Can't add foreign_keys '#{args.inspect}'"
|
|
|
|
Rails.logger.info e
|
|
|
|
ActiveRecord::Base.connection.reconnect!
|
|
|
|
|
2018-05-08 10:10:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|