Using an additional static database for information

I’m running a Rails 6 app with a config that looks this:

default: &default
  adapter: postgresql
  ...

static_default: &static_default
  adapter: sqlite3
  database: db/static/business_data.sqlite3
  migrations_paths: db/static/migrate

development:
  primary:
    <<: *default
    ...
  static:
    <<: *static_default

test:
  primary:
    <<: *default
    ...
  static:
    <<: *static_default

production:
  primary:
    <<: *default
    ... 
  static:
    <<: *static_default

static_default can be thought of as a portable database that we check in to git. It rarely gets updated but some ActiveRecord objects live in there.

I’d like to disable it being affected when doing commands like bin/rails db:reset, bin/rails db:migrate, etc. Has anyone had ways to make this work without overwriting rake tasks in lib/?

Sorry for deviating from the original question, but I wanted to mention that we are using frozen_record for similar purpose - GitHub - byroot/frozen_record: Read only ActiveRecord-like interface to query static YAML files you may want to consider switching to this

2 Likes

I haven’t used this yet but your question made me curious and it looks like there’s a config flag now in at least 7+ to ignore a DB from the regular tasks.

database_tasks: false

Anchor link to specific section in the Rails guides: Multiple Databases with Active Record — Ruby on Rails Guides

1 Like

I think that’s what Shopify uses and that’s what we used at my previous company

I just wanted to say a big “thank you” for mentioning frozen_record. Earlier this year I spent ages searching around for something like this and drew a blank. frozen_record is exactly what I was looking for all along!

1 Like