2

I generated a new rails migration:

rails generate migration some_new_column_to_table

edit: --

ran the migration:

rake db:migrate

updated test db:

rake db:test:prepare

realized I hadn't added anything to the change method, then

updated the migration file:

class AddSomeColumnToTable < ActiveRecord::Migration
  def change
    add_column :table, :some_column, :string
    add_index :table, :some_column
  end
end

ran the migration (again):

rake db:migrate

updated test db (again):

rake db:test:prepare

After running these commands "some_column" had not be added to the database. I found a solution to this problem here: rake db:migrate is not working

rake db:drop:all
rake db:create:all
rake db:migrate

Why did this fix the problem? How can I prevent it in the future?

Community
  • 1
  • 1
nodocitna
  • 148
  • 1
  • 7

2 Answers2

1

It's not clear what your problem is. But your last commands do straighten things out.

rake db:drop:all  << drop the database
rake db:create:all  << create the database (not the tables)
rake db:migrate << build up the database based on the migrations

In my experience, migrations don't 'stop working' and they do exactly as layed out, as they are no more than individual commands. But, they require their execution to be done in order, so if you start editing the migrations, you have to be aware of whether a migration has run or not. Migration problems are typically the result of our editing them out-of-sequence. The safest best is to don't edit migrations that have run already. First do any of the following:

rake db:rollback  << rollback the last migration (you can do this multiple times)
rake db:migrate VERSION=00000   << This will rollback to a specific migration
rake db:rollback STEP=2 << rollback 2 migrations

Hopefully that's helpful.

Carson Cole
  • 4,183
  • 6
  • 25
  • 35
  • This explains exactly why my migration broke and how to fix it in the future (don't mess with migrations after they've already been run), but can you be more specific as to why recreating the database fixes the issue? – nodocitna Nov 08 '12 at 15:39
  • The only reason you'd need to do the 'drop' is because you migrations got out of sync--they were edited usually and don't match reality. I would usually try to fix the migrations, sometimes temporarily deleting sections, rolling back, then adding the sections back. This way I don't have to start from the very beginning, and not lose all my data. – Carson Cole Nov 08 '12 at 15:52
  • Is there a way to view a list of past migrations and their details? – nodocitna Nov 08 '12 at 15:58
  • All migrations: app/db/migrations – Carson Cole Nov 08 '12 at 16:07
  • If you want to see what migrations have been run, the most recent is listed in the first line of your db/schema.rb file. "ActiveRecord::Schema.define(:version => 20121106221959)". So the version is the last migration you've run, so make only careful changes to any PRIOR migration. Any later migration, you can change as much as you want. – Carson Cole Nov 08 '12 at 16:09
0

I can't say that the problem is this for sure, but it worth a shot.

You may have messed with the migrations.

Is not safe to edit migration unless it has not been migrated even once on any computer.

For Example, lets say u created a migration that add a field just like u did. Then you realize that field is not need anymore, and took it ou of the migration.

But if you have already run the migration, it will leave the filed on your database or on your case, it will not add that field.

So when working with migrations, try to not edit older migrations. Always create new ones.

Paulo Henrique
  • 1,025
  • 8
  • 12