Ensure valid migration filename on generating migration #7419

Merged
merged 1 commit into from Sep 6, 2012
View
9 activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
@@ -7,6 +7,7 @@ class MigrationGenerator < Base
def create_migration_file
set_local_assigns!
+ validate_file_name!
migration_template "migration.rb", "db/migrate/#{file_name}.rb"
end
@@ -41,6 +42,14 @@ def index_name_for(attribute)
attribute.name.singularize.foreign_key
end.to_sym
end
+
+ private
+
+ def validate_file_name!
+ unless file_name =~ /^[_a-z0-9]+$/
+ raise IllegalMigrationNameError.new(file_name)
+ end
+ end
end
end
end
View
7 railties/test/generators/migration_generator_test.rb
@@ -28,6 +28,13 @@ def test_migration_with_class_name
run_generator [migration]
assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/
end
+
+ def test_migration_with_invalid_file_name
+ migration = "add_something:datetime"
+ assert_raise ActiveRecord::IllegalMigrationNameError do
+ run_generator [migration]
+ end
+ end
def test_add_migration_with_attributes
migration = "add_title_body_to_posts"