Rails tests not running due to apparently pending migrations - ruby-on-rails

I have a very strange problem in which running rails test seems to be dropping a table in my test database. I can drop, create and migrate a database, then use psql to check the correct tables are there. If I then run rails t however, I am told ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "groups" does not exist, and checking with psql shows that the table is indeed now missing. Rails then tells me I have 24 pending migrations, which is all my migrations.

I fixed with
rails db:setup --trace

Related

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 Migration Error on Git Project

I am learning Ruby on Rails, an am getting an error creating the database. I run the following command from console:
rake db:create db:migrate db:seed
And get:
== 20140328232600 AddAuthLevelToUser: migrating ===============================
-- add_column(:users, :auth_level, :Integer)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "users" does not exist
I looked at the error message on the webpage, and tried running:
'bin/rake db:migrate RAILS_ENV=development'
As suggested, but had little luck.
The project I am working on had been started by another team of developers, so I pulled it off git... Any suggestions?
Cheers
As per the error, you are running the migration AddAuthLevelToUser when you don't even have users table in database.
Firstly, verify if you have migration for users table, if not then create one.
If yes, then check that migration for users table has a VERSION NUMBER lower than that of AddAuthLevelToUser. Fix it and run the migrations.
Try running just rake db:create at first. What database is your project using? Work this out and use the database client to connect to the database on your local system, and verify whether the users table exists on the database. It's possible that the db:create job is not correctly creating all the tables necessary for the database schema.

How to reset synch with a database on rails?

I am having a lot of issues with rails migration mechanism.
I think I have run a migration file and has been executed partially.
So when I am trying to run
rake db:migrate
again it gives me an error the column name already exist.
I am trying to reset it with
rake db:reset
and gives me an error
Unknown database 'databasename'
Is there a way to reset the whole mechanism ?
Is it a good idea to manually drop all tables and try to run rake db:migrate again ?
You should change the database name in your database.yml.

Heroku DB Migration Error

Getting a migration error when I try to migrate my database on Heroku. Found a solution on here offering this advice:
rake db:create
rake db:schema:load
rake db:migrate
but it hasn't made a difference. The error starts like this:
PG::UndefinedColumn: ERROR: column "property_id" of relation "bookings" does not exist
I don't have a property_id column anymore as this got changed in later migrations locally.
My migrations work locally by the way so why not on Heroku?
Could you post your Schema so I can see if you have a column "property_id".
If you do not then you will have to add this column property_id to the bookings class.
Also try
rake db:drop
rake db:migrate
what happens?
Did you migrate data to or from your heroku postgres database and your local one? It seems like the schema_migrations table is out of sync with reality on your heroku instance.
If you can lose all data, I would just start over. The following command will wipe your entire database, destroy all tables:
heroku pg:reset # destructive action, careful
After that, try heroku run rake db:migrate again; it should work.
If that does not work, then you'll probably have to manually inspect the schema_migrations table and make sure that the correct migrations have been applied and perhaps manually put it back in a consistent state.
This situation is quite abnormal and can only have happened by manually touching this data or schema. Because postgres supports transactional DDL, it cannot have happened via heroku run rake db:migrate alone, as in case of errors the whole migration rolls back and the database is left in a consistent state.

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.

Resources