scenic icon indicating copy to clipboard operation
scenic copied to clipboard

PG::NoActiveSqlTransaction: ERROR: SAVEPOINT can only be used in transaction blocks

Open drselump14 opened this issue 4 years ago • 4 comments

Get this stranger error when trying to update the materialized view.

It seems that adapter prevents any usage of SAVEPOINT but for transactions.

Any help would be appreciated

drselump14 avatar Feb 04 '21 11:02 drselump14

Can you give us more information? What migration are you running? What is the SQL? What stacktrace do you get?

derekprior avatar Feb 04 '21 14:02 derekprior

Hello,

I've encountered the same issue.

In my case, we're using the doctolib/safe-pg-migrations gem, which automatically disable transactions. Wrapping my migration in a

ActiveRecord::Base.connection.transaction do
end

block fixed the problem and everything run smoothly.

As this gem assumes that its run in a transaction, maybe it could wrap itself into one if necessary. What do you think ?

stem avatar Mar 19 '21 19:03 stem

I don't think i want to sneakily renable transactions if the user explicitly decided not to use them for the migration (either themselves or by choosing a gem that does it for them). I think I'd be more likely to accept a patch that raises when IndexReapplication is used in a context where a transaction is not currently running. That would not be a behavior change as the migration would still fail, but the error message could tell people what's going on.

derekprior avatar Mar 19 '21 19:03 derekprior

Makes perfect sense !

I guess I did not think about that 'cause I've re-enabled the transaction on my migration 😅

For what it's worth, I think the check should be done before making any change in the database, as if the migration fails after dropping a view (my use case today) and is not in a transaction, the view is really gone.

stem avatar Mar 19 '21 19:03 stem