Skip to content

SolidCache::Entry doesn't respect cache: database configuration from database.yml #246

@konung

Description

@konung

Description

When using solid_cache with a different database for caching (e.g., PostgreSQL for main app, SQLite3 for cache), the SolidCache::Entry model doesn't automatically use the cache: configuration from database.yml. Instead, it inherits the default database connection (primary database).

Current Behavior

With the following configuration:

# config/database.yml
default: &default
  adapter: postgresql
  # ... PostgreSQL config

development:
  primary:
    <<: *default
    database: my_app_development
  cache:
    adapter: sqlite3
    database: storage/development_cache.sqlite3

And application config:

# config/application.rb
config.solid_cache.store_settings = {
  database: "storage/development_cache.sqlite3",
  adapter: "sqlite3"
}

SolidCache::Entry still connects to PostgreSQL instead of SQLite3:

SolidCache::Entry.connection
#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:...>

Workaround

Currently requires an explicit initializer to force the correct database connection:

# config/initializers/solid_cache.rb
Rails.application.config.to_prepare do
  SolidCache::Entry.establish_connection(
    adapter: "sqlite3",
   database: Rails.root.join("storage/#{Rails.env}_cache.sqlite3").to_s # This needs to match database.yml config for cache db
  )
end

Expected Behavior

SolidCache::Entry should automatically use the database configuration from the cache: section in database.yml when it exists, similar to how other Rails engines handle multiple databases.

Suggestions?

  1. If this is expected behavior, it would be helpful to document it in the README or guides, specifically:

    • That solid_cache requires explicit connection setup when using a different database, if different from main setup
    • The recommended initializer pattern shown in the workaround above? Don't know if this is the best approach, but open to suggestions.
  2. Documenting simple ways to inspect the current solid_cache connection and configuration for debugging (took me digging through the code to find this.

# Verify actual database connection
SolidCache::Entry.connection.instance_variable_get(:@config)
# or
SolidCache::Entry.connection

This would help users debug their setup and verify which database solid_cache is actually using.

Environment

  • Rails version: 7.2.2.1
  • solid_cache version: 1.0.6
  • Ruby version: 3.3.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions