diff options
author | José Valim <jose.valim@gmail.com> | 2009-07-01 11:50:06 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-07-02 10:27:39 +0200 |
commit | 10a9205c583a95ef35704aca93079b1700dd35f8 (patch) | |
tree | 554b0a0cfdb88fc5b00ec33c9bbf8b66fa327856 /railties/lib/generators/migration.rb | |
parent | b77a7dcf4d1b072775e4f276ca59e18eaccc84ef (diff) | |
download | rails-10a9205c583a95ef35704aca93079b1700dd35f8.tar.gz rails-10a9205c583a95ef35704aca93079b1700dd35f8.tar.bz2 rails-10a9205c583a95ef35704aca93079b1700dd35f8.zip |
Put migration methods into a separate folder and let just ActiveRecord dependency in ActiveRecord models.
Diffstat (limited to 'railties/lib/generators/migration.rb')
-rw-r--r-- | railties/lib/generators/migration.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/railties/lib/generators/migration.rb b/railties/lib/generators/migration.rb new file mode 100644 index 0000000000..1981ea6a59 --- /dev/null +++ b/railties/lib/generators/migration.rb @@ -0,0 +1,56 @@ +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) + # just by implementing the next migration number method. + # + module Migration + + # Creates a migration template at the given destination. The difference + # to the default template method is that the migration number is appended + # to the destination file name. + # + # The migration number, migration file name, migration class name are + # available as instance variables in the template to be rendered. + # + # ==== Examples + # + # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" + # + def migration_template(source, destination=nil, log_status=true) + destination = File.expand_path(destination || source, self.destination_root) + + migration_dir = File.dirname(destination) + @migration_number = next_migration_number(migration_dir) + @migration_file_name = File.basename(destination).sub(/\.rb$/, '') + @migration_class_name = @migration_file_name.camelize + + if existing = migration_exists?(migration_dir, @migration_file_name) + raise Rails::Generators::Error, "Another migration is already named #{@migration_file_name}: #{existing}" + end + + destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb") + template(source, destination, log_status) + end + + protected + + 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{ |f| f.split("_").first.to_i }.max + end + + def next_migration_number(dirname) #:nodoc: + raise NotImplementError + end + + end + end +end |