Skip to content

Instantly share code, notes, and snippets.

@jsuchal
Created December 2, 2009 09:07
Show Gist options
  • Save jsuchal/247070 to your computer and use it in GitHub Desktop.
Save jsuchal/247070 to your computer and use it in GitHub Desktop.
# old style
class AddUserToUploadedFiles < ActiveRecord::Migration
def self.up
add_column :uploaded_files, :user_id, :integer
end
def self.down
remove_column :uploaded_files, :user_id
end
end
# new even better style
class AMigration < ActiveRecord::Migration
def self.up
add_column :uploaded_files, :user_id, :integer
end
end
AMigration.migrate(:up) # add column
AMigration.migrate(:down) # remove column - this is generated automagically
# more complex
class ASlightlyComplexMigration < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :username, :null => false
t.string :name, :null => false
end
add_index :users, :username, :unique => true
end
end
ASlightlyComplexMigration.migrate(:up) # create table, add_index
ASlightlyComplexMigration.migrate(:down) # drop_index, drop_table - generated automatically
# drop table
class DropMigration < ActiveRecord::Migration
def self.down
create_table :users do |t|
t.string :username, :null => false
t.string :name, :null => false
end
end
end
DropMigration.migrate(:up) # drop_table - generated automatically
DropMigration.migrate(:down) # create_table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment