-
Notifications
You must be signed in to change notification settings - Fork 78
Description
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.sqlite3And 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
)
endExpected 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?
-
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.
-
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.connectionThis 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