New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add set_constraints
helper for PostgreSQL
#49187
Conversation
5b4d83b
to
9587390
Compare
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
Outdated
Show resolved
Hide resolved
9587390
to
e3b8fa7
Compare
@alpaca-tc What do you think about this pull request? I'm asking this question because you have authored #46192 and #47655 . |
@yahonda I think the As for
begin
Post.connection.defer_constraints do
raise 'a'
end
rescue
# The user expects the constraint to be back to IMMEDIATE, but it remains DEFERRABLE.
end |
I actually went through several iterations of what to do here, before simply leaving it very simple. In most cases, any exception will end up rolling back the transaction anyway. One of my attempts was to inspect the connection's state and see if a transaction was open, and if it wasn't avoid rolling it back. But then you have to special case Rollback, to not set the deferred state, since the transaction is going to rollback anyway, it's just a wasted command. And then realized that for most other exceptions, that's also the case. And technically just because we set constraints to deferred, doesn't mean it was initially immediate, so setting it "back" to immediate may be wrong. It all seems rather complicated. In light of all that, I think it would probably be best to get rid of the defer_constraints helper, and only add set_constraints. Thoughts? |
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
Show resolved
Hide resolved
Sorry I left some knit-picks on the ChangeLog, that are not super important. Upon thinking about this though, I think I like the idea of just adding |
agree 👍 |
The docs already talk about how to set up deferrable constraints, but then rely on the user crafting custom SQL to actually use the feature. The helper makes it easier to handle juggling multiple specific constraints and quoting issues.
e3b8fa7
to
f9fee1a
Compare
set_constraints
and defer_constraints
helpers for PostgreSQLset_constraints
helper for PostgreSQL
rebased, |
The docs already talk about how to set up deferrable constraints, but then rely on tthe user crafting custom SQL to actually use the feature. The helpers makes it easier to handle juggling multiple specific constraints and quoting issues.
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]