cache and uncached do not work when using DATABASE_URL (such as on Heroku) #6951

Closed
jjb opened this Issue Jul 3, 2012 · 12 comments

Comments

Projects
None yet
5 participants
Contributor

jjb commented Jul 3, 2012

ActiveRecord::Base cache and uncached are only live when there are configurations present:

def cache(&block)
  if ActiveRecord::Base.configurations.blank?
    yield
  else
    connection.cache(&block)
  end
end

def uncached(&block)
  if ActiveRecord::Base.configurations.blank?
    yield
  else
    connection.uncached(&block)
  end
end

However, when using DATABASE_URL to specify the database configuration, ActiveRecord::Base.configurations is never populated:

initializer "active_record.initialize_database" do |app|
  ActiveSupport.on_load(:active_record) do
    db_connection_type = "DATABASE_URL"
    unless ENV['DATABASE_URL']
      db_connection_type  = "database.yml"
      self.configurations = app.config.database_configuration
    end
    Rails.logger.info "Connecting to database specified by #{db_connection_type}"

    establish_connection
  end
end

Which means that explicit calls to cache and uncached are never live on such environments. Such as Heroku.

I checked to make sure that ActiveRecord::Base.configurations wasn't being populated elsewhere in the stack, and it's not. In my local console, it has configurations. In the heroku console, it's empty.

This theory is consistent with a bug that I am seeing in one of my applications. However it seems pretty unlikely that this wouldn't have been noticed sooner by many many people. So maybe there is something particular about my environment. However this does seem to still be the cause and probably not an ideal way to check if ActiveRecord is configured.

Contributor

wuputah commented Jul 4, 2012

cc/ @hone

Contributor

jjb commented Jul 5, 2012

looks like @jeremy wrote the relevant querycache code in 2007, and @tpope added the ActiveRecord::Base.configurations code in april of 2012.

Contributor

frodsan commented Oct 29, 2012

@jjb Is this still an issue? Could you provide a sample app or a test case that reproduces this problem?

Contributor

jjb commented Oct 29, 2012

The code is the same in master. You can see here cache and uncached do not do anything if the configurations array is empty: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/query_cache.rb#L7

And here the configurations array is not populated if ENV['DATABASE_URL'] is present: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railtie.rb#L103

If this is not enough explanation and you would like an example app let me know.

Contributor

kennyj commented Oct 30, 2012

I sent the PR #8074. Please confirm it :)

Contributor

jjb commented Oct 30, 2012

looks good

On Oct 30, 2012, at 1:27 PM, kennyj notifications@github.com wrote:

I sent the PR #8074. Please confirm it :)


Reply to this email directly or view it on GitHub.

@kennyj kennyj closed this in a7c3c90 Oct 31, 2012

rafaelfranca added a commit that referenced this issue Oct 31, 2012

Merge pull request #8074 from kennyj/fix_6951
Fix #6951. Use query cache/uncache, when using not only database.yml but also DATABASE_URL.
Contributor

jjb commented Oct 31, 2012

any chance to get this into 3.2?

Owner

rafaelfranca commented Oct 31, 2012

@jjb I'll do that, but only after the 3.2.9 release

Contributor

kennyj commented Oct 31, 2012

If necessary, I'll try to backport this tonight (jst-9).

Contributor

kennyj commented Oct 31, 2012

oops. at the same time...

@rafaelfranca one question. Should I add a CHANGELOG entry to 3.2 ? (and remove the entry on master ?)

Owner

rafaelfranca commented Oct 31, 2012

No. both are necessary.

Contributor

kennyj commented Oct 31, 2012

Okay.

I prepare backported fix on my box.
If 3.2.9 is released, I'll send pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment