Topic: Migration to add a column

So I've got a migration to create an events table.

class CreateEvents < ActiveRecord::Migration
      def self.up
        create_table :events do |t|
          t.column :name, :string, :null => false
          t.column :start, :datetime, :null => false
          t.column :city, :string, :null => false
          t.column :state, :string, :null => false
          t.column :description, :text, :null => false
          t.column :event_type, :string, :null => false
          t.column :contact, :string, :null => false
          t.column :email, :string, :null => true
          t.column :phone, :string, :null => false
          t.column :url, :string, :null => true
          t.column :created_at, :datetime
          t.column :created_by, :string, :null => false
        end
      end
   
      def self.down
        drop_table :events
      end
    end

I've got a few dozen records in there. Need to add a column without losing data. I wrote a new migration to do this (I think it's right).
class AddApprovalStatus < ActiveRecord::Migration
  def self.up
    add_column :events, :approved, :integer, :default => "0", :null => false
  end
   
  def self.down
    remove_column :events, :approved
  end
end

My question relates to executing this migration. I assume I don't want to do just "rake migrate" because that will run all of them and drop my events table and the data therein.

So what is the syntax to run just this migration?

Re: Migration to add a column

No, when you rung 'rake migrate' rake will not drop the existing tables. Instead it will look at which schema version your database is and will only run that migrations necessary to get it up to date.

EDIT:
But if you want to be sure, make a backup of your DB fist before you run rake migrate.

Last edited by Dieter Komendera (2006-11-07 16:22:23)

My homepage: http://www.komendera.com/
Working at: http://www.abloom.at/
My blog: soaked and soaped http://soakedandsoaped.com/

Re: Migration to add a column

You know that's what I was thinking, but I wasn't sure. Thanks man.