![](/style/images/good.png)
![](/style/images/bad.png)
Changes to behavior in rails db:migrate:name command
source link: https://blog.saeloun.com/2020/09/09/rails-fixes-schema-generation-on-migrating-db-with-name
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.
Changes to behavior in rails db:migrate:name command
Sep 9, 2020 , by Vamsi Pavan Mahesh 1 minute readLet us assume we have a multi-database configuration in our Rails app having a primary and secondary database.
default: &default
adapter: sqlite3
development:
primary:
<<: *default
database: db/development.sqlite3
pool: 10
timeout: 6000
secondary:
<<: *default
database: db/secondary_development.sqlite3
pool: 20
timeout: 10000
Before
In Rails 6,
rails db:migrate
would dump the schemas of all the databases present in database.yml.
In our case, it would generate the following two schema files.
db/schema.rb
db/secondary_schema.rb
Now, with rails db:migrate:primary
,
we would expect the primary database schema dump file to be generated.
But turns out, it is not the case.
One additional inconsistency is,
rails db:migrate
resets the ActiveRecord::Base
connection back to the original configuration,
but rails db:migrate:primary
does not.
The result of ActiveRecord::Base.connection_db_config.inspect
after execution of rails db:migrate
is as follows:
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fdc09a99ee8 @env_name="development", @name="primary", @spec_name="primary", @config={:adapter=>"sqlite3", :database=>"db/development.sqlite3", :pool=>10, :timeout=>6000}, @owner_name=nil
And the result of ActiveRecord::Base.connection_db_config.inspect
after execution of rails db:migrate:secondary
is:
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007f830df39098 @env_name="development", @name="secondary", @spec_name="secondary", @config={:adapter=>"sqlite3", :database=>"db/secondary_development.sqlite3", :pool=>20, :timeout=>10000}, @owner_name=nil>
After
Rails has made changes
to dump schema(or structure) for a database
and reset ActiveRecord::Base
to its original configuration
when db:migrate:name
is executed.
Now we can run rails db:migrate:name
and it will generate the schema file
for the database, we run the migration for.
For instance:
Running rails db:migrate:primary
would generate db/schema.rb
.
Also, the result of ActiveRecord::Base.connection_db_config.inspect
is same
before and after running rails db:migrate:secondary
:
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fa7d9d36ed0 @env_name="development", @name="primary", @spec_name="primary", @config={:adapter=>"sqlite3", :database=>"db/development.sqlite3", :pool=>10, :timeout=>6000}, @owner_name=nil>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK