up create_table :products do |t| t.string :name t.text :description t.timestamps end end def down drop_table :products end end class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.text :description t.timestamps end end end rails 3.1 ~> subclass of ActiveRecord::Migration 13֙ 3ਘ 30ੌ షਃੌ
method allows you to execute arbitrary SQL • Migrations are wrapped in a transaction(PostgreSQL or SQLite3, not support MySQL) add_column add_index change_column change_table create_table drop_table remove_column remove_index rename_column execute "SQL" 13֙ 3ਘ 30ੌ షਃੌ
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 updated_at, created_at 13֙ 3ਘ 30ੌ షਃੌ
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 13֙ 3ਘ 30ੌ షਃੌ
from schema_migrations; +----------------+ | version | +----------------+ | 20120531061820 | | 20120531105534 | | 20120531124444 | | 20120531125446 | | 20120531133035 | | 20120601102629 | | 20120603223525 | | 20120603224330 | | 20120603224625 | | 20120604064155 | | 20120604110743 | | 20120702123904 | | 20120702125358 | | 20120703005951 | | 20120704033651 | | 20120728014210 | | 20120728061841 | | 20120728102213 | | 20120729053924 | | 20120804011723 | | 20120804012821 | | 20120804013538 | | 20120808023400 | | 20120810071351 | +----------------+ 34 rows in set (0.00 sec) database: medibook_development Status Migration ID Migration Name -------------------------------------------------- up 20120531061820 Create users up 20120531105534 Rolify create roles up 20120531124444 Create boards up 20120531125446 Create board types up 20120531133035 Create posts up 20120601102629 Create categories up 20120603223525 Create products up 20120603224330 Add some fields to users up 20120603224625 Add some fields to categories up 20120604064155 Add category id to products up 20120604110743 Add attachment product photo to products up 20120702123904 Add view count to products up 20120702125358 Create comments up 20120703005951 Create favourites up 20120704033651 Add account to users up 20120728014210 Add devise to users up 20120728061841 Remove password digest from users up 20120728102213 Create user groups up 20120729053924 Create users groups up 20120804011723 Add user details to users 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 13֙ 3ਘ 30ੌ షਃੌ
database: db/development.sqlite3 Status Migration ID Migration Name -------------------------------------------------- up 20120818022501 Create posts down 20120818023340 Create comments down 20120818031154 Create tags $ rake db:abort_if_pending_migrations You have 2 pending migrations: 20120818023340 CreateComments 20120818031154 CreateTags Run `rake db:migrate` to update your database then try again. 13֙ 3ਘ 30ੌ షਃੌ
rake db:migrate:down VERSION=20120702125358 database: medibook_development Status Migration ID -------------------------- up 20120531061820 up 20120531105534 up 20120531124444 up 20120531125446 up 20120531133035 up 20120601102629 up 20120603223525 up 20120603224330 up 20120603224625 up 20120604064155 up 20120604110743 up 20120702123904 up 20120702125358 up 20120703005951 up 20120704033651 up 20120728014210 up 20120728061841 up 20120728102213 up 20120729053924 up 20120804011723 up 20120804012821 up 20120804013538 up 20120808023400 up 20120810071351 Running Migrations UP db:migrate:down 13֙ 3ਘ 30ੌ షਃੌ
rake db:migrate:up VERSION=20120702125358 database: medibook_development Status Migration ID -------------------------- up 20120531061820 up 20120531105534 up 20120531124444 up 20120531125446 up 20120531133035 up 20120601102629 up 20120603223525 down 20120603224330 down 20120603224625 down 20120604064155 down 20120604110743 down 20120702123904 down 20120702125358 down 20120703005951 down 20120704033651 down 20120728014210 down 20120728061841 down 20120728102213 down 20120729053924 down 20120804011723 down 20120804012821 down 20120804013538 down 20120808023400 down 20120810071351 Running Migrations UP db:migrate:up 13֙ 3ਘ 30ੌ షਃੌ
up 20120531105534 up 20120531124444 up 20120531125446 up 20120531133035 up 20120601102629 up 20120603223525 up 20120603224330 up 20120603224625 up 20120604064155 up 20120604110743 up 20120702123904 up 20120702125358 up 20120703005951 up 20120704033651 up 20120728014210 up 20120728061841 up 20120728102213 up 20120729053924 up 20120804011723 up 20120804012821 up 20120804013538 up 20120808023400 up 20120810071351 $ rake db:rollback $ rake db:migrate:redo $ rake db:rollback STEP=3 $ rake db:migrate:redo STEP=3 Running Migrations UP 13֙ 3ਘ 30ੌ షਃੌ
create_table :products do |t| t.string :name end end say "Created a table" suppress_messages {add_index :products, :name} say "and an index!", true say_with_time 'Waiting for a while' do sleep 10 250 end end end == CreateProducts: migrating ============ -- Created a table -> and an index! -- Waiting for a while -> 10.0013s -> 250 rows == CreateProducts: migrated (10.0054s) ==== 13֙ 3ਘ 30ੌ షਃੌ
< 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 Model in Migration • YourTable.reset_column_information • Alice and Bob’s Story 13֙ 3ਘ 30ੌ షਃੌ