Activerecord 支持数据库的Rails迁移

Activerecord 支持数据库的Rails迁移,activerecord,ruby-on-rails-3.1,rails-migrations,Activerecord,Ruby On Rails 3.1,Rails Migrations,我正在开发一个rails应用程序,在我的开发环境中使用Sqlite,在生产环境中使用PostgreSQL。有没有办法编写“数据库感知”迁移?i、 e.在Sqlite上执行某个SQL语句,在Postgres上执行另一个SQL语句的人?您应该能够编写如下内容: class MyMigration < ActiveRecord::Migration def up if ActiveRecord::Base.connection.kind_of? ActiveRecord::Conne

我正在开发一个rails应用程序,在我的开发环境中使用Sqlite,在生产环境中使用PostgreSQL。有没有办法编写“数据库感知”迁移?i、 e.在Sqlite上执行某个SQL语句,在Postgres上执行另一个SQL语句的人?

您应该能够编写如下内容:

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
classmymigration

这不是我必须自己实现的东西,所以我没有意识到任何陷阱。

谢谢!我想这正是我想要的!