RAILS: Editing old migration files to solve ActiveRecord::IrreversibleMigration - ruby-on-rails

I have a few old migration files that contain a change method. When executing rake db:migrate VERSION=0 they give an ActiveRecord::IrreversibleMigration, I can change the migration to have and up and down method, which solves the error on my local machine, but is this a good idea or will it cause problems? For example when deploying to production?
What is the right way of solving this issue?
Thanks.

Rule of thumb is: Changing old migrations is ok, when either:
It has never been run anywhere else (e.g. you are the only developer and it hasn't been deployed yet)
It has already been run in production
I personally like to have all migrations work from any point on my local machine so that when something goes wrong I can just execute:
rake db:drop && rake db:create && rake db:migrate
The only problem that I see is, when your changing of the migration would significantly change the schema. Then you'd end up with a different schema in production and development. This might cause problems with debugging and bug fixing.

Related

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.

How to fix a problematic earlier migration in Rails?

I wrote a Rails app locally and have maybe 10-15 migrations written. This all works fine locally.
When I wanted to deploy on heroku, I ran into a problem because they are using a earlier version of PostGreSQL than what I was using locally. One of my earliest migrations is failing because of a missing DB function in one of my database views.
I found out a way to hack around the DB function issue, but now I'm stuck because I can't write a new migration that changes the view to use the hack, since the rake db:migrate will abort after it hits the original problematic view creation.
What can I do to solve this?
First of all drop your db:
heroku pg:reset
Then run your new migrations:
heroku rake db:migrate

How to update production database schema safely in rails 3.1.3?

We need constantly update our database schema in production for rails 3.1.3 app. The first db schema was created with the following rails command:
$rake RAILS_ENV=production db:schema:load
Question is: can we still use the command above to update db schema in production while safely retaining all current data?
Thanks so much.
I never used rake db:schema:load in production, but according to this answer to another question here on SO, I don't think you want to do that.
On the other hand, I have used RAILS_ENV=production rake db:migrate several times on the server with data already in the database and never experienced any problems.

Rails - how to solve this issue of an orphaned migration?

After rake db:migrate:rollback STEP=1, rake db:migrate:reset, rake db:migrate:setup, rake db:migrate:up VERSION=XXXXXXXXX I get the following entry:
Status Migration ID Migration Name
------------------------------------------------------
up 0 *********NO FILE**********
up 20120209023430 Create tasks
How can I get rid of the orphaned entry? I have encountered this problem a few times after raking the db similar to above. Could somebody please explain what exactly causes this.
Thx in advance.
Shahram
You could use rake db:migrate:reset db:seed. It's a little less verbose and will completely wipe your database, re-run all migrations, and then seed your database from the db/seeds.rb file.
I've just released this gem that can solve this issue for good.
The idea of that gem is simple. It keeps all migrated migrations inside tmp folder so that Git ignores them. It's just only your local story. These files are needed to roll back the "unknown" migrations being in another branch. Now, whenever you have an inconsistent DB schema due to running migrations in some other branch just run rails db:migrate inside the current branch and it will fix the issue automatically. The gem does all this magic automatically for you.

heroku rake db:migrate didn't work

I've run a migration adding active column to users using heroku rake db:migrate.
and succesfully. but when I run the application the "active" column/attributes is not defined.
I already try to migrate:down and run migrate again for that version.. but it's still not worked.
did you add :active to the list of accessible attributes?
attr_accessible :active
failing that, sometimes you need to run heroku rake db:restart
and just finally, if it's still not working, run heroku logs and you will probably find something
Try this, it will run both the up and the down which will help you better analyze the situation. Also, there may be a problem in your migrations, if you post the code I should be able to asset further.
rake db:migrate:redo
Try to restart you application or drop database, if it doesnt contain valuable information.

Resources