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
Use Rails upsert
to generate update_count! query in Counters concern
#28738
Use Rails upsert
to generate update_count! query in Counters concern
#28738
Conversation
cb75f4e
to
353f72c
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #28738 +/- ##
==========================================
- Coverage 85.01% 85.01% -0.01%
==========================================
Files 1059 1060 +1
Lines 28277 28286 +9
Branches 4538 4536 -2
==========================================
+ Hits 24040 24047 +7
- Misses 3074 3075 +1
- Partials 1163 1164 +1 ☔ View full report in Codecov by Sentry. |
353f72c
to
bbcf607
Compare
bbcf607
to
fa4c5ab
Compare
fa4c5ab
to
c711f4c
Compare
c711f4c
to
0fc3a4e
Compare
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Rails upsert support has improved (see notes in original impl here about lack of full support) and there now are options to support this operation via the framework.
There are specs for the
increment_count!
anddecrement_count!
methods in this class, which are the main users of the method and I think cover the two basic paths (with and without statuses_count being present).Here's the before/after SQL for the general case when
statuses_count
is not present (formatting modified for comparison, but generated syntax not edited)...Before:
After:
And when
statuses_count
is present...Before:
After:
Differences here:
CURRENT_TIMESTAMP
instead ofNOW()
, but I believe these are equivalent. I've used the former in the code so it matches framework.last_status_at = NOW()
in our sql string for the scenario where we are creating the record for the first time and updating statuses_count and need a value for the status timestamp ... but I can't figure out how to do this in the hash args style, so I'm usingTime.current
on the ruby side. I suspect this is fine, since the previous implementation also allowed for possible gaps between the written value for the status and the value in this field, but just wanted to note it as a difference. We could potentially improve this by finding the status first, at the cost of an additional query.