Leslie Tate
Leslie Tate

Reputation: 570

Rails: How to delete a pending migration

I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.

I am trying to save data into the database. However, when I run: rails server I get the following error:

Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

I've looked at the other articles and when I run:

bin/rake db:migrate 

I get a rake aborted!

After running:

rake db:abort_if_pending_migrations....

I see that:

You have 1 pending migration:
20150805200129 CreateDatabases

SQLite3::SQLException: table "databases" already exists:

and it just tells me to run rake db:migrate to start again.

It seems that it already exists. Is there a way to cancel the pending migration?

Upvotes: 27

Views: 32992

Answers (6)

EugZol
EugZol

Reputation: 6545

Your migration may have failed midway (so it created the table, but didn't finish).

You are just using development environment, so it's okay to just drop the database and rebuild it from scratch:

rake db:drop # THIS WILL DELETE YOUR DATABASE
rake db:create
rake db:migrate

Upvotes: 17

mechnicov
mechnicov

Reputation: 15258

You can recreate database and run all migrations in your development environment with such command

rails db:migrate:reset

Upvotes: 0

zawhtut
zawhtut

Reputation: 8551

I do not encourage to drop the database and start from the beginning especially when you already have the data inside the database.

My approach to this will be migrate first, then rollback. After that you can safely delete the migration file. So the procedure is as following.

rails db:migrate
rails db rollback
rm db/migrate/your_last_migration_file.rb

Upvotes: 1

curt
curt

Reputation: 4582

If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.

Do not use this method if you rely on Rails to maintain your database structure!

I keep my Rails structure file up to date by building it from the database using:

bundle exec rake db:structure:dump

Upvotes: 3

Rakshit Saxena
Rakshit Saxena

Reputation: 1

If you want to revert the wrong migrations, You can drop the whole db using this:

rake db:drop

Then remove the migrations file manually(This wont corrupt the db when you recreate as the Schema migrations would be dropped as well).

Then run

rake db:migrate

And if there is data to be seeded, then run this as well

rake db:setup

Upvotes: -3

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

Sometimes, even dropping a local development database is not a good idea. There are better ways to delete/destroy a specific migration in your Rails application.

You could use rails d migration command to destroy a particular migration:

rails d migration MigrationName

To undo the changes corresponding to a particular migration, you can use db:migrate:down method like this:

rake db:migrate:down VERSION=XXX

Sometimes, things could get more messy and in those situation another handy thing is to take a look at the schema_migrations table in your database which has all the migrations with their version saved in it.

You can delete a particular migration from this table like this:

delete from schema_migrations WHERE version = VERSION;

if you don't want that migration to be present anymore.

Upvotes: 44

Related Questions