2015년 11월 30일 월요일

Difference between rake db:migrate db:reset and db:schema:load

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 a rake 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.
shareimprove this question
2 
Does rake --tasks help ? – zx1986 Nov 28 '14 at 2:42

4 Answers

up vote654down voteaccepted
  • db:migrate runs (single) migrations that have not run yet.
  • db:create creates the database
  • db:drop deletes the database
  • db:schema:load creates tables and columns within the (existing) database following schema.rb
  • db:setup does db:create, db:schema:load, db:seed
  • db:reset does db:drop, db:setup
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:create creates the database for the current env
  • db:create:all creates the databases for all envs
  • db:drop drops the database for the current env
  • db:drop:all drops the databases for all envs
  • db:migrate runs migrations for the current env that have not run yet
  • db:migrate:up runs one specific migration
  • db:migrate:down rolls back one specific migration
  • db:migrate:status shows current migration status
  • db:rollback rolls back the last migration
  • db:forward advances the current schema version to the next one
  • db:seed (only) runs the db/seed.rb file
  • db:schema:load loads the schema into the current env's database
  • 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
  • db:reset runs db:drop db:setup
  • db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration
  • db:migrate:reset runs db:drop db:create db:migrate
shareimprove this answer
10 
3 
@cutation: db:setup does surely not run db:migrate, because it would be much too brittle to run all migrations just for a db setup (this is what schema.rb is for). – moritz Apr 25 '12 at 9:10
2 
I'm executing db:reset and it's seeding my db. Why could it be? – Alejandro Riedel Feb 11 '13 at 0:17
1 
@AlejandroRiedel: Please have a look at the update. – moritz Feb 20 '13 at 13:09
   
@moritz: it makes sense now. Thanks for the heads up. – Alejandro Riedel Feb 22 '13 at 2:44
As far as I understand, it is going to drop your database and re-create it based on your db/schema.rbfile. That is why you need to make sure that your schema.rb file is always up to date and under version control.
shareimprove this answer
You may find all the db do stuff in this file on github these days.
If you really want to have some SQL fun try doing a structure dump.
shareimprove this answer
Thanks for valuable post. As I found from this discussion there is only one way to recreate DB from migrations sequence i.e. NOT form db/schema.rb. This way is the following:
rake db:drop && rake db:create && rake db:migrate && rake db:seed
shareimprove this answer
   
Precisely. Not sure why its posted as an Answer, instead of a comment! – Gaurav Agarwal Sep 4 at 9:00
1 
Or even shorter - rake db:drop db:create db:migrate db:seed – Anton Antonov Sep 14 at 16:31

Your Answer

댓글 없음: