migrations in rails 2.0
DESCRIPTION
Chicago Ruby Meetup presentation on Rails 2.0 migrations.TRANSCRIPT
![Page 1: Migrations in Rails 2.0](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/1.jpg)
![Page 2: Migrations in Rails 2.0](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081413/5464db7eaf795992368b4bb8/html5/thumbnails/12.jpg)
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/