Setup Rails 8 w/ SQLite & Solid Cable, Cache & Queue
Let's say I want this setup:
SQLite
4 databases:
primary
cache
queue
cable
I want to match
development
environment toproduction
environment in regards to databases.
New Rails App
rails new projectname --css=tailwindcss --skip-kamal --skip-ci
After initial creation, you should have:
These schemas with definitions for tables for eacH:
db/cable_schema.rb
db/cache_schema.rb
db/queue_schema.rb
Nothing in /storage directory yet (databases haven't been created yet).
Before you create databases, first setup database.yml:
Database.yml
If you also have a staging environment, then copy the production block to a staging block.
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
primary: &primary_development
<<: *default
database: storage/development.sqlite3
cache:
<<: *default
database: storage/development_cache.sqlite3
migrations_paths: db/cache_migrate
queue:
<<: *default
database: storage/development_queue.sqlite3
migrations_paths: db/queue_migrate
cable:
<<: *default
database: storage/development_cable.sqlite3
migrations_paths: db/cable_migrate
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: storage/test.sqlite3
# Store production database in the storage/ directory, which by default
# is mounted as a persistent Docker volume in config/deploy.yml.
production:
primary:
<<: *default
database: storage/production.sqlite3
cache:
<<: *default
database: storage/production_cache.sqlite3
migrations_paths: db/cache_migrate
queue:
<<: *default
database: storage/production_queue.sqlite3
migrations_paths: db/queue_migrate
cable:
<<: *default
database: storage/production_cable.sqlite3
migrations_paths: db/cable_migrate
Create the 4 databases
rails db:create
This should create 4 sqlite databases in storage folder:
storage/development.sqlite3
storage/development_cable.sqlite3
storage/development_cache.sqlite3
storage/development_queue.sqlite3
Run the first migration
Note: You don't yet have the migration folders inside of db/ folder and you don't have any migration files yet. That's OK.
rails db:migrate
The first time you run rails db:migrate, it should load the schemas for the 3 solid databases (queue, cable, cache).
Connect to each database using TablePlus
In TablePlus, create a new connection using SQLite. Simply browse for the sqlite folder in the storage directory, then connect.
You should expect to see the necessy tables for each solid database (like the tables for solid queue etc.).
Creating migrations in each database
By default, when running rails g migration ...
this will create a migration file in db/migrate
folder.
As of this writing, I'm unaware of a way to generate migration files in the other files, so if you need to migrate tables in any of the other 3 tables, then you have to create the migration file normally, then manually move it out of the db/migrate folder and into the appropropriate folder. For example, if migrating something in the solid queue database, move the migration to the db/queue_migration folder.
To run migrations in a specific database, append it to the command. Examples:
rails db:migrate:queue
rails db:migrate:cable
rails db:migrate:cache
Last updated