3. 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.
4. Advantages
Migration Sqlite3
Active Record MySQL
Postgres
Oracle
schema_migrations
$ rake db:migrate
db/schema.rb
ROR Lab.
5. To Create
Migration Files
• $ rails generate model Post title:string
• $ rails generate scaffold Post title:string
• $ rails generate migration
AddContentToPosts content:text
ROR Lab.
6. 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.
7. g Migration
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.
8. To Destroy
Migration Files
• $ rails destroy model Post title:string
• $ rails destroy scaffold Post title:string
• $ rails destroy migration
AddContentToPosts content:text
ROR Lab.
10. 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.
11. A Migration File
CreateProducts Class
✴20080906120000_create_products.rb
AddDetailToProducts Class
✴20080906120001_add_details_to_prod
ucts.rb
ROR Lab.
13. 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.
14. 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.
15. 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.
16. 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.
17. Change Method
class 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_index
end
• add_timestamps
• remove_timestamps
ROR Lab.
19. Running Migrations
database: medibook_development
Status Migration ID
--------------------------
up 20120531061820
UP 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.
20. Running Migrations
database: medibook_development
Status Migration ID
--------------------------
up 20120531061820
UP 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.
21. Running Migrations
database: medibook_development
Status Migration ID
--------------------------
up 20120531061820
UP $ 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.
24. 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.
25. Schema works
• $ rake db:schema:dump
called by db:migrate
create schema.rb
• $ rake db:schema:load
load schema.rb
ROR Lab.