Pending Migration Error | Cannot Remove Dupe Migrations - ruby-on-rails

I am trying to run a rails server and am coming across this error:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
The DB in question is currently empty so no worries of losing data. When I run the code:
bundle exec rake db:migrate
RAILS_ENV=development rake db:migrate
rake db:migrate
I am returned the error:
rake aborted!
ActiveRecord::DuplicateMigrationNameError:
Multiple migrations have the name CreatePosts
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
When running the server, this is returned to my terminal:
Started GET "/" for ::1 at 2015-09-22 11:30:34 -0400
ActiveRecord::PendingMigrationError (
Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=development
):
And finally, running rake db:migrate:status returns:
Schema migrations table does not exist yet.
What could possibly be going on? Any help is greatly appreciated!
Output of migrate:status after bin/rake command
Status Migration ID Migration Name
--------------------------------------------------
up 20150922142819 Devise create users
up 20150922143253 Create posts
down 20150922143414 Create posts
down 20150922145906 Acts as votable migration
down 20150922150209 Create comments
down 20150922151035 Acts as follower migration

You must create the database schema/structure before you run migrations. db:schema:load will do this for you:
bin/rake db:schema:load
As for the error "Multiple migrations have the name CreatePosts" - it's as it says. Look in the contents your db/migrate folder for two files that have the same name class CreatePosts within the files - they should be named differently, or the second one should be removed if they are duplicates.

It seems like you have more than one migration with the same name. This is not allowed. If your database is empty, no tables, the best plan is to rename the second one so they don't conflict any more.
Obviously the filenames are unique, but each migration file defines a class, and these classes must be unique as well, plus should match the filename.
Having 201509201949343_create_posts and 201509220293910_create_posts would be a typical conflict here.
You may want to investigate how this came to pass, as having your development schema diverge from the production one is generally a bad idea. Once you resolve the conflict locally, try and update the schema tracking table on production accordingly.

Related

Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development error when running as server

I have tried to run as a server before and it worked well, I don't change anything in the database but when I tried to run again a have this message Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
i tried as this site link
but it failed
any help please
i am a beginner in ruby on rails
Migration files live in the db/migrate directory, and their names look like this:
20150121164407_create_comments.rb
From your comment, it looks like inside your db/migrate directory, you have several migrations with the same “basic” name create_comments (and probably different timestamps).
You need to remove one of those duplicated files.
If you did and if you don't have any important data in the database, you should destroy your database and recreate it using existing migrations. These commands in this order:
$ rake db:drop
if you're using postgres
$ rake db:create
and finally remigrate
$ rake db:migrate

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?
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.
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
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
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
You can recreate database and run all migrations in your development environment with such command
rails db:migrate:reset
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

Rails: I modified my migration file (Bad, I know)

I recently started using Rails, and created a few Models using the CLI which in turn created some migrations.
I ran the rake db:migrate command after adding all my columns in there, and then realized that I'd left out the associations.
So what did I do?
I went ahead and edited the migrations to include those keys.
I ran rake db:migrate again, and nothing changed in the schema.
Then I ran rake db:reset and then rake db:setup.
When that didn't work, I deleted my schema.rb (the darn thing wouldn't get updated!) and tried recreating it. When I realized that didn't work, I dropped the database, and killed the schema.
Now I'm stuck with some manually modified migrations, no schema.rb and no database.
How do I get the modified migrations to generate a schema, and play nice with Rails?
In development it does not matter to drop and rebuild your database. I do it often and I even have a rake task for that. The 3 command to chain are:
rake db:drop
rake db:create
rake db:migrate
# And a 4rth optional command to rebuild your test database
rake db:test:prepare
With this you should be good
Next time you need to modify a migration manually after migrating it, you should process by:
rake db:rollback
edit your migration
rake db:migrate
Following those steps will save you some headaches
Bonus info:
After you deployed your migration to your production server you cannot manually modify it, hence you must write another migration that will perform the modification (adding columns, etc...)

Rails pending migration in rake db:test:prepare

I've run rake db:migrate and all of my migrations ran. However, when I try to run rake db:test:prepare I get the error:
You have 1 pending migrations:
20130724211328 CreateGalleries
Run `rake db:migrate` to update your database then try again.
Then running rake db:migrate again gives the error:
PG::Error: ERROR: relation "galleries" already exists...
But in the console I can create and manipulate the Gallery model exactly as shown in the CreateGalleries migration. The table is not being created or even mentioned in any other migrations.
It seems the migration ran just fine but did not register. Any ideas how to fix this?
EDIT
I solved this with rake db:drop db:create db:migrate then rake db:test:prepare, but I'm happy to give the solution to anyone who can shed light on what caused the problem in the first place.
I suspect the migration for galleries hasn't been executed properly. If you're 100% sure everything is right in your table, you can bump up the migration version to the version of the galleries migration.
To do this, find the timestamp of your galleries migration (the 14 numbers in front of your migration file, in this case 20130724211328) and insert this as a new row into the table schema_migrations (which is done automatically by Rails after successfully executing a migration).
If the table is empty, you could also drop the table galleries and run rake db:migrate again. This way you can also make sure your migration doesn't trigger any errors.

What does rake db:test:prepare actually do?

I am following the rails tutorial videos and I can't figure out what the db:test:prepare command actually does. Can someone provide an explanation?
The rake db:migrate above runs any pending migrations on the
development environment and updates db/schema.rb. The rake
db:test:load recreates the test database from the current
db/schema.rb. On subsequent attempts, it is a good idea to first run
db:test:prepare, as it first checks for pending migrations and warns
you appropriately.
-- http://guides.rubyonrails.org/testing.html
Basically it handles cloning the database so you don't have to run the migrations against test to update the test database.
Specifically, rake db:test:prepare will do the following:
Check for pending migrations and,
load the test schema
That is, it will look your db/schema.rb file to determine if any migrations that exist in your project that have not been run. Assuming there are no outstanding migrations, it will then empty the database and reload it based on the contents of the db/schema.rb file.
rake db:test:prepare is a good solution for PG issues like this.
“PG::UndefinedTable: ERROR: relation does not exist” with a correct Rails naming and convention" where I couldn't just execute rake db:migrate RAILS_ENV=production
When, for example you can't create test database for a bug discussed here: "PG undefinedtable error relation users does not exist"
All arround this error
"PG::UndefinedTable: ERROR: relation xxxxx does not exist”

Resources