Skip to content

Add the touch option to ActiveRecord#increment! and decrement! - #27660

Merged
kaspth merged 1 commit into
rails:masterfrom
akihiro17:updates-timestamp
Jan 14, 2017
Merged

kaspth merged 1 commit into
rails:masterfrom
akihiro17:updates-timestamp

Conversation

@akihiro17

Copy link
Copy Markdown
Contributor

Summary

I modified #21979 with the changes from #26995.

In this commit, AR#increment! updates timestamp columns.
This is the same behavior as Rails before #11410

@rails-bot

Copy link
Copy Markdown

r? @kaspth

(@rails-bot has picked a reviewer for you, use r? to override)

@kaspth kaspth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the changes have been made and a changelog entry in Active Record's changelog is added, you need to squash your commits down to 1. Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change? Presumably sanitize_sql_for_assignment returns an empty string that's safe to append?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the last item of updates is an empty string, ActiveRecord generates an SQL like below.

updates = ["\"credit_limit\" = COALESCE(\"credit_limit\", 0) + 1", ""]
unscoped.where(primary_key => id).update_all updates.join(", ")
# => UPDATE "accounts" SET "credit_limit" = COALESCE("credit_limit", 0) + 1,  WHERE "accounts"."id" = ?

This causes ActiveRecord::StatementInvalid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to collect the touch argument from the user and pass it on like this:

# Wrapper around #increment that writes the update to the database.
# Only +attribute+ is updated; the record itself is not saved.
# This means that any other modified attributes will still be dirty.
# Validations and callbacks are skipped. Supports the `touch` option from
# +update_counters+, see that for more.
# Returns +self+.
def increment!(attribute, by = 1, touch: nil)
  # …
  self.class.update_counters(id, attribute => change, touch: touch)
  # …
end

Same equivalent for decrement! and a test there.

Supports the `touch` option from update_counters.
The default behavior is not to update timestamp columns.
@akihiro17 akihiro17 changed the title Touch timestamp columns in AR#increment! Add the touch option to ActiveRecord#increment! and decrement! Jan 13, 2017
@kaspth
kaspth merged commit 31a95ed into rails:master Jan 14, 2017
@kaspth

kaspth commented Jan 14, 2017

Copy link
Copy Markdown
Contributor

Neat, thanks!

kamipo added a commit that referenced this pull request Jun 18, 2018
…thod

`touch` option was added to `increment!` (#27660) and `update_counters`
(#26995). But that option behaves inconsistently with
`Persistence#touch` method.

If `touch` option is passed attribute names, it won't update
update_at/on attributes unlike `Persistence#touch` method.

Due to changed from `Persistence#touch` to `increment!` with `touch`
option, #31405 has a regression that `counter_cache` with `touch` option
which is passed attribute names won't update update_at/on attributes.

I think that the inconsistency is not intended. To get back consistency,
ensure that `touch` option updates update_at/on attributes.
kamipo added a commit to kamipo/rails that referenced this pull request Jun 18, 2018
…thod

`touch` option was added to `increment!` (rails#27660) and `update_counters`
(rails#26995). But that option behaves inconsistently with
`Persistence#touch` method.

If `touch` option is passed attribute names, it won't update
update_at/on attributes unlike `Persistence#touch` method.

Due to changed from `Persistence#touch` to `increment!` with `touch`
option, rails#31405 has a regression that `counter_cache` with `touch` option
which is passed attribute names won't update update_at/on attributes.

I think that the inconsistency is not intended. To get back consistency,
ensure that `touch` option updates update_at/on attributes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants