【发布时间】:2011-11-04 13:39:58
【问题描述】:
我正在开发一个 Rails 应用程序,我在开发环境中使用 Sqlite,在生产环境中使用 PostgreSQL。有没有办法编写“数据库感知”迁移?即executeSqlite 上的某个 SQL 语句和 Postgres 上的不同语句?
【问题讨论】:
标签: activerecord ruby-on-rails-3.1 rails-migrations
我正在开发一个 Rails 应用程序,我在开发环境中使用 Sqlite,在生产环境中使用 PostgreSQL。有没有办法编写“数据库感知”迁移?即executeSqlite 上的某个 SQL 语句和 Postgres 上的不同语句?
【问题讨论】:
标签: activerecord ruby-on-rails-3.1 rails-migrations
你应该能够写出类似的东西:
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
这不是我必须自己实现的,所以我不知道有任何陷阱。
【讨论】: