Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activerecord in Sinatra transactional callbacks warning

Tags:

sinatra

Sinatra project with activerecord gem raise a warning:

DEPRECATION WARNING: Currently, Active Record suppresses errors raised
within `after_rollback`/`after_commit` callbacks and only print them
to the logs. In the next version, these errors will no longer be
suppressed. Instead, the errors will propagate normally just like in
other Active Record callbacks.

You can opt into the new behavior and remove this warning by setting:


**config.active_record.raise_in_transactional_callbacks = true**

Gemfile.lock

activemodel (4.2.0)

      activesupport (= 4.2.0)
      builder (~> 3.1)

How to remove this? thanks!

EDIT: The mini project is here https://gist.github.com/williamhqs/c127e5d7018aa61cb02a

like image 524
William Hu Avatar asked Jan 09 '15 14:01

William Hu


2 Answers

The error message tells you what to do. In Rails, add this line to your application for the time being:

config.active_record.raise_in_transactional_callbacks = true

In Sinatra, you can do it directly on ActiveRecord::Base:

ActiveRecord::Base.raise_in_transactional_callbacks = true

Keep in mind that this configuration option itself will be deprecated with the next release of Active Record.

like image 117
Patrick Oscity Avatar answered Nov 14 '22 06:11

Patrick Oscity


Patrick is right, to remove the warning and go to the new behaviour, you should add

Rails : config.active_record.raise_in_transactional_callbacks = true

Sinatra : ActiveRecord::Base.raise_in_transactional_callbacks = true

I think a little background on what it exactly does is important : https://github.com/rails/rails/pull/16537 and https://github.com/rails/rails/pull/14488

Actual Behaviour for after_commit / after_rollback : When there is an error, the error is logged however it's not shallowed. Meaning that the error does not stop the current execution.

Futur Behaviour for after_commit / after_rollback : When there is an error, the error is logged and WILL NOT BE shallowed. Meaning that the error WILL STOP the current execution.

The warning displayed is only here to inform you of that futur behaviour, and this warning will be removed on futur versions of Rails / ActiveRecord.

like image 42
Erowlin Avatar answered Nov 14 '22 06:11

Erowlin