20190405000001_database_indexes.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class DatabaseIndexes < ActiveRecord::Migration[5.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. map = {
  7. activity_streams: [
  8. {
  9. columns: %i[permission_id group_id],
  10. },
  11. {
  12. columns: %i[permission_id group_id created_at],
  13. name: 'index_activity_streams_on_permission_id_group_id_created_at',
  14. },
  15. ],
  16. stores: [
  17. {
  18. columns: [:store_file_id],
  19. },
  20. ],
  21. cti_caller_ids: [
  22. {
  23. columns: %i[object o_id level user_id caller_id],
  24. name: 'index_cti_caller_ids_on_object_o_id_level_user_id_caller_id',
  25. },
  26. ],
  27. tickets: [
  28. {
  29. columns: [:updated_at],
  30. },
  31. {
  32. columns: %i[customer_id state_id created_at],
  33. },
  34. {
  35. columns: %i[group_id state_id updated_at],
  36. },
  37. {
  38. columns: %i[group_id state_id owner_id updated_at],
  39. name: 'index_tickets_on_group_id_state_id_owner_id_updated_at',
  40. },
  41. {
  42. columns: %i[group_id state_id created_at],
  43. },
  44. {
  45. columns: %i[group_id state_id owner_id created_at],
  46. name: 'index_tickets_on_group_id_state_id_owner_id_created_at',
  47. },
  48. {
  49. columns: %i[group_id state_id close_at],
  50. },
  51. {
  52. columns: %i[group_id state_id owner_id close_at],
  53. name: 'index_tickets_on_group_id_state_id_owner_id_close_at',
  54. },
  55. ]
  56. }
  57. map.each do |table, indexes|
  58. indexes.each do |index|
  59. params = [table, index[:columns]]
  60. params.push(name: index[:name]) if index[:name]
  61. next if index_exists?(*params)
  62. add_index(*params)
  63. end
  64. end
  65. end
  66. end