Sooner or later every new Ruby developer needs to understand differences between this two common rake tasks. Basically, these simple definition tells us everything we need to know:
rake db:migrateruns migrations that have not run yet
rake db:schema:loadloads the schema.db file into database.
but the real question is when to use one or the other.
Advice: when you are adding a new migration to an existing app then you need to run
rake db:migrate, but when you join to existing application (especially some old application), or when you drop your applications database and you need to create it again, always run
rake db:schema:load to load schema.
I am working on application which use globalize gem for ActiveRecord model/data translations. Globalize work this way:
- first specify attributes which need to be translatable
1 2 3
- then create translation tables
1 2 3 4 5 6 7 8 9 10 11 12
Note that the ActiveRecord model Post must already exist and have listed attributes for translations.
- and run
Problem comes when you change your mind and decide to leave title to be untranslatable.
- remove title from post translations table
1 2 3 4 5 6 7 8 9
- add title to posts table
1 2 3 4 5
- remove title attribute from model translations
1 2 3
- and run
Everything looking good, so where is the problem?
Here it is! If you decide to delete your database and create it again you need to use:
Because, if you try to use
rake db:migrate instead of
rake db:schema:load you will get BIG ERROR!, because for your first migration “create_posts” it is necessary that you have defined translatable attributes :title and :text in Post model, but you removed :title from Post model translations.
So just follow advice above, and good luck.