Rails Database Migration, Season 2
Upcoming SlideShare
Loading in...5
×
 

Rails Database Migration, Season 2

on

  • 2,188 views

This is a Keynote slide about the "Rails Database Migrations"

This is a Keynote slide about the "Rails Database Migrations"

Statistics

Views

Total Views
2,188
Views on SlideShare
1,049
Embed Views
1,139

Actions

Likes
1
Downloads
11
Comments
0

14 Embeds 1,139

http://rorlab.org 865
http://lab.wordstory.net 167
http://www.rorlab.org 44
http://localhost 39
http://kt.rorlab.org 9
http://ror.la 3
http://livecoding.rorlab.org 3
http://www.hanrss.com 2
https://www.facebook.com 2
http://www.facebook.com 1
http://t.co 1
http://lvh.me 1
http://webcache.googleusercontent.com 1
http://211.60.184.138 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Rails Database Migration, Season 2 Rails Database Migration, Season 2 Presentation Transcript

  • ROR Lab. Season 2 - The 5th Round -DatabaseMigrations September 1st, 2012 Hyoseong Choi ROR Lab.
  • Migration?• To change Database• in Ruby code ROR Lab.
  • Why?• In the legacy web development, what if you modify a sql statement directly? • You should announce your colleagues to implement your sql modification. • You should remember it in the following deployment. ROR Lab.
  • Advantages Migration Sqlite3 Active Record MySQL Postgres Oracle schema_migrations $ rake db:migrate db/schema.rb ROR Lab.
  • To Create Migration Files• $ rails generate model Post title:string• $ rails generate scaffold Post title:string• $ rails generate migration AddContentToPosts content:text ROR Lab.
  • g Model | Scaffold rails 3.1 ~>class CreateProducts < ActiveRecord::Migration class CreateProducts < ActiveRecord::Migration  def up   def change    create_table :products do |t|     create_table :products do |t|      t.string :name       t.string :name      t.text :description       t.text :description         t.timestamps       t.timestamps    end     end  end   end  end  def down    drop_table :products  end add columns or tables ROR Lab.
  • g Migrationclass AddReceiveNewsletterToUsers < ActiveRecord::Migration  def up    change_table :users do |t|      t.boolean :receive_newsletter, :default => false    end    User.update_all ["receive_newsletter = ?", true]  end   def down    remove_column :users, :receive_newsletter  endend ROR Lab.
  • To Destroy Migration Files• $ rails destroy model Post title:string• $ rails destroy scaffold Post title:string• $ rails destroy migration AddContentToPosts content:text ROR Lab.
  • Rake it~• $ rake db:migrate “up” state• $ rake db:rollback “down” state• $ rake db:migrate:status ROR Lab.
  • Rake it~ mysql> select * from schema_migrations;$ rake db:migrate:status +----------------+ | version |database: medibook_development +----------------+ | 20120531061820 | Status Migration ID Migration Name | 20120531105534 |-------------------------------------------------- | 20120531124444 | | 20120531125446 | up 20120531061820 Create users | 20120531133035 | up 20120531105534 Rolify create roles | 20120601102629 | up 20120531124444 Create boards | 20120603223525 | up 20120531125446 Create board types | 20120603224330 | up 20120531133035 Create posts | 20120603224625 | up 20120601102629 Create categories | 20120604064155 | up 20120603223525 Create products | 20120604110743 | | 20120702123904 | up 20120603224330 Add some fields to users | 20120702125358 | up 20120603224625 Add some fields to categories | 20120703005951 | up 20120604064155 Add category id to products | 20120704033651 | up 20120604110743 Add attachment product photo to products | 20120728014210 | up 20120702123904 Add view count to products | 20120728061841 | up 20120702125358 Create comments | 20120728102213 | up 20120703005951 Create favourites | 20120729053924 | up 20120704033651 Add account to users | 20120804011723 | | 20120804012821 | up 20120728014210 Add devise to users | 20120804013538 | up 20120728061841 Remove password digest from users | 20120808023400 | up 20120728102213 Create user groups | 20120810071351 | up 20120729053924 Create users groups +----------------+ up 20120804011723 Add user details to users 34 rows in set (0.00 sec) up 20120804012821 Create affiliations up 20120804013538 Add some fields to affiliations up 20120808023400 Add attachment company logo to affiliations up 20120810071351 Add active to users ROR Lab.
  • A Migration File CreateProducts Class✴20080906120000_create_products.rb AddDetailToProducts Class✴20080906120001_add_details_to_prod ucts.rb ROR Lab.
  • schema_migrations• db/schema.rb• A database table : history of migrations $ rake db:migrate:status • up • down ROR Lab.
  • Up & Down Methods (1) class CreateProducts < ActiveRecord::Migration   def up     create_table :products do |t|       t.string :name       t.text :description         t.timestamps     end   end     def down     drop_table :products   end end ROR Lab.
  • Up & Down Methods (2) class AddReceiveNewsletterToUsers < ActiveRecord::Migration   def up     change_table :users do |t|       t.boolean :receive_newsletter, :default => false     end     User.update_all ["receive_newsletter = ?", true]   end     def down     remove_column :users, :receive_newsletter   end end ROR Lab.
  • Up & Down Methods (3) class ExampleMigration < ActiveRecord::Migration   def up     create_table :products do |t|       t.references :category     end     #add a foreign key     execute <<-SQL       ALTER TABLE products         ADD CONSTRAINT fk_products_categories         FOREIGN KEY (category_id)         REFERENCES categories(id)     SQL     add_column :users, :home_page_url, :string     rename_column :users, :email, :email_address   end     def down     rename_column :users, :email_address, :email     remove_column :users, :home_page_url     execute <<-SQL       ALTER TABLE products         DROP FOREIGN KEY fk_products_categories     SQL     drop_table :products   end end ROR Lab.
  • Up & Down Methods (3) class ExampleMigration < ActiveRecord::Migration   def up     create_table :products do |t|       t.references :category     end     #add a foreign key     execute <<-SQL       ALTER TABLE products         ADD CONSTRAINT fk_products_categories         FOREIGN KEY (category_id)         REFERENCES categories(id)     SQL     add_column :users, :home_page_url, :string     rename_column :users, :email, :email_address   end     def down     rename_column :users, :email_address, :email     remove_column :users, :home_page_url     execute <<-SQL       ALTER TABLE products         DROP FOREIGN KEY fk_products_categories     SQL     drop_table :products   end end ROR Lab.
  • Change Methodclass CreateProducts < ActiveRecord::Migration  def change    create_table :products do |t|      t.string :name • create_table      t.text :description • add_column  • add_index      t.timestamps • rename_table    end • rename_column  end • rename_indexend • add_timestamps • remove_timestamps ROR Lab.
  • Running Migrations db/schema.rb db:schema:dump UP $ rake db:migrate $ rake db:migrate VERSION=20090906120000 $ rake db:migrate:up VERSION=20090906120000DOWN $ rake db:rollback $ rake db:rollback STEP=3 $ rake db:migrate:redo STEP=3 ROR Lab.
  • Running Migrations database: medibook_development Status Migration ID -------------------------- up 20120531061820UP up up 20120531105534 20120531124444 up 20120531125446 $ rake db:migrate up 20120531133035 up 20120601102629 $ rake db:migrate VERSION=20120702125358 up 20120603223525 up 20120603224330 $ rake db:migrate:down VERSION=20120702125358 up 20120603224625 up 20120604064155 up 20120604110743 up 20120702123904 up 20120702125358 up 20120703005951 up 20120704033651 up 20120728014210 up 20120728061841 up 20120728102213 up 20120729053924 up 20120804011723 db:migrate:down up up 20120804012821 20120804013538 up 20120808023400 up 20120810071351 ROR Lab.
  • Running Migrations database: medibook_development Status Migration ID -------------------------- up 20120531061820UP up up 20120531105534 20120531124444 up 20120531125446 $ rake db:migrate up 20120531133035 up 20120601102629 $ rake db:migrate VERSION=20120702125358 up 20120603223525 down 20120603224330 $ rake db:migrate:up VERSION=20120702125358 down 20120603224625 down 20120604064155 down 20120604110743 down 20120702123904 down 20120702125358 down 20120703005951 down 20120704033651 down 20120728014210 down 20120728061841 down 20120728102213 down 20120729053924 down 20120804011723 db:migrate:up down 20120804012821 down 20120804013538 down 20120808023400 down 20120810071351 ROR Lab.
  • Running Migrations database: medibook_development Status Migration ID -------------------------- up 20120531061820UP $ rake db:rollback up up 20120531105534 20120531124444 up 20120531125446 $ rake db:migrate:redo up 20120531133035 up 20120601102629 $ rake db:rollback STEP=3 up 20120603223525 up 20120603224330 $ rake db:migrate:redo STEP=3 up 20120603224625 up 20120604064155 up 20120604110743 up 20120702123904 up 20120702125358 up 20120703005951 up 20120704033651 up 20120728014210 up 20120728061841 up 20120728102213 up 20120729053924 up 20120804011723 db:migrate:down up up 20120804012821 20120804013538 up 20120808023400 up 20120810071351 ROR Lab.
  • App DB Init using db/schema.rb$ rake db:setup $ rake db:reset1. db:create 1. db:drop2. db:schema:load 2. db:setup3. db:seed https://github.com/rails/rails/commit/983815632cc1d316c7c803a47be28f1abe6698fb ROR Lab.
  • Add & Remove Columns• AddXXXToYYY• RemoveXXXFromYYY$ rails g migration AddNameToUsers$ rails g migration RemoveNameFromUsers ROR Lab.
  • Model in Migration• YourTable.reset_column_information # db/migrate/20100513121110_add_flag_to_product.rb   class AddFlagToProduct < ActiveRecord::Migration   class Product < ActiveRecord::Base   end     def change     add_column :products, :flag, :integer     Product.reset_column_information     Product.all.each do |product|       product.update_attributes!(:flag => false)     end   end end• Alice and Bob’s Story ROR Lab.
  • Schema works• $ rake db:schema:dump called by db:migrate create schema.rb• $ rake db:schema:load load schema.rb ROR Lab.
  • 감사합니다.
  •