PostgreSQL reload virtual columns on update via RETURNING clause#48628
PostgreSQL reload virtual columns on update via RETURNING clause#48628abaldwin88 wants to merge 1 commit intorails:mainfrom
Conversation
|
I just checked out Rails main to see how the generated columns work with Then I tried this branch and it works as expected 🥳 Given a virtual column like: add_column :products, :premium, :virtual, type: :boolean, as: "price >= 1000", stored: trueI could do: product = Product.create! user: u, price: 100
product.premium
=> false
product.update price: 2000
=> true
product.premium
=> true
product.previous_changes
=>
{"price"=>[100, 2000],
"updated_at"=>[Sun, 23 Jul 2023 22:22:35.416328000 UTC +00:00, Sun, 23 Jul 2023 22:22:51.945466000 UTC +00:00],
"premium"=>[false, true]}Yay! I can see the changes caused by the update of the I hope this makes it into 7.1 as the virtual columns will solve a gnarly problem for me if the dirty tracking works for them |
|
Ran into this one today as well. Would love to see this fixed. |
477cf89 to
b71ebf1
Compare
|
Rebased onto the latest from main. Added a test for dirty tracking behavior based on the example provided by @ideasasylum |
nvasilevski
left a comment
There was a problem hiding this comment.
I think the biggest issue with the current implementation is changing return value of public methods which is a breaking change and normally requires a deprecation cycle
Other than that I think it's a very valuable feature to be merged and also a safe one since most of the changes happen behind the scenes and don't require any public APIs to be exposed
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/mysql2/database_statements.rb
Outdated
Show resolved
Hide resolved
7549871 to
f708a50
Compare
|
Thanks for the feedback @nvasilevski. I've implemented your suggestion for keeping the signature the same except when Build error from Rubocop appears to be from |
|
Hi guys. Any chance the PR can be merged soon? |
| return super unless returning_columns&.any? | ||
|
|
||
| returning_columns_statement = returning_columns.map { |c| quote_column_name(c) }.join(", ") | ||
| sql = "#{sql} RETURNING #{returning_columns_statement}" |
There was a problem hiding this comment.
Instead of concat sqls, can we just build the right AST in the right place? We probably need to build the AST before passing to exec_update in the update method.
There was a problem hiding this comment.
Sorry I'm not entirely clear on the ask here. Is there something different here than sql_for_insert which uses the same code pattern?
I'm planning to move this method from being postgres specific and instead put it under the abstract class. Does that help address your concerns?
There was a problem hiding this comment.
My two cents is any further changes around this would be better handled as a separate follow-up PR? A refactor really ought to also include changes to sql_for_insert. However, including changes to that method here would be too broad and make this PR unfocused.
There was a problem hiding this comment.
@rafaelfranca I don't think Arel supports Returning nodes yet (implemented in #47161 🙏🏻), also #48241 was merged in 2023 with the same strategy 💡
activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
Outdated
Show resolved
Hide resolved
f708a50 to
162afd7
Compare
abaldwin88
left a comment
There was a problem hiding this comment.
Rebased and addressed feedback.
With the latest changes in main I see a pretty clear path to adding this functionality for SQLite and Maria as well. That can be done in a follow-up PR.
| create_table :defaults, force: true do |t| | ||
| t.integer :random_number, default: -> { "ABS(RANDOM())" } | ||
| t.virtual :virtual_stored_number, type: :integer, as: "random_number * 10", stored: true if supports_virtual_columns? | ||
| t.integer :random_number, default: -> { "ABS(RANDOM() % 100)" } |
There was a problem hiding this comment.
This way it will match the behavior of random_number in the postgresql schema
| ActiveRecord::Schema.define do | ||
| create_table :defaults, force: true do |t| | ||
| t.integer :random_number, default: -> { "ABS(RANDOM())" } | ||
| t.virtual :virtual_stored_number, type: :integer, as: "random_number * 10", stored: true if supports_virtual_columns? |
There was a problem hiding this comment.
Ensures certain code paths / edge cases will be exercised during tests
|
Is this PR mergable or is there any outstanding feedback that needs to be addressed? Happy to help with anything to get this moved along. |
Extends Active Record to automatically reload virtual columns on update when using PostgreSQL. This is done by issuing a single UPDATE query that includes a RETURNING clause.
Motivation
Saves an extra round trip to the database for reload and removes a developer pitfall around stale values.
Detail
Given a
Postmodel represented by the following schema:total_votes_countwill reflect the sum of upvotes and downvotes afterupdateis successfully called. Prior to this change callingreloadwould have been necessary to obtain the value calculated by the database.Additional information
ActiveRecord.reload_attributes#48434CC: @nvasilevski
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]