rake db:schema:load
will load a schema.rb
file into a rails database. Is there a way to load a structure.sql
file into the database through rake or do I just need to do this manually?
Unlike rake db:migrate that runs migrations that have not run yet, rake db:schema:load loads the schema that is already generated in db/schema. rb into the database. Always use this command when: You run the application for the first time.
Use rake db:structure:load , which will load db/structure. sql . In order to specify the path of SQL file, use the SCHEMA environment variable (not DB_STRUCTURE ) like rake db:structure:load SCHEMA=db/another. sql .
A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.
structure. sql differs from schema. rb in the following ways: It allows for an exact copy of the database structure. This is important when working with a team, as well as if you need to rapidly generate a new database in production from a rails db:setup task.
Use rake db:structure:load
, which will load db/structure.sql
.
[Update]
If you want to load another file, you can specify its path via
SCHEMA
environment variable (Rails 5.0 or later)DB_STRUCTURE
environment variable (Rails 4.x)For example, run
rake db:structure:load SCHEMA=db/another.sql
or
rake db:structure:load DB_STRUCTURE=db/another.sql
Just use
rake db:setup
which will use either schema.rb
or structure.sql
depending on your configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With