-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
PostgreSQL reload virtual columns on update via RETURNING clause #48628
base: main
Are you sure you want to change the base?
PostgreSQL reload virtual columns on update via RETURNING clause #48628
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: true I 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
b71ebf1
to
7549871
Compare
Extends Active Record to reload virtual columns on update when using PostgreSQL. This is done by issuing a single UPDATE query that includes a RETURNING clause. Given a `Post` model represented by the following schema: ```ruby create_table :posts, id: false do |t| t.integer :upvotes_count t.integer :downvotes_count t.virtual :total_votes_count, type: :integer, as: "upvotes_count + downvotes_count", stored: true end ``` `total_votes_count` will reflect the sum of upvotes and downvotes after `update` is successfully called. Prior to this change calling `reload` would have been necessary to obtain the value calculated by the database. ```ruby post = Post.find(1) post.update(upvotes_count: 2, downvotes_count: 2) post.total_votes => 4 ``` * At this moment MySQL and SQLite adapters do not support this behavior
7549871
to
f708a50
Compare
Thanks for the feedback @nvasilevski. I've implemented your suggestion for keeping the signature the same expect when Build error from Rubocop appears to be from |
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
Post
model represented by the following schema:total_votes_count
will reflect the sum of upvotes and downvotes afterupdate
is successfully called. Prior to this change callingreload
would 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]