diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-09-06 13:03:09 -0700 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-09-06 13:03:09 -0700 |
commit | 75bde023217a1343e386fef6c6524fb67116e23c (patch) | |
tree | 79263e71bde4866946ed8899a10a1db9061b4b51 | |
parent | e8c9f0513da0fac45ba585cf62739da57603f4ed (diff) | |
parent | d965bbfe9f45be8d203a85482923253c751a20bd (diff) | |
download | rails-75bde023217a1343e386fef6c6524fb67116e23c.tar.gz rails-75bde023217a1343e386fef6c6524fb67116e23c.tar.bz2 rails-75bde023217a1343e386fef6c6524fb67116e23c.zip |
Merge pull request #7419 from releu/fix-invalid-filename-migration-generator
Ensure valid migration filename on generating migration
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/migration_generator.rb | 9 | ||||
-rw-r--r-- | railties/test/generators/migration_generator_test.rb | 7 |
2 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb index f6a432c6e5..a3c274d9b9 100644 --- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb +++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb @@ -7,6 +7,7 @@ module ActiveRecord def create_migration_file set_local_assigns! + validate_file_name! migration_template "migration.rb", "db/migrate/#{file_name}.rb" end @@ -41,6 +42,14 @@ module ActiveRecord 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 diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 774038c0e1..15e5a0b92b 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -28,6 +28,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase 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" |