【问题标题】:Heroku - clean up the database, run the migrations with new itemsHeroku - 清理数据库,使用新项目运行迁移
【发布时间】:2012-04-25 08:36:51
【问题描述】:

我在 Heroku 上有一个应用程序,需要在那里清理数据库,再次使用新的默认行运行所有(编辑的)迁移(在迁移中将默认行添加到表中)。

我跑了

heroku run rake db:reset

此命令清除了数据库,但没有将新行添加到表中。我正在尝试以这种方式添加新行:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      ...columns definitions...

      t.timestamps
    end
  end

  def self.up
    Users.new(:name => 'my name', :password => 'super-secret-pass')
  end
end

但是没有添加新用户。我错过了什么?

【问题讨论】:

    标签: sql ruby-on-rails ruby-on-rails-3 heroku migration


    【解决方案1】:

    迁移应该有以下之一:

    1. change 方法。
    2. up 和 down 方法(最好是 3+ 中的实例方法,但类方法也可以)。

    你有一个change 方法和一个self.up 方法。迁移系统looks for change first:

    ActiveRecord::Base.connection_pool.with_connection do |conn|
      @connection = conn
      if respond_to?(:change)
        #...
      else
        time = Benchmark.measure { send(direction) }
      end
      @connection = nil
    end
    

    所以你的self.up 永远不会运行。

    两种解决方案立即出现:

    1. 使用单独的up 和down 方法。您可能需要查看指南的 Using Models in Your Migrations 部分。
    2. 使用两个单独的迁移:一个用于创建表,另一个用于创建您的用户。

    我可能会选择 2。

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2014-03-05
      • 2016-02-28
      • 1970-01-01
      • 2015-04-20
      相关资源
      最近更新 更多