【问题标题】:Fixing rails migration ran on server instead of git repo using Capistrano使用 Capistrano 修复 Rails 迁移在服务器而不是 git repo 上运行
【发布时间】:2017-05-15 03:44:23
【问题描述】:

我将 Spree Commerce 从 3.0 版升级到 3.1 版,但忘记在本地开发环境中签入 Git 上的迁移文件

我改为在服务器上生成迁移文件;我最终将迁移从我的开发环境提交到 git,但现在我在部署时遇到了各种问题,因为它试图在表存在时运行迁移。

我想我真的不需要运行迁移,因为它在服务器上?

在服务器上运行rake db:migrate:status显示:

   up     20151015124064  Add meta title to page.spree static content
   up     20151015124065  Add render as partial for layout for spree pages.spree static content
   up     20151015124066  Add pages stores.spree static content
  down    20160707102753  Create spree store credits.spree
  down    20160707102754  Create spree store credit categories.spree
  down    20160707102755  Create spree store credit events.spree
  down    20160707102756  Create spree store credit types.spree
  down    20160707102757  Add missing indexes.spree
  down    20160707102758  Remove duplicated indexes from multi columns.spree
  down    20160707102759  Remove user index from spree state changes.spree
  down    20160707102760  Add position to spree payment methods.spree
  down    20160707102761  Add taxable adjustment total to line item.spree
  down    20160707102762  Migrate payment methods display.spree
  down    20160707102763  Spree payment method store credits.spree
  down    20160707102764  Rename has and belongs to associations to model names.spree
  down    20160707102765  Spree store credit types.spree
  down    20160707102766  Add discontinued to products and variants.spree
  down    20160707102767  Remove shipping method id from spree orders.spree
  down    20160707102768  Add id column to earlier habtm tables.spree
  down    20160707102769  Add indexes.spree
  down    20160707102770  Add missing indices on user.spree auth
  down    20160707102771  Remove show in footer from spree pages.spree static content

在我的定位机器上显示:

   up     20151015124064  Add meta title to page.spree static content
   up     20151015124065  Add render as partial for layout for spree pages.spree static content
   up     20151015124066  Add pages stores.spree static content
   up     20160707102753  Create spree store credits.spree
   up     20160707102754  Create spree store credit categories.spree
   up     20160707102755  Create spree store credit events.spree
   up     20160707102756  Create spree store credit types.spree
   up     20160707102757  Add missing indexes.spree
   up     20160707102758  Remove duplicated indexes from multi columns.spree
   up     20160707102759  Remove user index from spree state changes.spree
   up     20160707102760  Add position to spree payment methods.spree
   up     20160707102761  Add taxable adjustment total to line item.spree
   up     20160707102762  Migrate payment methods display.spree
   up     20160707102763  Spree payment method store credits.spree
   up     20160707102764  Rename has and belongs to associations to model names.spree
   up     20160707102765  Spree store credit types.spree
   up     20160707102766  Add discontinued to products and variants.spree
   up     20160707102767  Remove shipping method id from spree orders.spree
   up     20160707102768  Add id column to earlier habtm tables.spree
   up     20160707102769  Add indexes.spree
   up     20160707102770  Add missing indices on user.spree auth
   up     20160707102771  Remove show in footer from spree pages.spree static content

我认为服务器上的迁移状态应该是up 而不是down

有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 capistrano spree rails-migrations


    【解决方案1】:

    如果您没有任何数据丢失,您可以从 sql 控制台删除这些表并重新运行 capistrano 部署,或者您可以手动 up 从服务器迁移文件

    rake db:migrate:up VERSION=20151015124064
    

    其中版本是rake db:migrate:status结果的第二列

    注意:请记住,您必须在两种情况下都删除这些表

    【讨论】:

    • 谢谢 Vishal,我确实有数据,不幸的是,我们尝试迁移:向上失败,因为表已经存在。删除表很好,但存在不可逆的迁移。
    • 不可逆迁移是 spree 表或其他表的一部分,如果可能,您能否列出不可逆迁移文件。
    • 由于迁移不成功,您可以添加 2 个方法 def updef down 而不是 def change迁移是不可逆的,因此您可以为迁移和回滚编写不同的逻辑。我们可以通过转储 SQL 来保留数据。
    【解决方案2】:

    如果您不想丢失数据并重新开始,您可以尝试另一种方法。

    根据迁移版本是否作为 schema_migrations 表中的记录存在,迁移被标记为 updown。 因此,解决问题的一种方法是添加一个文件 app/models/schema_migration.rb,其中包含以下内容:

    class SchemaMigration < ActiveRecord::Base
      self.primary_key = :version
      attr_accessible :version
    
      # you can call the method below via console or even call
      # or execute the commands directly from the rails console
      def self.fix_migrations
         # basically a list of all migrations that you run on server but are not marked as up
        down_migrations = %w(20160707102753 20160707102754 ... 20160707102771)
        down_migrations.each do |m|
          # this will add an entry in the schema_migrations datatable
          # on server so rake db:migrate won't try to run these again
          SchemaMigration.create(version: m) 
        end        
      end
    
    end
    

    然后通过 rails 控制台,你执行:SchemaMigration.fix_migrations

    如果您需要再次运行特定迁移,或者您不小心添加了以前从未执行过的迁移版本,您始终可以使用 SchemaMigration.find_by_version('xxxx').delete 从 schema_migrations 中删除该条目。这将允许rake db:migrate 再次尝试运行该迁移。

    【讨论】:

      猜你喜欢
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2015-06-28
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多