【发布时间】:2013-03-27 22:53:53
【问题描述】:
我正在用 ruby gem 构建 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:install,它会失败并出现以下错误:
Another migration is already named migration1: /Users/jh/Code/Web/demoapp/db/migrate/20130327222221_migration1.rb
我希望它只是默默地忽略已经存在迁移的事实并继续进行下一次迁移。 最好的方法是什么?
编辑:
根据 Dmitry 的回答,这是我的解决方案:
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
【问题讨论】:
-
@Mischa,我的在实际应用程序中确实有有意义的名称。当您重新运行安装生成器来升级它时,这无济于事。
-
啊,我明白了。很高兴你解决了...
标签: ruby-on-rails ruby ruby-on-rails-3.2 gem rails-engines