2011-11-04 77 views
0

我正在使用Rails應用程序,並且在開發環境和PostgreSQL中使用Sqlite進行生產。有沒有辦法編寫「數據庫感知」遷移?即在Sqlite上使用execute某個SQL語句以及在Postgres上使用不同語句的情況?數據庫感知Rails遷移

回答

0

你應該能夠寫類似:

class MyMigration < ActiveRecord::Migration 
    def up 
    if ActiveRecord::Base.connection.kind_of? ActiveRecord::ConnectionAdapters::SQLite3Adapter 
     execute 'SQL Statement...' 
    else 
     execute 'Different SQL Statement...' 
    end 
    end 

    def down 
    ... 
    end 
end 

它不是我不得不實施自己,所以我不知道有任何缺陷。

+0

謝謝!我想這正是我正在尋找的! – Andrew