資料來(lái)源:Rails Guide
Guide
-使用 generator
自動(dòng)生成遷移
-使用遷移創(chuàng)建,更改數(shù)據(jù)庫(kù)模式
-使用任務(wù)執(zhí)行遷移旨巷,完成數(shù)據(jù)庫(kù)模式更新
-在 schema.rb
文件中查看更新后的模式
1. Definition
Migration 以一種簡(jiǎn)單的方式來(lái)不斷更新你的數(shù)據(jù)庫(kù)模式 (database schema)。它使用 ruby DSL 所以你不必去寫那些繁瑣的數(shù)據(jù)庫(kù)SQL語(yǔ)句來(lái)更新模式脓诡,并且它是獨(dú)立于某種特定的數(shù)據(jù)庫(kù) (database independent)霍比,使用它可以作用于多個(gè)不同的數(shù)據(jù)庫(kù),當(dāng)你想要換數(shù)據(jù)庫(kù)時(shí)會(huì)顯得特別方便瘪松。你可以把每個(gè)數(shù)據(jù)庫(kù)遷移想象為一個(gè)數(shù)據(jù)庫(kù)版本咸作,schema 從什么都沒(méi)有開始一點(diǎn)點(diǎn)地隨著每個(gè)版本向前更新。你可以創(chuàng)建刪除表格宵睦,添加修改列名及其屬性记罚,添加索引等等復(fù)雜的操作。Active Record 會(huì)更新你的db/schema.rb
文件來(lái)匹配你最新的數(shù)據(jù)庫(kù)結(jié)構(gòu)壳嚎。
–(1) 按照時(shí)間順序管理數(shù)據(jù)庫(kù)模式;
–(2) 對(duì)數(shù)據(jù)庫(kù)的操作和所用的數(shù)據(jù)庫(kù)種類無(wú)關(guān);
–(3) 可以把每次遷移看做是數(shù)據(jù)庫(kù)的一個(gè)修訂版本.
2. Create Migration
通過(guò) generator 生成遷移時(shí)遵循名字add_xxx_to_yyy, remove_xxx_from_yyy
桐智,隨后跟著屬性和相匹配的類型 (默認(rèn)為字符串類型) (可以在類型后在指定是否要添加索引),會(huì)在遷移中自動(dòng)生成add_column, remove_column
語(yǔ)句烟馅。同理说庭,使用create_xxx
跟著屬性列表會(huì)自動(dòng)生成create_table
(屬性列表若包含類型為references, belongs_to
會(huì)生成加索引的關(guān)聯(lián),并在數(shù)據(jù)庫(kù)層添加完整性驗(yàn)證)郑趁。
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
通過(guò)使用rake db:migrate / rake db:rollback
來(lái)進(jìn)行更新或回滾操作刊驴。
運(yùn)行rake db:migrate
任務(wù)時(shí),會(huì)生成調(diào)用遷移中的change
或up
方法,隨后運(yùn)行rake db:schema:dump
捆憎,更新與你數(shù)據(jù)庫(kù)結(jié)構(gòu)相匹配的db/schema.rb
文件舅柜。
– 針對(duì)那些無(wú)法自動(dòng)回滾的操作,我們可以使用 reversible
或者 up/down
方法
– 若想修改已經(jīng)運(yùn)行的遷移躲惰,要先回滾到之前的狀態(tài)致份,做修改,再運(yùn)行遷移
– 若想運(yùn)行指定版本的遷移(破壞順序) rake db:migrate:up VERSION=20080906120000
– 指定目標(biāo)版本會(huì)(跟新/回滾)到那個(gè)版本 rake db:migrate VERSION=20080906120000
– 若想回滾到最初版本則將目標(biāo)版本設(shè)為零即可 rake db:migrate VERSION=0
– 若想回滾指定次數(shù)的遷移則通過(guò)設(shè)置STEP
來(lái)達(dá)到 rake db:migrate STEP=4
– 若想回滾之后重新運(yùn)行指定次數(shù)的遷移 rake db:migrate:redo STEP=3
– rake db:setup
用來(lái)創(chuàng)建數(shù)據(jù)庫(kù)础拨,導(dǎo)入數(shù)據(jù)庫(kù)模式知举,初始化種子數(shù)據(jù)
– rake db:reset
用來(lái)刪除原先數(shù)據(jù)庫(kù),再重新創(chuàng)建配置 drop then setup
– rake db:reset
注意是通過(guò)導(dǎo)入模式文件而不是重新運(yùn)行遷移
– rake db:schema:dump => db/schema.rb
匹配自動(dòng)生成新的數(shù)據(jù)庫(kù)模式
– rake db:structure:dump => db/structure.sql
生成SQL語(yǔ)句的結(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