Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion regarding auto-generated cache_schema.rb #248

Open
davidrossdegroot opened this issue Jan 14, 2025 · 1 comment
Open

Confusion regarding auto-generated cache_schema.rb #248

davidrossdegroot opened this issue Jan 14, 2025 · 1 comment

Comments

@davidrossdegroot
Copy link

davidrossdegroot commented Jan 14, 2025

If your application is adding solid_cache and you have config.active_record.schema_format = :sql set, should the cache_schema.rb file be generated?

I was under the impression that you would not since:

  1. the application's database does not have a corresonding schema.rb
  2. the database.yml files specify a migrations_path which is where you would add the migration to create the solid_cache_entries table, not via the cache_schema.rb file.

db:seed Gets run twice

The reason I bring this up is that if for some reason a developer follows these steps, the db:seed task will be run a 2nd time on the application's database. Here are the steps to reproduce:

  1. rails new solid-cache-app --database=postgresql
  2. rails g model Dog. Add a name attribute to the dog and add this to the seeds.rb file Dog.create(name: "Fido")
  3. Set config.active_record.schema_format = :sql in application.rb
  4. Run rails db:prepare

add cache database steps

  1. Update the database.yml file to include the cache database
development:
  primary: &primary_development
    <<: *default
    database: multiple_db_app_development
  cache:
    <<: *primary_development
    database: multiple_db_app_development_cache
    migrations_paths: db/cache_migrate
  1. Create a migration for the solid_cache_entries.
class AddCacheDatabaseEntries < ActiveRecord::Migration[8.0]
  def change
    create_table "solid_cache_entries" do |t|
      t.binary "key", limit: 1024, null: false
      t.binary "value", limit: 536_870_912, null: false
      t.datetime "created_at", null: false
      t.integer "key_hash", limit: 8, null: false
      t.integer "byte_size", limit: 4, null: false
      t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size"
      t.index %w[key_hash byte_size], name: "index_solid_cache_entries_on_key_hash_and_byte_size"
      t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true
    end
  end
end
  1. Remove the cache_schema.rb file because of the reasons mentioned above.
  2. Run rails db:prepare.

Expect the seed file to not have run on the primary database

Actual results: Seed file was run on primary database and you now have two dogs named Fido.

Environment

Rails version: 8.0.1
solid_cache version: 1.0.6
Ruby version: 3.4.1

@davidrossdegroot
Copy link
Author

davidrossdegroot commented Jan 16, 2025

Looks related to #228 and rails/rails#53348

I can't tell if Rails main has the fix or not but at any rate, maybe there's still some clarification of the README that could be done for those that use config.active_record.schema_format = :sql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant