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

Ruby on Rails: PostgreSQL/Capistrano override migration when deploying

我更新了 Rails、Spree(电子商务框架)并添加了一些新的 gem。这些都需要在更新安装后迁移。在开发app上,我运行遇到了问题。每当我尝试迁移时,我都会收到错误

问题

ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: relation "index_products_promotion_rules_on_product_id" already exists : CREATE INDEX "index_products_promotion_rules_on_product_id" ON "spree_products_promotion_rules" ("product_id")

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

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

当我尝试将网站部署到实时服务器 error/problem returns 时, 但在实时服务器上,我实际上已经存储了客户数据和订单。所以我不能删除数据库。

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

问题:

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

是否可以从 migration fileschema.rb 中删除行?

或者有其他方法可以解决这个问题吗?

schema.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"

Migration File

# 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 直到我不再收到错误消息。