Rails 7 adds database-specific setup and reset tasks for multi DB configurations
source link: https://blog.saeloun.com/2021/10/27/rails-7-adds-database-specific-setup
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
As a developer, we use multiple databases very often. It would be very helpful if we could reset one particular database at a time.
Rails 7 have introduced database-specific tasks for setup and reset under each namespace. We can now reset any specific database. Before this change, we didn’t have the option to perform these tasks on specific databases.
The default setup and reset tasks remain unchanged, and they would work the same as before.
Before
Let’s say we have two databases,
primary
and secondary
in our Rails application.
rails db:reset
and rails db:setup
are the rake tasks to reset and setup databases.
These tasks apply to all the databases present in the application.
rails db:reset
# Drops and recreates all databases from their schema for the current environment and loads the seeds.
rails db:setup
# Creates all databases, loads all schemas, and initializes with the seed data.
There was no task to reset or setup any particular database. If we try to reset or setup any particular database, it would throw an error.
rails db:reset:primary
# Command was not available, and it would throw an error.
rails db:setup:primary
# Command was not available, and it would throw an error.
After
After the changes in Rails 7, we can reset or setup the particular database we want.
Reset
rails db:reset:primary
Drops and recreates the primary database from its schema for the current environment and loads the seeds.
rails db:reset:secondary
Drops and recreates the secondary database from its schema for the current environment and loads the seeds.
Setup
rails db:setup:primary
It creates the primary database, loads the schema, and initializes with the seed data.
rails db:setup:secondary
It creates the secondary database, loads the schema, and initializes with the seed data.
Limitation
db:seed
that is part of the setup would not seed a specific database.
db:seed
didn’t behave in the same line as other tasks.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK