Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback to beginning and recreate/rebuild new migrations

So this is my first real Ruby on Rails project. I've learned my lesson -- I didn't make all changes using migrations so things are a bit messed up.

What's the best way to start over with new migration files and rebuild the schema, etc? My project is too far along to rebuild the entire project, but not far enough along to where I care about losing the migrations I have thus far. I also don't mind losing the data in the database. I was trying to rollback to the beginning but some of it is failing.

I know this is a bad state to be in, but lesson learned.

EDIT: I just deleted all the migrations files and rebuilt the schema file with db:schema:dump. I assume this puts me in a clean state with my existing database, just lost migrations.

like image 254
99miles Avatar asked Feb 10 '10 15:02

99miles


People also ask

How do I rollback migration?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements. where n is number of migrations to rollback, counting from latest migration. where xxxxx is the version number of the migration.

How do you rerun all migrations?

just use rake db:reset , that will drop your database (same as undoing all migrations) and reset to the last schema. UPDATE: a more correct approach will be using rake db:migrate:reset . That will drop the database, create it again and run all the migrations, instead of resetting to the latest schema.

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 do I delete a migration?

To delete a migration created using Mover, do the following: Click Migration actions. From the dropdown menu, select Delete migration.


1 Answers

if you want to migrate some steps back you can

rake db:rollback STEP=2

That command will migrate your database 2 migrations back. If you need more help with rake commands, jus type

rake -T

That command will list all the tasks you have in you application.

like image 112
Boris Barroso Avatar answered Nov 16 '22 03:11

Boris Barroso