Ruby on Rails:PostgreSQL/Capistrano 在部署时覆盖迁移



我已经更新了Rails,Spree(电子商务框架(并添加了一些新的宝石。这些都需要在更新和安装后迁移。在开发应用程序上,我遇到了一个问题。每当我尝试迁移时,我都会收到错误

问题

活动记录::语句无效:PG::D uplicate表:错误:关系"index_products_promotion_rules_on_product_id"已存在 :在"spree_products_promotion_rules"("product_id"(上创建索引"index_products_promotion_rules_on_product_id">

我通过删除我的数据库 (DB( 并创建一个新数据库来解决这个问题。由于它是开发模式,因此丢失保存在数据库上的一些数据不是问题。

现在我使用 postgreSQL 作为我的数据库和 Capistrano 来部署我的应用程序。

当我尝试将我的网站部署到实时服务器时,错误/问题返回, 但是在实时服务器上,我实际上已经存储了客户数据和订单。所以我不能删除数据库。

我试图从migration fileschema.rb中删除该行,但它没有帮助。

问题:

现在我想知道我可以覆盖实时服务器上的当前表吗?

是否保存从migration fileschema.rb中删除行?

还是有其他方法可以解决这个问题?

模式.rb

create_table "spree_product_promotion_rules", force: :cascade do |t|
t.integer "product_id"
t.integer "promotion_rule_id"
t.index ["product_id"], name: "index_products_promotion_rules_on_product_id"
t.index ["promotion_rule_id", "product_id"], name: "index_products_promotion_rules_on_promotion_rule_and_product"

迁移文件

# This migration comes from spree (originally 20120831092359)
class SpreePromoOneTwo < ActiveRecord::Migration[4.2]
def up
# This migration is just a compressed migration for all previous versions of spree_promo
return if table_exists?(:spree_products_promotion_rules)
create_table :spree_products_promotion_rules, :id => false, :force => true do |t|
t.references :product
t.references :promotion_rule
end
add_index :spree_products_promotion_rules, [:product_id], :name => 'index_products_promotion_rules_on_product_id'
add_index :spree_products_promotion_rules, [:promotion_rule_id], :name => 'index_products_promotion_rules_on_promotion_rule_id'
create_table :spree_promotion_actions, force: true do |t|
t.references :activator
t.integer    :position
t.string     :type
end
create_table :spree_promotion_rules, force: true do |t|
t.references :activator
t.references :user
t.references :product_group
t.string     :type
t.timestamps null: false
end
add_index :spree_promotion_rules, [:product_group_id], name: 'index_promotion_rules_on_product_group_id'
add_index :spree_promotion_rules, [:user_id], name: 'index_promotion_rules_on_user_id'
create_table :spree_promotion_rules_users, id: false, force: true do |t|
t.references :user
t.references :promotion_rule
end
add_index :spree_promotion_rules_users, [:promotion_rule_id], name: 'index_promotion_rules_users_on_promotion_rule_id'
add_index :spree_promotion_rules_users, [:user_id], name: 'index_promotion_rules_users_on_user_id'
end
end

最终我下载了数据库,因此我可以尝试在本地迁移。我更改了文件,删除了已经存在的行并执行了rails db:migrate ,直到我不再收到错误消息。

最新更新