2014-03-13 15:02:32 -04:00
|
|
|
require 'thor/actions'
|
2014-01-06 06:01:15 -05:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
module Actions
|
2015-12-18 22:00:49 -05:00
|
|
|
class CreateMigration < Thor::Actions::CreateFile #:nodoc:
|
2014-01-06 06:01:15 -05:00
|
|
|
|
|
|
|
def migration_dir
|
|
|
|
File.dirname(@destination)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migration_file_name
|
|
|
|
@base.migration_file_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def identical?
|
|
|
|
exists? && File.binread(existing_migration) == render
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke!
|
|
|
|
say_destination = exists? ? relative_existing_migration : relative_destination
|
|
|
|
say_status :remove, :red, say_destination
|
|
|
|
return unless exists?
|
|
|
|
::FileUtils.rm_rf(existing_migration) unless pretend?
|
|
|
|
existing_migration
|
|
|
|
end
|
|
|
|
|
|
|
|
def relative_existing_migration
|
|
|
|
base.relative_to_original_destination_root(existing_migration)
|
|
|
|
end
|
|
|
|
|
|
|
|
def existing_migration
|
|
|
|
@existing_migration ||= begin
|
|
|
|
@base.class.migration_exists?(migration_dir, migration_file_name) ||
|
|
|
|
File.exist?(@destination) && @destination
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :exists? :existing_migration
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2014-10-17 14:52:32 -04:00
|
|
|
def on_conflict_behavior
|
2014-01-06 06:01:15 -05:00
|
|
|
options = base.options.merge(config)
|
|
|
|
if identical?
|
|
|
|
say_status :identical, :blue, relative_existing_migration
|
|
|
|
elsif options[:force]
|
|
|
|
say_status :remove, :green, relative_existing_migration
|
|
|
|
say_status :create, :green
|
|
|
|
unless pretend?
|
|
|
|
::FileUtils.rm_rf(existing_migration)
|
2014-10-17 14:52:32 -04:00
|
|
|
yield
|
2014-01-06 06:01:15 -05:00
|
|
|
end
|
|
|
|
elsif options[:skip]
|
|
|
|
say_status :skip, :yellow
|
|
|
|
else
|
|
|
|
say_status :conflict, :red
|
|
|
|
raise Error, "Another migration is already named #{migration_file_name}: " +
|
2014-07-18 17:21:53 -04:00
|
|
|
"#{existing_migration}. Use --force to replace this migration " +
|
2014-07-18 11:51:36 -04:00
|
|
|
"or --skip to ignore conflicted file."
|
2014-01-06 06:01:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def say_status(status, color, message = relative_destination)
|
|
|
|
base.shell.say_status(status, message, color) if config[:verbose]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|