20170531144425_foreign_keys.rb 7.6 KB

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