加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 将新的迁移从Rails引擎gem添加到应用程序通过

发布时间:2020-12-16 22:01:33 所属栏目:百科 来源:网络整理
导读:我正在建立一个ruby宝石的Rails引擎.它包括当您运行时调用的一些迁移: rails g myengine:install 生成器中的代码如下: module MyEngine module Generators class InstallGenerator ::Rails::Generators::Base include Rails::Generators::Migration source
我正在建立一个ruby宝石的Rails引擎.它包括当您运行时调用的一些迁移:
rails g myengine:install

生成器中的代码如下:

module MyEngine
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      include Rails::Generators::Migration

      source_root File.expand_path('../templates',__FILE__)

      # ...

      def copy_migrations
        migration_template "migrations/migration1.rb","db/migrate/migration1.rb"
        migration_template "migrations/migration2.rb","db/migrate/migration2.rb"
      end

      # ...
    end
  end
end

但是,如果我运行rails g myengine:再次安装,则会失败,并显示此错误:

Another migration is already named migration1: /Users/jh/Code/Web/demoapp/db/migrate/20130327222221_migration1.rb

我希望它只是默默地忽略已经有迁移的事实,并继续下一次迁移.这样做最好的方法是什么?

编辑:

根据德米特里的回答,这是我的解决办法:

def copy_migrations
    copy_migration "migration1"
    copy_migration "migration2"
  end

protected

  def copy_migration(filename)
    if self.class.migration_exists?("db/migrate","#{filename}")
      say_status("skipped","Migration #{filename}.rb already exists")
    else
      migration_template "migrations/#{filename}.rb","db/migrate/#{filename}.rb"
    end
  end

解决方法

在Rails中使用 migration_template作为示例,您可以检查destination = self.class.migration_exists?(migration_dir,@migration_file_name),如果迁移已存在,请跳过进行migration_template调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读