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
|
2010-02-03 11:57:54 -05:00
|
|
|
attr_reader :migration_number, :migration_file_name, :migration_class_name
|
|
|
|
|
2009-07-13 14:42:25 -04:00
|
|
|
def self.included(base) #:nodoc:
|
2010-02-03 11:57:54 -05:00
|
|
|
base.extend ClassMethods
|
2009-07-13 14:42:25 -04:00
|
|
|
end
|
2009-07-01 05:50:06 -04:00
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
#
|
|
|
|
# migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb"
|
|
|
|
#
|
2009-07-15 14:16:37 -04:00
|
|
|
def migration_template(source, destination=nil, config={})
|
2009-07-01 05:50:06 -04:00
|
|
|
destination = File.expand_path(destination || source, self.destination_root)
|
|
|
|
|
|
|
|
migration_dir = File.dirname(destination)
|
2010-01-30 11:38:42 -05:00
|
|
|
@migration_number = self.class.next_migration_number(migration_dir)
|
2009-07-01 05:50:06 -04:00
|
|
|
@migration_file_name = File.basename(destination).sub(/\.rb$/, '')
|
|
|
|
@migration_class_name = @migration_file_name.camelize
|
|
|
|
|
2010-01-30 11:38:42 -05:00
|
|
|
destination = self.class.migration_exists?(migration_dir, @migration_file_name)
|
2009-07-01 06:07:05 -04:00
|
|
|
|
2010-12-10 16:16:34 -05:00
|
|
|
if !(destination && options[:skip]) && behavior == :invoke
|
2010-09-02 10:25:36 -04:00
|
|
|
if destination && options.force?
|
|
|
|
remove_file(destination)
|
|
|
|
elsif destination
|
|
|
|
raise Error, "Another migration is already named #{@migration_file_name}: #{destination}"
|
|
|
|
end
|
2009-07-01 06:07:05 -04:00
|
|
|
destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb")
|
2009-07-01 05:50:06 -04:00
|
|
|
end
|
|
|
|
|
2009-07-15 14:16:37 -04:00
|
|
|
template(source, destination, config)
|
2009-07-01 05:50:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|