Can't run DB migration on PostgreSQL - ruby-on-rails

I've just changed from sqlite3 to PG and after creating a database via pgAdmin and trying to run a migration, I run into the following, which I don't understand.
Pawel:bodb pawel$ rake db:create
DEPRECATION WARNING: Rake tasks in /Users/pawel/Ruby/apps/bodb/vendor/plugins/google_charts_on_rails/tasks/google_charts_on_rails_tasks.rake are deprecated. Use lib/tasks instead. (called from /Users/pawel/Ruby/apps/bodb/Rakefile:7)
firstdb already exists
Pawel:bodb pawel$ rake db:migrate
DEPRECATION WARNING: Rake tasks in /Users/pawel/Ruby/apps/bodb/vendor/plugins/google_charts_on_rails/tasks/google_charts_on_rails_tasks.rake are deprecated. Use lib/tasks instead. (called from /Users/pawel/Ruby/apps/bodb/Rakefile:7)
== AddLikesToUsers: migrating ================================================
-- add_column(:Users, :likes, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: relation "Users" does not exist
: ALTER TABLE "Users" ADD COLUMN "likes" character varying(255)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

I don't know about the warnings, but the error message says:
relation "Users" does not exist
Maybe you are using upper case "Users", where the table's name is users? Identifiers in PostgreSQL are case insensitive as long as they are not double-quoted.

It appear that the migration assumes that an Users table already exists which is not the case with a completely new PostgreSQL database...
Did you forget to add some start schema to the database?

Related

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.

db:migrate error during rake on Rails 4.0.0

Working on project in Rails 4.0.0, ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin10.8.0], with Rake 10.1.1
I was working on an app for a class in Rails, I made an error and deleted the changes made while in Git. When I went to redo the project, and ran the rake DB migration, I was given the following error message:
Joses-MacBook-Air:crumblr JRV$ rails generate migration CreateHearts Post_id:integer
invoke active_record
create db/migrate/20140120235500_create_hearts.rb
Joses-MacBook-Air:crumblr JRV$ bundle exec rake db:migrate
== CreateHearts: migrating ===================================================
-- create_table(:hearts)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "hearts" already exists: CREATE TABLE "hearts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "Post_id" integer) /usr/local/rvm/gems/ruby-1.9.3- p392/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
4.0.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
How do I get rid of the old table reference?
There are a couple of commands you can use:
rake db:rollback will rollback the latest migration
rake db:rollback STEP=3 allows you to rollback by more than 1 migration (3 in this example)
rake db:reset will drop the database, recreate it and load the current schema into it.
and if you want to rollback then migrate back up again you can use
rake db:migrate:redo STEP=3 rollback 3 migrations and redo the migration
http://guides.rubyonrails.org/migrations.html

Heroku db:migrate adding migrations before table

I made a rails 4 app with the default sqllite. But on heroku when I run my first migration I get errors, meanwhile everything works flawlessly locally:
$ heroku run rake db:migrate
Error:
Running `rake db:migrate` attached to terminal... up, run.3709
Migrating to AddIndexToUserName (20131003064019)
== AddIndexToUserName: migrating =============================================
-- add_index(:users, :name, {:unique=>true})
PG::UndefinedColumn: ERROR: column "name" does not exist
: CREATE UNIQUE INDEX "index_users_on_name" ON "users" ("name")
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::UndefinedColumn: ERROR: column "name" does not exist
: CREATE UNIQUE INDEX "index_users_on_name" ON "users" ("name")/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `exec'
/
This is my database.yml
development:
adapter: postgresql
encoding: unicode
database: poets_app_development
pool: 5
username: alain
password: some_password
So, if I were to debug this I would do two things.
Use the same DB in development as you would in production(best practice and will save time in the long run).
Try dropping the DB and re-running the migration locally. I suspect that this will fail as well.
rake db:reset
rake db:migrate
It seems like your migrations might be out of order or referencing something that is not yet in the database. This should throw an error locally as well.

Error with heroku run rake db:migrate

I am trying to run the command on Heroku
Heroku run rake db:migrate
but I get the error:
Migrating to AddNameToUsers (20130320002032)
== AddNameToUsers: migrating =================================================
-- add_column(:users, :name, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "users" does not exist
: ALTER TABLE "users" ADD COLUMN "name" character varying(255)
This might have to do with the fact that I had some issues with the migration files on my local server. I managed to work around it and had to delete one of the files, but I worry that I might have deleted something I need that hadn't been migrated to heroku's database?
my github for the account is https://github.com/jeremybelcher/omrails
Any help is appreciated
Your previous migrations are missing.
You can do:
rake db:create
rake db:schema:load
rake db:migrate
Which will recreate your database based on your schema.rb file.

Need help migrating database from development to test using Rails

Similar questions has been asked a lot. But, I think my situation is a bit different. I pulled a db from Heroku (I use sqlite3 in prod) to my local machine (uses PostgreSQL ). The prod db didn't have any data at that time.
After that time I haven't done any db migrations to test. Now I have a couple of fields in my dev db. However, when I go to test console to see what I have (referring a User table) User.count returns 0, but returns 2 users in dev.
So I did rake db:migrate RAILS_ENV=test there was neither an error message nor a success message. I thought it was success and checked the test db if it has those 2 users and still nothing. Then I tried bundle exec rake db:test:prepare. This was the output
**You have 4 pending migrations:
20120415210441 CreateUsers
20120418064930 AddIndexToUsersEmail
20120419034627 AddPasswordDigestToUsers
20120504031144 AddRememberTokenToUsers
Run `rake db:migrate` to update your database then try again.**
What is the solution?
EDIT:
FYI: I am using windows
I also run rake db:migrate and I got this error.
$ rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id"
INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varcha
(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
The error message indicates that your dev and test migrations are out of sync.
First run migrate on dev. This will update dev with all your recent migrations.
rake db:migrate
Then run db:test:prepare. This will make the test database the same schema as dev.
rake db:test:prepare
Now, understand that db:test:prepare makes test have the same schema as dev, but with no records. To populate records in test, implement fixtures or factories (see the factory-girl gem). Factories are generally called in your test to create records for that test.
That's another question and another answer.
You should drop the table and re-run the migrations.
If you don't know how to drop a table in SQLite, then
comment out the create_table method call in the 20120415210441_CreateUsers.rb.
Run > rake db:migrate VERSION=20120415210441
Run > rake db:migrate VERSION=??? where ??? is the version of the migration prior to CreateUsers.
Uncomment the code in 20120415210441_CreateUsers.rb
Run > rake db:migrate

Resources