2013-07-01 12:57:01 -04:00
|
|
|
require 'active_support/concern'
|
2014-01-06 06:01:15 -05:00
|
|
|
require 'rails/generators/actions/create_migration'
|
2013-07-01 12:57:01 -04:00
|
|
|
|
2009-07-01 05:50:06 -04:00
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
# Holds common methods for migrations. It assumes that migrations has the
|
|
|
|
# [0-9]*_name format and can be used by another frameworks (like Sequel)
|
2010-03-28 03:46:46 -04:00
|
|
|
# just by implementing the next migration version method.
|
2009-07-01 05:50:06 -04:00
|
|
|
module Migration
|
2013-07-01 12:57:01 -04:00
|
|
|
extend ActiveSupport::Concern
|
2010-02-03 11:57:54 -05:00
|
|
|
attr_reader :migration_number, :migration_file_name, :migration_class_name
|
|
|
|
|
2010-01-30 11:38:42 -05:00
|
|
|
module ClassMethods
|
|
|
|
def migration_lookup_at(dirname) #:nodoc:
|
|
|
|
Dir.glob("#{dirname}/[0-9]*_*.rb")
|
|
|
|
end
|
|
|
|
|
|
|
|
def migration_exists?(dirname, file_name) #:nodoc:
|
|
|
|
migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_migration_number(dirname) #:nodoc:
|
|
|
|
migration_lookup_at(dirname).collect do |file|
|
|
|
|
File.basename(file).split("_").first.to_i
|
|
|
|
end.max.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_migration_number(dirname) #:nodoc:
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-06 06:01:15 -05:00
|
|
|
def create_migration(destination, data, config = {}, &block)
|
|
|
|
action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_migration_assigns!(destination)
|
|
|
|
destination = File.expand_path(destination, self.destination_root)
|
|
|
|
|
|
|
|
migration_dir = File.dirname(destination)
|
|
|
|
@migration_number = self.class.next_migration_number(migration_dir)
|
|
|
|
@migration_file_name = File.basename(destination, '.rb')
|
|
|
|
@migration_class_name = @migration_file_name.camelize
|
|
|
|
end
|
|
|
|
|
2009-07-01 05:50:06 -04:00
|
|
|
# Creates a migration template at the given destination. The difference
|
2010-03-28 03:46:46 -04:00
|
|
|
# to the default template method is that the migration version is appended
|
2009-07-01 05:50:06 -04:00
|
|
|
# to the destination file name.
|
|
|
|
#
|
2010-03-28 03:46:46 -04:00
|
|
|
# The migration version, migration file name, migration class name are
|
2009-07-01 05:50:06 -04:00
|
|
|
# available as instance variables in the template to be rendered.
|
|
|
|
#
|
|
|
|
# migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"
|
2014-01-06 06:01:15 -05:00
|
|
|
def migration_template(source, destination, config = {})
|
|
|
|
source = File.expand_path(find_in_source_paths(source.to_s))
|
2009-07-01 05:50:06 -04:00
|
|
|
|
2014-01-06 06:01:15 -05:00
|
|
|
set_migration_assigns!(destination)
|
|
|
|
context = instance_eval('binding')
|
2009-07-01 05:50:06 -04:00
|
|
|
|
2014-01-06 06:01:15 -05:00
|
|
|
dir, base = File.split(destination)
|
|
|
|
numbered_destination = File.join(dir, ["%migration_number%", base].join('_'))
|
2009-07-01 06:07:05 -04:00
|
|
|
|
2014-01-06 06:01:15 -05:00
|
|
|
create_migration numbered_destination, nil, config do
|
|
|
|
ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context)
|
2009-07-01 05:50:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|