資料來源:Rails Guide
Guide
-使用 generator 自動生成遷移
-使用遷移創(chuàng)建,更改數(shù)據(jù)庫模式
-使用任務(wù)執(zhí)行遷移,完成數(shù)據(jù)庫模式更新
-在 schema.rb 文件中查看更新后的模式
1. Definition
Migration 以一種簡單的方式來不斷更新你的數(shù)據(jù)庫模式 (database schema)。它使用 ruby DSL 所以你不必去寫那些繁瑣的數(shù)據(jù)庫SQL語句來更新模式,并且它是獨(dú)立于某種特定的數(shù)據(jù)庫 (database independent),使用它可以作用于多個不同的數(shù)據(jù)庫,當(dāng)你想要換數(shù)據(jù)庫時會顯得特別方便。你可以把每個數(shù)據(jù)庫遷移想象為一個數(shù)據(jù)庫版本,schema 從什么都沒有開始一點(diǎn)點(diǎn)地隨著每個版本向前更新。你可以創(chuàng)建刪除表格,添加修改列名及其屬性,添加索引等等復(fù)雜的操作。Active Record 會更新你的db/schema.rb文件來匹配你最新的數(shù)據(jù)庫結(jié)構(gòu)。
–(1) 按照時間順序管理數(shù)據(jù)庫模式;
–(2) 對數(shù)據(jù)庫的操作和所用的數(shù)據(jù)庫種類無關(guān);
–(3) 可以把每次遷移看做是數(shù)據(jù)庫的一個修訂版本.
2. Create Migration
通過 generator 生成遷移時遵循名字add_xxx_to_yyy, remove_xxx_from_yyy,隨后跟著屬性和相匹配的類型 (默認(rèn)為字符串類型) (可以在類型后在指定是否要添加索引),會在遷移中自動生成add_column, remove_column語句。同理,使用create_xxx跟著屬性列表會自動生成create_table(屬性列表若包含類型為references, belongs_to會生成加索引的關(guān)聯(lián),并在數(shù)據(jù)庫層添加完整性驗證)。
Tips: 特殊詞: limit, precision, scale, polymorphic, null, default, index, comment
# migration generator examples
rails generate migration add_part_number_to_products part_number:string:index
rails generate migration add_details_to_products part_number:string price:decimal
rails generate migration create_products name:string part_number:string
rails generate migration add_user_ref_to_products user:references
rails generate migration create_join_table_customer_product customer product
rails generate migration add_details_to_products 'price:decimal{5,2}' supplier:references{polymorphic}
3. Running Migration
通過使用rake db:migrate / rake db:rollback來進(jìn)行更新或回滾操作。
運(yùn)行rake db:migrate任務(wù)時,會生成調(diào)用遷移中的change或up方法,隨后運(yùn)行rake db:schema:dump,更新與你數(shù)據(jù)庫結(jié)構(gòu)相匹配的db/schema.rb文件。
– 針對那些無法自動回滾的操作,我們可以使用 reversible 或者 up/down方法
– 若想修改已經(jīng)運(yùn)行的遷移,要先回滾到之前的狀態(tài),做修改,再運(yùn)行遷移
– 若想運(yùn)行指定版本的遷移(破壞順序) rake db:migrate:up VERSION=20080906120000
– 指定目標(biāo)版本會(跟新/回滾)到那個版本 rake db:migrate VERSION=20080906120000
– 若想回滾到最初版本則將目標(biāo)版本設(shè)為零即可 rake db:migrate VERSION=0
– 若想回滾指定次數(shù)的遷移則通過設(shè)置STEP來達(dá)到 rake db:migrate STEP=4
– 若想回滾之后重新運(yùn)行指定次數(shù)的遷移 rake db:migrate:redo STEP=3
– rake db:setup 用來創(chuàng)建數(shù)據(jù)庫,導(dǎo)入數(shù)據(jù)庫模式,初始化種子數(shù)據(jù)
– rake db:reset 用來刪除原先數(shù)據(jù)庫,再重新創(chuàng)建配置 drop then setup
– rake db:reset 注意是通過導(dǎo)入模式文件而不是重新運(yùn)行遷移
– rake db:schema:dump => db/schema.rb 匹配自動生成新的數(shù)據(jù)庫模式
– rake db:structure:dump => db/structure.sql 生成SQL語句的結(jié)構(gòu)模式
Migration for MySQL Limit
# ActiveRecord Migrate
create_table 'example' do |t|
t.integer :int # int (4 bytes, max 2,147,483,647)
t.integer :int, :limit => 1 # tinyint (1 byte, -128 to 127)
t.integer :int, :limit => 2 # smallint (2 bytes, max 32,767)
t.integer :int, :limit => 3 # mediumint (3 bytes, max 8,388,607)
t.integer :int, :limit => 4 # int (4 bytes)
t.integer :int, :limit => 5 # bigint (8 bytes, max 9,223,372,036,854,775,807)
t.integer :int, :limit => 8 # bigint (8 bytes)
t.integer :int, :limit => 11 # int (4 bytes)
end