【问题标题】:Database-aware Rails Migration数据库感知 Rails 迁移
【发布时间】:2011-11-04 13:39:58
【问题描述】:

我正在开发一个 Rails 应用程序,我在开发环境中使用 Sqlite,在生产环境中使用 PostgreSQL。有没有办法编写“数据库感知”迁移?即executeSqlite 上的某个 SQL 语句和 Postgres 上的不同语句?

【问题讨论】:

    标签: activerecord ruby-on-rails-3.1 rails-migrations


    【解决方案1】:

    你应该能够写出类似的东西:

    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
    

    这不是我必须自己实现的,所以我不知道有任何陷阱。

    【讨论】:

    • 谢谢!我想这正是我想要的!
    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 2017-02-18
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2012-08-24
    相关资源
    最近更新 更多