"Fossies" - the Fresh Open Source Software Archive

Member "db/migrate/20170531144425_foreign_keys.rb" (25 Mar 2021, 8117 Bytes) of package /linux/www/zammad-4.0.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "20170531144425_foreign_keys.rb": 3.4.1_vs_3.5.0.

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