2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2017-09-08 08:28:34 +00:00
|
|
|
class ForeignKeys < ActiveRecord::Migration[4.2]
|
2017-08-10 09:20:29 +00:00
|
|
|
disable_ddl_transaction!
|
|
|
|
|
2017-06-06 15:49:49 +00:00
|
|
|
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')
|
2017-06-06 15:49:49 +00:00
|
|
|
|
|
|
|
# remove wrong plural of ID columns
|
|
|
|
ActiveRecord::Migration.rename_column :ticket_flags, :tickets_id, :ticket_id
|
|
|
|
ActiveRecord::Migration.rename_column :ticket_article_flags, :ticket_articles_id, :ticket_article_id
|
|
|
|
|
|
|
|
# add missing foreign keys
|
|
|
|
foreign_keys = [
|
|
|
|
# Base
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[users organizations],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:users, :users, { column: :created_by_id }],
|
|
|
|
[:users, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:signatures, :users, { column: :created_by_id }],
|
|
|
|
[:signatures, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:email_addresses, :users, { column: :created_by_id }],
|
|
|
|
[:email_addresses, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[groups signatures],
|
|
|
|
%i[groups email_addresses],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:groups, :users, { column: :created_by_id }],
|
|
|
|
[:groups, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:roles, :users, { column: :created_by_id }],
|
|
|
|
[:roles, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:organizations, :users, { column: :created_by_id }],
|
|
|
|
[:organizations, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[roles_users users],
|
|
|
|
%i[roles_users roles],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[groups_users users],
|
|
|
|
%i[groups_users groups],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[organizations_users users],
|
|
|
|
%i[organizations_users organizations],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[authorizations users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:translations, :users, { column: :created_by_id }],
|
|
|
|
[:translations, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[tokens users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:packages, :users, { column: :created_by_id }],
|
|
|
|
[:packages, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[taskbars users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[tags tag_items],
|
|
|
|
%i[tags tag_objects],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:tags, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:recent_views, :object_lookups, { column: :recent_view_object_id }],
|
|
|
|
[:recent_views, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:activity_streams, :type_lookups, { column: :activity_stream_type_id }],
|
|
|
|
[:activity_streams, :object_lookups, { column: :activity_stream_object_id }],
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[activity_streams permissions],
|
|
|
|
%i[activity_streams groups],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:activity_streams, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[histories history_types],
|
|
|
|
%i[histories history_objects],
|
|
|
|
%i[histories history_attributes],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:histories, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[stores store_objects],
|
|
|
|
%i[stores store_files],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:stores, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:avatars, :users, { column: :created_by_id }],
|
|
|
|
[:avatars, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:online_notifications, :users, { column: :created_by_id }],
|
|
|
|
[:online_notifications, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:schedulers, :users, { column: :created_by_id }],
|
|
|
|
[:schedulers, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:calendars, :users, { column: :created_by_id }],
|
|
|
|
[:calendars, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[user_devices users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[object_manager_attributes object_lookups],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:object_manager_attributes, :users, { column: :created_by_id }],
|
|
|
|
[:object_manager_attributes, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[cti_caller_ids users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:stats_stores, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:http_logs, :users, { column: :created_by_id }],
|
|
|
|
[:http_logs, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
|
|
|
# Ticket
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_state_types, :users, { column: :created_by_id }],
|
|
|
|
[:ticket_state_types, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_states, :ticket_state_types, { column: :state_type_id }],
|
|
|
|
[:ticket_states, :users, { column: :created_by_id }],
|
|
|
|
[:ticket_states, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_priorities, :users, { column: :created_by_id }],
|
|
|
|
[:ticket_priorities, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[tickets groups],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:tickets, :users, { column: :owner_id }],
|
|
|
|
[:tickets, :users, { column: :customer_id }],
|
|
|
|
[:tickets, :ticket_priorities, { column: :priority_id }],
|
|
|
|
[:tickets, :ticket_states, { column: :state_id }],
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[tickets organizations],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:tickets, :users, { column: :created_by_id }],
|
|
|
|
[:tickets, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_flags, :tickets, { column: :ticket_id }],
|
|
|
|
[:ticket_flags, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_article_types, :users, { column: :created_by_id }],
|
|
|
|
[:ticket_article_types, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_article_senders, :users, { column: :created_by_id }],
|
|
|
|
[:ticket_article_senders, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[ticket_articles tickets],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_articles, :ticket_article_types, { column: :type_id }],
|
|
|
|
[:ticket_articles, :ticket_article_senders, { column: :sender_id }],
|
|
|
|
[:ticket_articles, :users, { column: :created_by_id }],
|
|
|
|
[:ticket_articles, :users, { column: :updated_by_id }],
|
|
|
|
[:ticket_articles, :users, { column: :origin_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_article_flags, :ticket_articles, { column: :ticket_article_id }],
|
|
|
|
[:ticket_article_flags, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[ticket_time_accountings tickets],
|
|
|
|
%i[ticket_time_accountings ticket_articles],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:ticket_time_accountings, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:overviews, :users, { column: :created_by_id }],
|
|
|
|
[:overviews, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[overviews_roles overviews],
|
|
|
|
%i[overviews_roles roles],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[overviews_users overviews],
|
|
|
|
%i[overviews_users users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[overviews_groups overviews],
|
|
|
|
%i[overviews_groups groups],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:triggers, :users, { column: :created_by_id }],
|
|
|
|
[:triggers, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:jobs, :users, { column: :created_by_id }],
|
|
|
|
[:jobs, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[links link_types],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:postmaster_filters, :users, { column: :created_by_id }],
|
|
|
|
[:postmaster_filters, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[text_modules users],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:text_modules, :users, { column: :created_by_id }],
|
|
|
|
[:text_modules, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[text_modules_groups text_modules],
|
|
|
|
%i[text_modules_groups groups],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[templates users],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:templates, :users, { column: :created_by_id }],
|
|
|
|
[:templates, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[templates_groups templates],
|
|
|
|
%i[templates_groups groups],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[channels groups],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:channels, :users, { column: :created_by_id }],
|
|
|
|
[:channels, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:slas, :users, { column: :created_by_id }],
|
|
|
|
[:slas, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:macros, :users, { column: :created_by_id }],
|
|
|
|
[:macros, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:chats, :users, { column: :created_by_id }],
|
|
|
|
[:chats, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[chat_sessions chats],
|
|
|
|
%i[chat_sessions users],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:chat_sessions, :users, { column: :created_by_id }],
|
|
|
|
[:chat_sessions, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[chat_messages chat_sessions],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:chat_messages, :users, { column: :created_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:chat_agents, :users, { column: :created_by_id }],
|
|
|
|
[:chat_agents, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2020-07-13 12:46:08 +00:00
|
|
|
[:report_profiles, :users, { column: :created_by_id }],
|
|
|
|
[:report_profiles, :users, { column: :updated_by_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[karma_users users],
|
2017-06-06 15:49:49 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
%i[karma_activity_logs users],
|
2020-07-13 12:46:08 +00:00
|
|
|
[:karma_activity_logs, :karma_activities, { column: :activity_id }],
|
2017-06-06 15:49:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
foreign_keys.each do |foreign_key|
|
2017-08-10 09:20:29 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2019-06-27 18:26:28 +00:00
|
|
|
|
|
|
|
add_foreign_key(*foreign_key)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "Inconsistent data status detected while adding foreign key '#{foreign_key.inspect}': #{e.message}"
|
|
|
|
|
2017-06-06 15:49:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|