Skip to content

Commit

Permalink
Merge pull request #9105 from bemurphy/cache_key_updated_on
Browse files Browse the repository at this point in the history
cache_key consults updated_on timestamp if present

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Mar 7, 2013
2 parents d3adfd6 + 1dc98c1 commit 2e3e171
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
10 changes: 10 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ##

* Expand `#cache_key` to consult all relevant updated timestamps.

Previously only `updated_at` column was checked, now it will
consult other columns that received updated timestamps on save,
such as `updated_on`. When multiple columns are present it will
use the most recent timestamp.
Fixes #9033.

*Brendon Murphy*

* Throw `NotImplementedError` when trying to instantiate `ActiveRecord::Base` or an abstract class.

*Aaron Weiner*
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cache_key
case
when new_record?
"#{self.class.model_name.cache_key}/new"
when timestamp = self[:updated_at]
when timestamp = max_updated_column_timestamp
timestamp = timestamp.utc.to_s(cache_timestamp_format)
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
else
Expand Down
6 changes: 6 additions & 0 deletions activerecord/lib/active_record/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def all_timestamp_attributes
timestamp_attributes_for_create + timestamp_attributes_for_update
end

def max_updated_column_timestamp
if (timestamps = timestamp_attributes_for_update.map { |attr| self[attr] }.compact).present?
timestamps.map { |ts| ts.to_time }.max
end
end

def current_time_from_proper_timezone
self.class.default_timezone == :utc ? Time.now.utc : Time.now
end
Expand Down
22 changes: 20 additions & 2 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1455,12 +1455,30 @@ def test_cache_key_changes_when_child_touched
assert_not_equal key, car.cache_key
end

def test_cache_key_format_for_existing_record_with_nil_updated_at
def test_cache_key_format_for_existing_record_with_nil_updated_timestamps
dev = Developer.first
dev.update_columns(updated_at: nil)
dev.update_columns(updated_at: nil, updated_on: nil)
assert_match(/\/#{dev.id}$/, dev.cache_key)
end

def test_cache_key_for_updated_on
dev = Developer.first
dev.updated_at = nil
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:nsec)}", dev.cache_key
end

def test_cache_key_for_newer_updated_at
dev = Developer.first
dev.updated_at += 3600
assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:nsec)}", dev.cache_key
end

def test_cache_key_for_newer_updated_on
dev = Developer.first
dev.updated_on += 3600
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:nsec)}", dev.cache_key
end

def test_touch_should_raise_error_on_a_new_object
company = Company.new(:rating => 1, :name => "37signals", :firm_name => "37signals")
assert_raises(ActiveRecord::ActiveRecordError) do
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def create_table(*args, &block)
t.integer :salary, :default => 70000
t.datetime :created_at
t.datetime :updated_at
t.datetime :created_on
t.datetime :updated_on
end

create_table :developers_projects, :force => true, :id => false do |t|
Expand Down

1 comment on commit 2e3e171

@yahonda
Copy link
Member

@yahonda yahonda commented on 2e3e171 Mar 7, 2013

Choose a reason for hiding this comment

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

git bisects says it causes this regression error with Oracle database only. Will investigate later and open pull request/issue if necessary.

$ ARCONN=oracle ruby -Itest test/cases/base_test.rb  -n test_find_keeps_multiple_group_values
Using oracle
Run options: -n test_find_keeps_multiple_group_values --seed 12885

# Running tests:

E

Finished tests in 0.623040s, 1.6050 tests/s, 0.0000 assertions/s.

  1) Error:
BasicsTest#test_find_keeps_multiple_group_values:
ActiveRecord::StatementInvalid: OCIError: ORA-00979: not a GROUP BY expression: SELECT "DEVELOPERS".* FROM "DEVELOPERS"  GROUP BY developers.name, developers.salary, developers.id, developers.created_at, developers.updated_at
    stmt.c:230:in oci8lib_200.so
    /home/yahonda/.rvm/gems/ruby-2.0.0-p0@railsmaster/gems/ruby-oci8-2.1.4/lib/oci8/cursor.rb:126:in `exec'
    /home/yahonda/git/oracle-enhanced/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:143:in `exec'
    /home/yahonda/git/oracle-enhanced/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:749:in `block in exec_query'
    /home/yahonda/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:334:in `block in log'
    /home/yahonda/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/yahonda/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:329:in `log'
    /home/yahonda/git/oracle-enhanced/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:1491:in `log'
    /home/yahonda/git/oracle-enhanced/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:729:in `exec_query'
    /home/yahonda/git/oracle-enhanced/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:1445:in `select'
    /home/yahonda/git/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
    /home/yahonda/git/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    /home/yahonda/git/rails/activerecord/lib/active_record/querying.rb:36:in `find_by_sql'
    /home/yahonda/git/rails/activerecord/lib/active_record/relation.rb:555:in `exec_queries'
    /home/yahonda/git/rails/activerecord/lib/active_record/relation.rb:447:in `load'
    /home/yahonda/git/rails/activerecord/lib/active_record/relation.rb:197:in `to_a'
    test/cases/base_test.rb:1145:in `test_find_keeps_multiple_group_values'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
$

Please sign in to comment.