Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is rake db:migrate the correct command to re-sync schema.rb with your database schema?

I ran "rake db:migrate" to re-sync schema.db with my database schema. But it failed, saying that one of my tables already exists. I think it was trying to re-create the table. If you just want to get schema.rb updated to reflected any changes you made in the database independently of Rails, what command should you use if not "rake db:migrate"? And what's the best source of documentation on this type of thing?

like image 684
drizzle Avatar asked May 08 '09 22:05

drizzle


People also ask

What does db Migrate do?

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.

How rails db Migrate works?

When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration.

What does rails db Reset do?

db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop , db:create , db:migrate tasks. db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran. If you want to undo previous n migrations, pass STEP=n to this task.

How rollback rails db Migrate?

To check for status, run rails db:migrate:status . Then you'll have a good view of the migrations you want to remove. Then, run rails db:rollback to revert the changes one by one. After doing so, you can check the status again to be fully confident.


5 Answers

"rake db:migrate" will try and run all the outstanding migrations for your project. If you just want to dump the schema do a "rake db:schema:dump".
But I think you have a problem where it says that the table already exists. One of your migrations is failing because the table it is trying to add already exists in your db. Did you create one by hand? Did you down a migration, but not have a down written for it? You will need to fix this before you can write future migrations. If it is just a mistake, and the table is there and correct and you want to ignore this. My recommendation is to hack around it by commenting out the create table in the failing migration. Then run "rake db:migrate". Then but the create table back. This will update your schema version.
Make sure you write proper downs on all migrations.

like image 119
scottd Avatar answered Oct 11 '22 17:10

scottd


Try

RAILS_ENV=development rake db:drop

before

RAILS_ENV=development rake db:migrate

and be happy!

Making sure that you run it on your test or development environment since this will drop the database/tables

like image 21
crmoreira Avatar answered Oct 11 '22 18:10

crmoreira


I've found that occasionally, when things get a little weird, you'll find yourself in a situation where Rails will want to run a migration that it ought rightly to be considering already done (the table already exists, etc). You can mark a migration as done by finding its number (the number part at the beginning of the filename), going into mysql and issuing a query like so:

insert into schema_migrations values('20090521153438');

(or whatever the number of your migration is)

Or if it's a plugin migration being run using Desert's migrate_plugin:

insert into plugin_schema_migrations values('my_plugin', '005');
like image 42
hoff2 Avatar answered Oct 11 '22 18:10

hoff2


rake db:migrate:reset will drop all your tables, run all the migrations and create a new schema.rb file.

like image 27
Mike Avatar answered Oct 11 '22 17:10

Mike


Try rake db:schema:dump or rake db:migrate:redo.

like image 28
pts Avatar answered Oct 11 '22 18:10

pts