migrations in rails 2.0

12

Upload: matt-polito

Post on 12-Nov-2014

3.522 views

Category:

Documents


0 download

DESCRIPTION

Chicago Ruby Meetup presentation on Rails 2.0 migrations.

TRANSCRIPT

Page 1: Migrations in Rails 2.0
Page 2: Migrations in Rails 2.0

create_table :users do |t|  t.column :first_name, :string, :null => false  t.column :last_name, :string, :null => false  t.column :account_id, :integer  t.column :description, :text  t.column :created_at, :datetime  t.column :updated_at, :datetimeend

create_table :users do |t|  t.column :first_name, :string, :null => false  t.column :last_name, :string, :null => false  t.column :account_id, :integer  t.column :description, :text  t.column :created_at, :datetime  t.column :updated_at, :datetimeend

Page 3: Migrations in Rails 2.0

create_table :users do |t|  t.integer :account_id  t.string :first_name, :last_name, :null => false  t.text :description  t.timestampsend

create_table :users do |t|  t.integer :account_id  t.string :first_name, :last_name, :null => false  t.text :description  t.timestampsend

Page 4: Migrations in Rails 2.0

script/generate migration AddMoreToProjects image_url:string complete:boolean script/generate migration AddMoreToProjects image_url:string complete:boolean

Page 5: Migrations in Rails 2.0

class AddMoreToProject < ActiveRecord::Migration

def self.up

add_column :projects, :image_url, :string add_column :projects, :complete, :boolean

end

def self.down

remove_column :projects, :complete

remove_column :projects, :image_url

end

end

class AddMoreToProject < ActiveRecord::Migration

def self.up

add_column :projects, :image_url, :string add_column :projects, :complete, :boolean

end

def self.down

remove_column :projects, :complete

remove_column :projects, :image_url

end

end

Page 6: Migrations in Rails 2.0

script/generate scaffold Project name:string url:string description:text

script/generate scaffold Project name:string url:string description:text

Page 7: Migrations in Rails 2.0

class CreateProjects < ActiveRecord::Migration

def self.up

create_table :projects do |t|

t.string :names, :url

t.text :description

t.timestamps

end

def self.down

drop_table :projects

end

end

class CreateProjects < ActiveRecord::Migration

def self.up

create_table :projects do |t|

t.string :names, :url

t.text :description

t.timestamps

end

def self.down

drop_table :projects

end

end

Page 8: Migrations in Rails 2.0

Where we used to have:

t.integer :commentary_id, :null => false

t.string :commentary_type, :null => false

Where we used to have:

t.integer :commentary_id, :null => false

t.string :commentary_type, :null => false

Page 9: Migrations in Rails 2.0

class VideoCast < ActiveRecord::Base

belongs_to :commentary, :polymorphic => true

end

class VideoCast < ActiveRecord::Base

belongs_to :commentary, :polymorphic => true

end

Page 10: Migrations in Rails 2.0

class CreateVideoCasts < ActiveRecord::Migrationdef self.up

create_table :video_casts do |t|t.references :commentary, :null => false,

:polymorphic => truet.timestamps

endend def self.down

drop_table :video_castsend

end

class CreateVideoCasts < ActiveRecord::Migrationdef self.up

create_table :video_casts do |t|t.references :commentary, :null => false,

:polymorphic => truet.timestamps

endend def self.down

drop_table :video_castsend

end

Page 11: Migrations in Rails 2.0

rake db:create creates database for development environment

rake db:create:all creates database for all environments

rake db:migrate:redo undo your last migration and re-run it

rake db:migrate:reset drops the database, re-creates it, and then runs all migrations

Rake db:rollback goes back one migration level

rake db:create creates database for development environment

rake db:create:all creates database for all environments

rake db:migrate:redo undo your last migration and re-run it

rake db:migrate:reset drops the database, re-creates it, and then runs all migrations

Rake db:rollback goes back one migration level

Page 12: Migrations in Rails 2.0

RailsJitsuhttp://www.railsjitsu.com/use-of-migration-references-in-rails-2-0-and-a-bug

Codefront.nethttp://blog.codefront.net/2007/12/24/generating-migrations-gets-easier-rails-20-a-feature-a-day-6/