20170531144425_foreign_keys.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class ForeignKeys < ActiveRecord::Migration[4.2]
  3. disable_ddl_transaction!
  4. def change
  5. # return if it's a new setup
  6. return if !Setting.exists?(name: 'system_init_done')
  7. # remove wrong plural of ID columns
  8. ActiveRecord::Migration.rename_column :ticket_flags, :tickets_id, :ticket_id
  9. ActiveRecord::Migration.rename_column :ticket_article_flags, :ticket_articles_id, :ticket_article_id
  10. # add missing foreign keys
  11. foreign_keys = [
  12. # Base
  13. %i[users organizations],
  14. [:users, :users, { column: :created_by_id }],
  15. [:users, :users, { column: :updated_by_id }],
  16. [:signatures, :users, { column: :created_by_id }],
  17. [:signatures, :users, { column: :updated_by_id }],
  18. [:email_addresses, :users, { column: :created_by_id }],
  19. [:email_addresses, :users, { column: :updated_by_id }],
  20. %i[groups signatures],
  21. %i[groups email_addresses],
  22. [:groups, :users, { column: :created_by_id }],
  23. [:groups, :users, { column: :updated_by_id }],
  24. [:roles, :users, { column: :created_by_id }],
  25. [:roles, :users, { column: :updated_by_id }],
  26. [:organizations, :users, { column: :created_by_id }],
  27. [:organizations, :users, { column: :updated_by_id }],
  28. %i[roles_users users],
  29. %i[roles_users roles],
  30. %i[groups_users users],
  31. %i[groups_users groups],
  32. %i[organizations_users users],
  33. %i[organizations_users organizations],
  34. %i[authorizations users],
  35. [:translations, :users, { column: :created_by_id }],
  36. [:translations, :users, { column: :updated_by_id }],
  37. %i[tokens users],
  38. [:packages, :users, { column: :created_by_id }],
  39. [:packages, :users, { column: :updated_by_id }],
  40. %i[taskbars users],
  41. %i[tags tag_items],
  42. %i[tags tag_objects],
  43. [:tags, :users, { column: :created_by_id }],
  44. [:recent_views, :object_lookups, { column: :recent_view_object_id }],
  45. [:recent_views, :users, { column: :created_by_id }],
  46. [:activity_streams, :type_lookups, { column: :activity_stream_type_id }],
  47. [:activity_streams, :object_lookups, { column: :activity_stream_object_id }],
  48. %i[activity_streams permissions],
  49. %i[activity_streams groups],
  50. [:activity_streams, :users, { column: :created_by_id }],
  51. %i[histories history_types],
  52. %i[histories history_objects],
  53. %i[histories history_attributes],
  54. [:histories, :users, { column: :created_by_id }],
  55. %i[stores store_objects],
  56. %i[stores store_files],
  57. [:stores, :users, { column: :created_by_id }],
  58. [:avatars, :users, { column: :created_by_id }],
  59. [:avatars, :users, { column: :updated_by_id }],
  60. [:online_notifications, :users, { column: :created_by_id }],
  61. [:online_notifications, :users, { column: :updated_by_id }],
  62. [:schedulers, :users, { column: :created_by_id }],
  63. [:schedulers, :users, { column: :updated_by_id }],
  64. [:calendars, :users, { column: :created_by_id }],
  65. [:calendars, :users, { column: :updated_by_id }],
  66. %i[user_devices users],
  67. %i[object_manager_attributes object_lookups],
  68. [:object_manager_attributes, :users, { column: :created_by_id }],
  69. [:object_manager_attributes, :users, { column: :updated_by_id }],
  70. %i[cti_caller_ids users],
  71. [:stats_stores, :users, { column: :created_by_id }],
  72. [:http_logs, :users, { column: :created_by_id }],
  73. [:http_logs, :users, { column: :updated_by_id }],
  74. # Ticket
  75. [:ticket_state_types, :users, { column: :created_by_id }],
  76. [:ticket_state_types, :users, { column: :updated_by_id }],
  77. [:ticket_states, :ticket_state_types, { column: :state_type_id }],
  78. [:ticket_states, :users, { column: :created_by_id }],
  79. [:ticket_states, :users, { column: :updated_by_id }],
  80. [:ticket_priorities, :users, { column: :created_by_id }],
  81. [:ticket_priorities, :users, { column: :updated_by_id }],
  82. %i[tickets groups],
  83. [:tickets, :users, { column: :owner_id }],
  84. [:tickets, :users, { column: :customer_id }],
  85. [:tickets, :ticket_priorities, { column: :priority_id }],
  86. [:tickets, :ticket_states, { column: :state_id }],
  87. %i[tickets organizations],
  88. [:tickets, :users, { column: :created_by_id }],
  89. [:tickets, :users, { column: :updated_by_id }],
  90. [:ticket_flags, :tickets, { column: :ticket_id }],
  91. [:ticket_flags, :users, { column: :created_by_id }],
  92. [:ticket_article_types, :users, { column: :created_by_id }],
  93. [:ticket_article_types, :users, { column: :updated_by_id }],
  94. [:ticket_article_senders, :users, { column: :created_by_id }],
  95. [:ticket_article_senders, :users, { column: :updated_by_id }],
  96. %i[ticket_articles tickets],
  97. [:ticket_articles, :ticket_article_types, { column: :type_id }],
  98. [:ticket_articles, :ticket_article_senders, { column: :sender_id }],
  99. [:ticket_articles, :users, { column: :created_by_id }],
  100. [:ticket_articles, :users, { column: :updated_by_id }],
  101. [:ticket_articles, :users, { column: :origin_by_id }],
  102. [:ticket_article_flags, :ticket_articles, { column: :ticket_article_id }],
  103. [:ticket_article_flags, :users, { column: :created_by_id }],
  104. %i[ticket_time_accountings tickets],
  105. %i[ticket_time_accountings ticket_articles],
  106. [:ticket_time_accountings, :users, { column: :created_by_id }],
  107. [:overviews, :users, { column: :created_by_id }],
  108. [:overviews, :users, { column: :updated_by_id }],
  109. %i[overviews_roles overviews],
  110. %i[overviews_roles roles],
  111. %i[overviews_users overviews],
  112. %i[overviews_users users],
  113. %i[overviews_groups overviews],
  114. %i[overviews_groups groups],
  115. [:triggers, :users, { column: :created_by_id }],
  116. [:triggers, :users, { column: :updated_by_id }],
  117. [:jobs, :users, { column: :created_by_id }],
  118. [:jobs, :users, { column: :updated_by_id }],
  119. %i[links link_types],
  120. [:postmaster_filters, :users, { column: :created_by_id }],
  121. [:postmaster_filters, :users, { column: :updated_by_id }],
  122. %i[text_modules users],
  123. [:text_modules, :users, { column: :created_by_id }],
  124. [:text_modules, :users, { column: :updated_by_id }],
  125. %i[text_modules_groups text_modules],
  126. %i[text_modules_groups groups],
  127. %i[templates users],
  128. [:templates, :users, { column: :created_by_id }],
  129. [:templates, :users, { column: :updated_by_id }],
  130. %i[templates_groups templates],
  131. %i[templates_groups groups],
  132. %i[channels groups],
  133. [:channels, :users, { column: :created_by_id }],
  134. [:channels, :users, { column: :updated_by_id }],
  135. [:slas, :users, { column: :created_by_id }],
  136. [:slas, :users, { column: :updated_by_id }],
  137. [:macros, :users, { column: :created_by_id }],
  138. [:macros, :users, { column: :updated_by_id }],
  139. [:chats, :users, { column: :created_by_id }],
  140. [:chats, :users, { column: :updated_by_id }],
  141. %i[chat_sessions chats],
  142. %i[chat_sessions users],
  143. [:chat_sessions, :users, { column: :created_by_id }],
  144. [:chat_sessions, :users, { column: :updated_by_id }],
  145. %i[chat_messages chat_sessions],
  146. [:chat_messages, :users, { column: :created_by_id }],
  147. [:chat_agents, :users, { column: :created_by_id }],
  148. [:chat_agents, :users, { column: :updated_by_id }],
  149. [:report_profiles, :users, { column: :created_by_id }],
  150. [:report_profiles, :users, { column: :updated_by_id }],
  151. %i[karma_users users],
  152. %i[karma_activity_logs users],
  153. [:karma_activity_logs, :karma_activities, { column: :activity_id }],
  154. ]
  155. foreign_keys.each do |foreign_key|
  156. ActiveRecord::Base.transaction do
  157. add_foreign_key(*foreign_key)
  158. rescue => e
  159. Rails.logger.error "Inconsistent data status detected while adding foreign key '#{foreign_key.inspect}': #{e.message}"
  160. end
  161. end
  162. end
  163. end