20181017000001_cti_generic_api2.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class CtiGenericApi2 < ActiveRecord::Migration[5.1]
  3. def up
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. return if !column_exists?(:cti_logs, :initialized_at)
  7. return if column_exists?(:cti_logs, :initialized_at_cleanup)
  8. if ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] == 'mysql2'
  9. # disable the MySQL strict_mode for the current connection
  10. execute("SET sql_mode = ''")
  11. add_column :cti_logs, :initialized_at_cleanup, :timestamp, limit: 3, null: true, default: '0000-00-00 00:00:00'
  12. else
  13. add_column :cti_logs, :initialized_at_cleanup, :timestamp, limit: 3, null: true
  14. end
  15. Cti::Log.connection.schema_cache.clear!
  16. Cti::Log.reset_column_information
  17. # clenaup table records
  18. Cti::Log.reorder(created_at: :desc).limit(2000).each do |log|
  19. if log.initialized_at
  20. begin
  21. initialized_at = Time.zone.parse(log.initialized_at)
  22. log.update_column(:initialized_at_cleanup, initialized_at) # rubocop:disable Rails/SkipsModelValidations
  23. if initialized_at && log.start_at
  24. log.update_column(:duration_waiting_time, log.start_at.to_i - initialized_at.to_i) # rubocop:disable Rails/SkipsModelValidations
  25. else
  26. log.update_column(:duration_waiting_time, nil) # rubocop:disable Rails/SkipsModelValidations
  27. end
  28. rescue => e
  29. Rails.logger.error e
  30. end
  31. end
  32. if log.end_at && log.start_at
  33. log.update_column(:duration_talking_time, log.end_at.to_i - log.start_at.to_i) # rubocop:disable Rails/SkipsModelValidations
  34. else
  35. log.update_column(:duration_talking_time, nil) # rubocop:disable Rails/SkipsModelValidations
  36. end
  37. end
  38. remove_column(:cti_logs, :initialized_at)
  39. Cti::Log.connection.schema_cache.clear!
  40. Cti::Log.reset_column_information
  41. rename_column :cti_logs, :initialized_at_cleanup, :initialized_at
  42. Cti::Log.connection.schema_cache.clear!
  43. Cti::Log.reset_column_information
  44. end
  45. end