From 70a03d657e5b13ccceadd1d1aa6b70518ceaf3d6 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 2 Nov 2017 10:00:44 +0100 Subject: [PATCH] Improved migration for issue #1375 - Improved handling assignment_timeout. --- .../20170830000001_last_owner_update.rb | 57 ------------------- .../20171102000001_last_owner_update2.rb | 32 +++++++++++ 2 files changed, 32 insertions(+), 57 deletions(-) delete mode 100644 db/migrate/20170830000001_last_owner_update.rb create mode 100644 db/migrate/20171102000001_last_owner_update2.rb diff --git a/db/migrate/20170830000001_last_owner_update.rb b/db/migrate/20170830000001_last_owner_update.rb deleted file mode 100644 index c3115dc96..000000000 --- a/db/migrate/20170830000001_last_owner_update.rb +++ /dev/null @@ -1,57 +0,0 @@ -class LastOwnerUpdate < ActiveRecord::Migration[4.2] - def up - - # return if it's a new setup - return if !Setting.find_by(name: 'system_init_done') - - # reset assignment_timeout to prevent unwanted things happen - Group.all.each do |group| - group.assignment_timeout = nil - group.save! - end - - add_column :tickets, :last_owner_update_at, :timestamp, limit: 3, null: true - add_index :tickets, [:last_owner_update_at] - Ticket.reset_column_information - - Scheduler.create_if_not_exists( - name: 'Process auto unassign tickets', - method: 'Ticket.process_auto_unassign', - period: 10.minutes, - prio: 1, - active: true, - updated_by_id: 1, - created_by_id: 1, - ) - - state_ids = Ticket::State.by_category(:work_on).pluck(:id) - if state_ids.present? - ticket_ids = Ticket.where('tickets.state_id IN (?) AND tickets.owner_id != 1', state_ids).order(created_at: :desc).limit(1000).pluck(:id) - ticket_ids.each do |ticket_id| - ticket = Ticket.find_by(id: ticket_id) - next if !ticket - ticket.last_owner_update_at = last_owner_update_at(ticket) - ticket.save! - end - end - end - - def last_owner_update_at(ticket) - type = History::Type.lookup(name: 'updated') - if type - object = History::Object.lookup(name: 'Ticket') - if object - attribute = History::Attribute.lookup(name: 'owner') - if attribute - history = History.where(o_id: ticket.id, history_type_id: type.id, history_object_id: object.id, history_attribute_id: attribute.id).where.not(id_to: 1).order(created_at: :desc).limit(1) - if history.present? - return history.first.created_at - end - end - end - end - return nil if ticket.owner_id == 1 - ticket.created_at - end - -end diff --git a/db/migrate/20171102000001_last_owner_update2.rb b/db/migrate/20171102000001_last_owner_update2.rb new file mode 100644 index 000000000..96fb88759 --- /dev/null +++ b/db/migrate/20171102000001_last_owner_update2.rb @@ -0,0 +1,32 @@ +class LastOwnerUpdate2 < ActiveRecord::Migration[5.1] + def up + + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + # reset assignment_timeout to prevent unwanted things happen + Group.all.each do |group| + group.assignment_timeout = nil + group.save! + end + + # check if column already exists + if !ActiveRecord::Base.connection.column_exists?(:tickets, :last_owner_update_at) + add_column :tickets, :last_owner_update_at, :timestamp, limit: 3, null: true + add_index :tickets, [:last_owner_update_at] + Ticket.reset_column_information + end + + Scheduler.create_if_not_exists( + name: 'Process auto unassign tickets', + method: 'Ticket.process_auto_unassign', + period: 10.minutes, + prio: 1, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + + end + +end