2016-10-13 5 views
0

レールジェネレータのワークフローで何かが誤解されていますが、コードやドキュメントを検索した後、問題。Rails 5ジェネレータdb:ロールバックは何もしませんが、マイグレーションファイルを削除する前に呼び出されます。

私は足場ファイルを作成した直後にいくつかの余分なファイルを追加し、生成された移行を実行するためのカスタム足場ジェネレータを作成しました。 同じ方法で、最初のものとしてマイグレーションをロールバックしようとしていますrails destroy my_scaffoldコマンドは、マイグレーションファイルが削除される前にロールバックするために実行されます。

私のカスタムジェネレータコードscaffold_meta.rb、マイグレーションファイルの作成後にdb:migrateコマンドを実行します。これが働く部分です。

require 'generators/resource/resource_generator' 
module Rails 
    module Generators 
    class ScaffoldMetaGenerator < ResourceGenerator # :nodoc: 
     remove_hook_for :resource_controller 
     remove_class_option :actions 

     class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets" 
     class_option :stylesheet_engine, desc: "Engine for Stylesheets" 
     class_option :assets, type: :boolean 
     class_option :resource_route, type: :boolean 
     class_option :scaffold_stylesheet, type: :boolean 
     class_option :steps, type: :boolean, default: 'step' 

     def handle_skip 
     @options = @options.merge(stylesheets: false) unless options[:assets] 
     @options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] && options[:scaffold_stylesheet] 
     end 

     hook_for :scaffold_controller, required: true 

     hook_for :assets do |assets| 
     invoke assets, [controller_name] 
     end 

     hook_for :stylesheet_engine do |stylesheet_engine| 
     if behavior == :invoke 
      invoke stylesheet_engine, [controller_name] 
     end 
     end 

     def mirate_if_invoke 
     if behavior == :invoke 
      say behavior.to_s + ' migrate', :green 
      rake("db:migrate --trace") 
     end 
     end 

     invoke 'step' 

    end 
    end 
end 

以前のコードでは、私のカスタム移行ファイルを削除する前にロールバックしようとしmodel_generator.rbを呼び出してしまいます。ロールバック suposedlyと呼ばれているが、効果はありません:REVOKE動作と発電機の

require 'rails/generators/model_helpers' 

module Rails 
    module Generators 
    class ModelGenerator < Rails::Generators::NamedBase # :nodoc: 
     include Rails::Generators::ModelHelpers 

     def rollback_if_revoke 
     if self.behavior == :revoke 
      say behavior.to_s + ' rollback', :red 
      rake("db:rollback --trace") 
     end 
     end 

     argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]" 
     hook_for :orm, required: true, desc: "ORM to be invoked" 
    end 
    end 
end 

出力はすくいデシベルを示します。

$rails d scaffold_meta pez edad:integer nombre 

Running via Spring preloader in process 8888 

***revoke rollback 

    rake db:rollback --trace*** 
    invoke active_record 
    remove db/migrate/20161013145014_create_pezs.rb 
    remove app/models/pez.rb 
    invoke rspec` 

非常に参考になります。

答えて

0

Rails 4.7.0でも同じ問題が発生しました。

rake db:rollbackは、behavior == :invokeのときに働いていたが、何もしなかったときに気づいたbehavior == :revoke

rake方法源を見て:in_rootに渡されたブロックで呼び出さrun方法はThor::Actionsモジュールからである

# GEMDIR/railties-4.2.7/lib/rails/generators/actions.rb: 

    # Runs the supplied rake task 
    # 
    # rake("db:migrate") 
    # rake("db:migrate", env: "production") 
    # rake("gems:install", sudo: true) 
    def rake(command, options={}) 
    log :rake, command 
    env = options[:env] || ENV["RAILS_ENV"] || 'development' 
    sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : '' 
    in_root { run("#{sudo}#{extify(:rake)} #{command} RAILS_ENV=#{env}", verbose: false) } 
    end 

# GEMDIR/thor-0.19.1/lib/thor/actions.rb: 

# Executes a command returning the contents of the command. 
# 
# ==== Parameters 
# command<String>:: the command to be executed. 
# config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output. Specify :with 
#    to append an executable to command execution. 
# 
# ==== Example 
# 
# inside('vendor') do 
#  run('ln -s ~/edge rails') 
# end 
# 
def run(command, config = {}) 
    return unless behavior == :invoke 

    destination = relative_to_original_destination_root(destination_root, false) 
    desc = "#{command} from #{destination.inspect}" 

    if config[:with] 
    desc = "#{File.basename(config[:with].to_s)} #{desc}" 
    command = "#{config[:with]} #{command}" 
    end 

    say_status :run, desc, config.fetch(:verbose, true) 

    unless options[:pretend] 
    config[:capture] ? `#{command}` : system("#{command}") 
    end 
end 

方法の最初の行、return unless behavior == :invoke短いです元のrake 'db:rollback'コマンドを実行する呼び出しを呼び出します。

欄外はまた、Thorのinvoke \ revokeロジックに従うと思われるActionsモジュール(Rails::Generators::Actions)を持っています。だから私は、発電機が:revoke州にあるときにロールバックを試みないことが最善であると結論づけました。

私は:invoke上の移行ファイルを生成し、:revokeにそれを破壊し、必要なときに手動でrake db:migrateまたはrake db:rollbackを実行するためにユーザーに頼るRailsの規約を以下終わってきました。

希望に役立ちます。

関連する問題