Prevent ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block issues (isolate Ticket.selector statements in own sub transaction).

This commit is contained in:
Martin Edenhofer 2017-08-13 11:54:57 +02:00
parent f1d0bed331
commit 8f049225b1
3 changed files with 141 additions and 25 deletions

View file

@ -377,6 +377,8 @@ get count of tickets and tickets which match on selector
query, bind_params, tables = selector2sql(selectors, current_user) query, bind_params, tables = selector2sql(selectors, current_user)
return [] if !query return [] if !query
ActiveRecord::Base.transaction(requires_new: true) do
begin
if !current_user if !current_user
ticket_count = Ticket.where(query, *bind_params).joins(tables).count ticket_count = Ticket.where(query, *bind_params).joins(tables).count
tickets = Ticket.where(query, *bind_params).joins(tables).limit(limit) tickets = Ticket.where(query, *bind_params).joins(tables).limit(limit)
@ -387,7 +389,14 @@ get count of tickets and tickets which match on selector
ticket_count = Ticket.where(access_condition).where(query, *bind_params).joins(tables).count ticket_count = Ticket.where(access_condition).where(query, *bind_params).joins(tables).count
tickets = Ticket.where(access_condition).where(query, *bind_params).joins(tables).limit(limit) tickets = Ticket.where(access_condition).where(query, *bind_params).joins(tables).limit(limit)
[ticket_count, tickets] return [ticket_count, tickets]
rescue ActiveRecord::StatementInvalid => e
Rails.logger.error e.inspect
Rails.logger.error e.backtrace
raise ActiveRecord::Rollback
end
end
[]
end end
=begin =begin

View file

@ -8,7 +8,6 @@ class Transaction
ApplicationHandleInfo.current = options[:interface_handle] ApplicationHandleInfo.current = options[:interface_handle]
end end
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
begin
PushMessages.init PushMessages.init
yield yield
if options[:interface_handle] if options[:interface_handle]
@ -19,11 +18,6 @@ class Transaction
disable: options[:disable], disable: options[:disable],
) )
PushMessages.finish PushMessages.finish
rescue ActiveRecord::StatementInvalid => e
Rails.logger.error e.inspect
Rails.logger.error e.backtrace
raise ActiveRecord::Rollback
end
end end
end end
end end

View file

@ -3367,4 +3367,117 @@ class TicketTriggerTest < ActiveSupport::TestCase
end end
test '3 invalid condition' do
trigger1 = Trigger.create_or_update(
name: 'aaa loop check',
condition: {
'ticket.action' => {
'operator' => 'is',
'value' => 'create',
},
},
perform: {
'ticket.tags' => {
'operator' => 'add',
'value' => 'xxx',
},
},
disable_notification: true,
active: true,
created_by_id: 1,
updated_by_id: 1,
)
trigger1.update_column(:condition, {
'ticket.action' => {
'operator' => 'is',
'value' => 'create',
},
'ticket.first_response_at' => {
'operator' => 'before (absolute)',
'value' => 'invalid invalid 4',
},
})
assert_equal('invalid invalid 4', trigger1.condition['ticket.first_response_at']['value'])
trigger2 = Trigger.create_or_update(
name: 'auto reply',
condition: {
'ticket.action' => {
'operator' => 'is',
'value' => 'create',
},
'ticket.state_id' => {
'operator' => 'is',
'value' => Ticket::State.lookup(name: 'new').id.to_s,
}
},
perform: {
'notification.email' => {
'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
'recipient' => 'ticket_customer',
'subject' => 'Thanks for your inquiry (#{ticket.title})!',
},
'ticket.priority_id' => {
'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
},
'ticket.tags' => {
'operator' => 'add',
'value' => 'aa, kk',
},
},
disable_notification: true,
active: true,
created_by_id: 1,
updated_by_id: 1,
)
ticket1 = Ticket.create(
title: "some <b>title</b>\n äöüß",
group: Group.lookup(name: 'Users'),
customer: User.lookup(email: 'nicole.braun@zammad.org'),
updated_by_id: 1,
created_by_id: 1,
)
assert(ticket1, 'ticket1 created')
Ticket::Article.create(
ticket_id: ticket1.id,
from: 'some_sender@example.com',
to: 'some_recipient@example.com',
subject: 'some subject',
message_id: 'some@id',
body: "some message <b>note</b>\nnew line",
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Agent'),
type: Ticket::Article::Type.find_by(name: 'note'),
updated_by_id: 1,
created_by_id: 1,
)
ticket1.reload
assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
assert_equal('new', ticket1.state.name, 'ticket1.state verify')
assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
assert_equal([], ticket1.tag_list)
Observer::Transaction.commit
ticket1.reload
assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
assert_equal('new', ticket1.state.name, 'ticket1.state verify')
assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
assert_equal(%w(aa kk), ticket1.tag_list)
article1 = ticket1.articles.last
assert_match('Zammad <zammad@localhost>', article1.from)
assert_match('nicole.braun@zammad.org', article1.to)
assert_match('Thanks for your inquiry (some <b>title</b> äöüß)!', article1.subject)
assert_match('Braun<br>some &lt;b&gt;title&lt;/b&gt;', article1.body)
assert_match('&gt; some message &lt;b&gt;note&lt;/b&gt;<br>&gt; new line', article1.body)
assert_equal('text/html', article1.content_type)
end
end end