The difference between rake db:migrate
and rake db:reset
is pretty clear in my head. The thing which I don't understand is how rake db:schema:load
different from the former two.
Just to be sure that I am on the same page:
rake db:migrate
- Runs the migrations which haven't been run yet.rake db:reset
- Clears the database (presumably does arake db:drop
+rake db:create
+rake db:migrate
) and runs migration on a fresh database.
Please help to clarify, if my understanding has gone wrong.
db:schema:load creates tables and columns within the (existing) database following schema.rb
db:setup does db:create, db:schema:load, db:seed
Typically, you would use db:migrate after having made changes to the schema via new migration files (this makes sense only if there is already data in the database). db:schema:load is used when you setup a new instance of your app.
I hope that helps.
UPDATE for rails 3.2.12:
I just checked the source and the dependencies are like this now:
db:schema:dump dumps the current env's schema (and seems to create the db as well)
db:setup runs db:schema:load, db:seed
For further information please have a look at https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (for Rails 3.2.x) and https://github.com/rails/rails/blob/v4.0.5/activerecord/lib/active_record/railties/databases.rake (for Rails 4.0.x)
You could simply look in the Active Record Rake tasks as that is where I believe they live as in this file. https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/lib/active_record/tasks/database_tasks.rb
What they do is your question right?
That depends on where they come from and this is just and example to show that they vary depending upon the task. Here we have a different file full of tasks.
https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/Rakefile
which has these tasks.
This may not answer your question but could give you some insight into go ahead and look the source over especially the rake files and tasks. As they do a pretty good job of helping you use rails they don't always document the code that well. We could all help there if we know what it is supposed to do.
TLDR
Use
rake db:migrate
If you wanna make changes to the schemarake db:reset
If you wanna drop the database, reload the schema fromschema.rb
, and reseed the databaserake db:schema:load
If you wanna reset database to schema as provided inschema.rb
(This will delete all data)Explanations
rake db:schema:load
will set up the schema as provided inschema.rb
file. This is useful for a fresh install of app as it doesn't take as much time asdb:migrate
rake db:migrate
makes changes to the existing schema. Its like creating versions of schema.db:migrate
will look indb/migrate/
for any ruby files and execute the migrations that aren't run yet starting with the oldest. Rails knows which file is the oldest by looking at the timestamp at the beginning of the migration filename.db:migrate
comes with a benefit that data can also be put in the database. This is actually not a good practice. Its better to userake db:seed
to add data.rake db:migrate
provides tasks up, down etc which enables commands likerake db:rollback
and makes it the most useful command.rake db:reset
does adb:drop
anddb:setup
It drops the database, create it again, loads the schema, and initializes with the seed data
Relevant part of the commands from databases.rake
As far as I understand, it is going to drop your database and re-create it based on your
db/schema.rb
file. That is why you need to make sure that yourschema.rb
file is always up to date and under version control.