2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-10-18 09:42:37 +00:00
|
|
|
class CtiGenericApi2 < ActiveRecord::Migration[5.1]
|
|
|
|
def up
|
|
|
|
|
|
|
|
# return if it's a new setup
|
2020-08-03 08:35:43 +00:00
|
|
|
return if !Setting.exists?(name: 'system_init_done')
|
2018-10-18 09:42:37 +00:00
|
|
|
return if !column_exists?(:cti_logs, :initialized_at)
|
2018-10-18 11:20:39 +00:00
|
|
|
return if column_exists?(:cti_logs, :initialized_at_cleanup)
|
2018-10-18 09:42:37 +00:00
|
|
|
|
2018-10-24 23:43:16 +00:00
|
|
|
if ActiveRecord::Base.connection_config[:adapter] == 'mysql2'
|
|
|
|
# disable the MySQL strict_mode for the current connection
|
|
|
|
execute("SET sql_mode = ''")
|
|
|
|
add_column :cti_logs, :initialized_at_cleanup, :timestamp, limit: 3, null: true, default: '0000-00-00 00:00:00'
|
|
|
|
else
|
|
|
|
add_column :cti_logs, :initialized_at_cleanup, :timestamp, limit: 3, null: true
|
|
|
|
end
|
|
|
|
|
2018-10-18 09:42:37 +00:00
|
|
|
Cti::Log.connection.schema_cache.clear!
|
|
|
|
Cti::Log.reset_column_information
|
|
|
|
|
|
|
|
# clenaup table records
|
|
|
|
Cti::Log.order(created_at: :desc).limit(2000).each do |log|
|
|
|
|
if log.initialized_at
|
|
|
|
begin
|
|
|
|
initialized_at = Time.zone.parse(log.initialized_at)
|
|
|
|
log.update_column(:initialized_at_cleanup, initialized_at) # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
if initialized_at && log.start_at
|
|
|
|
log.update_column(:duration_waiting_time, log.start_at.to_i - initialized_at.to_i) # rubocop:disable Rails/SkipsModelValidations
|
2018-10-18 11:20:39 +00:00
|
|
|
else
|
|
|
|
log.update_column(:duration_waiting_time, nil) # rubocop:disable Rails/SkipsModelValidations
|
2018-10-18 09:42:37 +00:00
|
|
|
end
|
|
|
|
rescue => e
|
2018-10-18 11:20:39 +00:00
|
|
|
Rails.logger.error e
|
2018-10-18 09:42:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if log.end_at && log.start_at
|
|
|
|
log.update_column(:duration_talking_time, log.end_at.to_i - log.start_at.to_i) # rubocop:disable Rails/SkipsModelValidations
|
2018-10-18 11:20:39 +00:00
|
|
|
else
|
|
|
|
log.update_column(:duration_talking_time, nil) # rubocop:disable Rails/SkipsModelValidations
|
2018-10-18 09:42:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
remove_column(:cti_logs, :initialized_at)
|
|
|
|
Cti::Log.connection.schema_cache.clear!
|
|
|
|
Cti::Log.reset_column_information
|
|
|
|
|
|
|
|
rename_column :cti_logs, :initialized_at_cleanup, :initialized_at
|
|
|
|
Cti::Log.connection.schema_cache.clear!
|
|
|
|
Cti::Log.reset_column_information
|
|
|
|
end
|
|
|
|
end
|