2012-08-10 54 views
4

我今天遇到了一種情況,我想在遷移中向表中添加部分postgres索引。當然這種東西是不可能的在軌道上使用add_index(它是coming some time如何在遷移中正確添加自定義SQL DML

因此,我強制在我的遷移中使用execute語句。

現在,schema.rb在頂部有此評論:

# Note that this schema.rb definition is the authoritative source for your 
# database schema. If you need to create the application database on another 
# system, you should be using db:schema:load, not running all the migrations 
# from scratch. The latter is a flawed and unsustainable approach (the more migrations 
# you'll amass, the slower it'll run and the greater likelihood for issues). 
# 

不幸的是這execute聲明schema.rb不追蹤這樣做的效果是使schema.rb幾乎無用的,如果我有任何自定義的DML。

有什麼辦法可以強制execute包含DML的語句找到schema.rb

+0

要添加到這個問題,有沒有辦法做到這一點使用rake的方式,每當rake創建一個數據庫,它運行一個腳本包含所有的自定義SQL DML? – 2013-01-22 10:36:51

回答

6

沒有辦法用schema.rb來實現你想要的,你想要做的就是切換到使用數據庫本地模式轉儲/檢索代碼。

config.active_record.schema_format = :sql 

現在,而不是一個schema.rb文件,你會得到一個structure.sql文件。這應該透明地處理所有軌道的內置任務,並且因爲它使用本地自卸車格式,所以它會支持你想要的任何瘋狂的東西。

相關問題