Editing migrations after deployment to production - ruby-on-rails

I'm finishing a major refactoring of an app and I am trying to clean up the migrations. I've made the changes and everything works great locally after reseting and re-migrating the database.
On production I can't get the migrations to run. I've tried every combination of reset, drop, etc. that I can think of but I keep getting this error.
It seems like the production database isn't being reset which is causing the migration to break. Any tips would be appreciated.
== AddFieldsToUsers: migrating ===============================================
-- add_column(:users, :company, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: company: ALTER TABLE "users" ADD "company" varchar(255)

For resetting the database you can drop and migrate the database again in production mode.
rake db:drop db:create db:migrate RAILS_ENV=production
And, if you want to edit some particular migration, you can reset it using its version no.
rake db:migrate:down VERSION=<version no.> RAILS_ENV=production
Edit the migration file, and then
rake db:migrate:up VERSION=<version no.> RAILS_ENV=production

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.

Rake db:migrate - how do I undo all migrations and redo them

Is there a quick rake db:rollback command for all of the migrations?
Rolling back all migrations
To rollback all migrations the best solution is the one #Claudio Floreani proposed:
rake db:migrate VERSION=0
This will rollback every migration. You can read why this is the best approach in his answer. Then, run all migrations again with
rake db:migrate
Resetting the database
Reset
rake db:migrate:reset #runs db:drop db:create db:migrate
This method drops the database and runs the migrations again.
Loading the last schema
rake db:reset
This method will drop the database and load the data from the last schema.
You can see more information in this post: Difference between rake db:migrate db:reset and db:schema:load
Thanks to #Claudio Floreani and all the users who commented to improve the answer.
If you really want to rollback all of the migrations, and not just take the database to a pristine state or to the last schema, you have to run:
rake db:migrate VERSION=0
This will actually rollback all the way down every migration and ensure that every migration is reversible.
If you now issue
rake db:migrate:status
you will see that all the migrations are still there but they are in a 'down' (not applied) state.
Other commands that imply a rake db:reset or rake db:drop (such as in the answers by #Orlando or #Alex Falke) won't do any rollback at all: that is, they won't ensure that every migration is reversible.
Moreover, rake db:drop cannot be run while the database is being accessed by other users, while rollbacks can be performed live (even if this is generally not recommended). And last but not least, simply dropping and recreating the database will also delete the schema migrations table: if someone runs rake db:migrate:status after the database has been dropped, he will be replied with "Schema migrations table does not exist yet" and will have no clues about which migrations can be applied (unless he knows it yet or can list them).
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.
If a permission problem raises (like it happened to me), maybe you could try dropping all database tables like I did with rubymine (just open database tool window, select all tables and right-click -> drop), it should be similar with other IDEs. Some tables like sqlite_master and sqlite_sequence were conveniently ignored in the drop.
This allowed me to do
rails db:migrate
and everything worked fine. Of course you loose all data!

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.

seems that rails is replay a migration

when I new a migration and run it,error occurred:
$ rake db:migrate
== CreateEReadings: migrating ================================================
-- create_table(:e_readings) rake aborted! An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "e_readings" already exists
while this e_readings is the last table I created using migration.
The migration file name is :20120508145115_create_e_readings.rb
and the version in db/schema.rb is :
:version => 20120508145115
Seems that rails forget that I already have run this migration and try to re-play it, so error occurred,but why is this happening and how can I solve this?
It seems like you may have run it before and it failed after creating the table for some reason. If you're sure it has already run, you can manually add a record to the "schema_migrations" table with the 20120508145115 as the version.
If this is just a dev environment and you don't mind blowing it away, you could also run rake db:reset and that would drop, create, load the schema and reseed it.
I think beerlington is right. Your migration probably failed the first time you ran it. In addition to his suggestions you could also try manually dropping the table from the database and re-running the migration to see what went wrong the first time
I agree with both Beerlington and Andy. If it's a development environment, try the following in the terminal:
rake db:drop
rake db:create
rake db:migrate
That will destroy your database, recreate it and run all your migrations.
The other thing you could try is to rollback using rake db:rollback until you see that this migration (or the one before it) has been rolled back, and then run rake db:migrate to run from that point till your latest point again. rake db:rollback rolls back one migration file at a time.
I'd go with the drop and recreate, though, just to make sure nothing funny has remained.
Hope this helps.

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