运行 rails 生产迁移
running rails migrations on production
我已经在 VPS 上使用 passenger 和 nginx 部署了一个 rails 应用程序。现在我添加了如下迁移:
def change
add_column :order_products, :order_id, :integer
add_index :order_products, :order_id
Order.find_each do |order|
OrderProduct.where(order_number: order.order_number).find_each do |op|
op.update_attributes!(order_id: order.id)
end
end
end
我在 VPS 上执行了 rake db:migrate
命令,但这并没有向 OrderProduct table 添加任何列。
db/schema.rb
包含以下内容:
create_table "order_products", force: :cascade do |t|
t.string "order_number"
t.string "item_code"
t.integer "quantity"
t.string "status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "order_id"
end
add_index "order_products", ["order_id"], name: "index_order_products_on_order_id", using: :btree
其中包含 order_id
列。此外,在 schema_migrations
table 中,我没有看到添加了最新的迁移版本。
我是否需要做任何其他事情才能让它在生产环境中运行?
感谢您的帮助。
您需要告诉 rake 您想要 运行 production
环境中的任务,因为默认情况下它会 运行 针对 development
数据库的迁移:
在 Ruby 的旧版本 Rails (< 5
)
$ RAILS_ENV=production rake db:migrate
在 Rails (>= 5
)
上 Ruby 的较新版本
$ RAILS_ENV=production rails db:migrate
正如@spickermann 所说,尝试使用 rake db:migrate RAILS_ENV=production
如果那不起作用尝试
rails generate migration AlterOrderProduct
def up
add_column(:order_products, :order_id, :integer)
add_index (:order_products, :order_id)
Order.find_each do |order|
OrderProduct.where(order_number: order.order_number).find_each do |op|
op.update_attributes!(order_id: order.id)
end
end
end
def down
remove_column(:order_products, :order_id, :integer)
remove_index (:order_products, :order_id)
end
我已经在 VPS 上使用 passenger 和 nginx 部署了一个 rails 应用程序。现在我添加了如下迁移:
def change
add_column :order_products, :order_id, :integer
add_index :order_products, :order_id
Order.find_each do |order|
OrderProduct.where(order_number: order.order_number).find_each do |op|
op.update_attributes!(order_id: order.id)
end
end
end
我在 VPS 上执行了 rake db:migrate
命令,但这并没有向 OrderProduct table 添加任何列。
db/schema.rb
包含以下内容:
create_table "order_products", force: :cascade do |t|
t.string "order_number"
t.string "item_code"
t.integer "quantity"
t.string "status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "order_id"
end
add_index "order_products", ["order_id"], name: "index_order_products_on_order_id", using: :btree
其中包含 order_id
列。此外,在 schema_migrations
table 中,我没有看到添加了最新的迁移版本。
我是否需要做任何其他事情才能让它在生产环境中运行?
感谢您的帮助。
您需要告诉 rake 您想要 运行 production
环境中的任务,因为默认情况下它会 运行 针对 development
数据库的迁移:
在 Ruby 的旧版本 Rails (< 5
)
$ RAILS_ENV=production rake db:migrate
在 Rails (>= 5
)
$ RAILS_ENV=production rails db:migrate
正如@spickermann 所说,尝试使用 rake db:migrate RAILS_ENV=production
如果那不起作用尝试
rails generate migration AlterOrderProduct
def up
add_column(:order_products, :order_id, :integer)
add_index (:order_products, :order_id)
Order.find_each do |order|
OrderProduct.where(order_number: order.order_number).find_each do |op|
op.update_attributes!(order_id: order.id)
end
end
end
def down
remove_column(:order_products, :order_id, :integer)
remove_index (:order_products, :order_id)
end