20190405000001_database_indexes.rb 1.8 KB

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