new2cpp
new2cpp

Reputation: 3667

Why directly changing a migrate file does not change the schema file?

My current migrate file is

class CreateMovies < ActiveRecord::Migration
  def up
    create_table :movies, :force => true do |t|
      t.string :title
      t.string :rating
      t.text :description
      t.datetime :release_date
      # Add fields that let Rails automatically keep track
      # of when movies are added or modified:
      t.timestamps
    end
  end

  def down
    drop_table :movies
  end
end

I try to change release_date type to integer. So I directly change the file to

class CreateMovies < ActiveRecord::Migration
  def up
    create_table :movies, :force => true do |t|
      t.string :title
      t.string :rating
      t.text :description
      t.integer :release_date
      # Add fields that let Rails automatically keep track
      # of when movies are added or modified:
      t.timestamps
    end
  end

  def down
    drop_table :movies
  end
end

Please pay attention, the release_date type has been changed. But after I run

bundle exec rake db:migrate

It still produce the same schema file as before. I am so confused.

Upvotes: 1

Views: 100

Answers (3)

Max Williams
Max Williams

Reputation: 32933

As an alternative to dropping and upping the migration, you could make a new migration to change the column type.

class ChangeMoviesReleaseTypeToInteger < ActiveRecord::Migration
  def up
    change_column :movies, :release_date, :integer
  end

  def down
    change_column :movies, :release_date, :datetime
  end
end

Just as a side note, release_date is a confusing name for an integer field - most people would expect it to be a datetime as you had originally.

Upvotes: 1

Nithin
Nithin

Reputation: 3699

down will remove the table

rake db:migrate:down VERSION=file_name(exclude extension)

up will create with new changes

rake db:migrate:up VERSION=file_name(exclude extension)

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51151

It's probably because you've already run your migration. So before you want to change it, you should rollback it first:

bundle exec rake db:rollback

then you should modify it and run again:

bundle exec rake db:migrate

Upvotes: 3

Related Questions