From 20e041579f5fe9df51bdc40048cc1ef78915e116 Mon Sep 17 00:00:00 2001 From: Sammy Larbi Date: Wed, 26 Dec 2012 13:02:28 -0600 Subject: Support creating a table migration generator Sometimes you want to create a table without an associated model and test, which is also not a join table. With this commit, you can now do that. Example: rails g migration create_posts title:string or rails g migration CreatePosts title:string This commit also moves the template the model generator uses for the migration to the migration templates folder, as it seems a more sensible place for it now that it is shared code. --- activerecord/CHANGELOG.md | 13 ++++++++++++- .../active_record/migration/migration_generator.rb | 11 +++++++++-- .../migration/templates/create_table_migration.rb | 15 +++++++++++++++ .../active_record/model/model_generator.rb | 2 +- .../active_record/model/templates/migration.rb | 15 --------------- guides/source/migrations.md | 21 +++++++++++++++++++++ .../test/generators/migration_generator_test.rb | 17 ++++++++++++++--- 7 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb delete mode 100644 activerecord/lib/rails/generators/active_record/model/templates/migration.rb diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 384ec03334..235a91fafa 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,15 @@ ## Rails 4.0.0 (unreleased) ## +* Added support for creating a table via Rails migration generator. + For example, + + rails g migration create_books title:string content:text + + will generate a migration that creates a table called books with + the listed attributes, without creating a model. + + *Sammy Larbi* + * Fix bug that raises the wrong exception when the exception handled by PostgreSQL adapter doesn't respond to `#result`. Fixes #8617. @@ -362,7 +372,7 @@ deprecated and removed in future versions of Rails. *Amparo Luna + Guillermo Iguaran* - + * `after_commit` and `after_rollback` now validate the `:on` option and raise an `ArgumentError` if it is not one of `:create`, `:destroy` or `:update` @@ -1549,4 +1559,5 @@ *Aaron Patterson* + Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activerecord/CHANGELOG.md) for previous changes. 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 5f1dbe36d6..b967bb6e0f 100644 --- a/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb +++ b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb @@ -8,13 +8,14 @@ module ActiveRecord def create_migration_file set_local_assigns! validate_file_name! - migration_template "migration.rb", "db/migrate/#{file_name}.rb" + migration_template @migration_template, "db/migrate/#{file_name}.rb" end protected attr_reader :migration_action, :join_tables def set_local_assigns! + @migration_template = "migration.rb" case file_name when /^(add|remove)_.*_(?:to|from)_(.*)/ @migration_action = $1 @@ -26,6 +27,9 @@ module ActiveRecord set_index_names end + when /^create_(.+)/ + @table_name = $1.pluralize + @migration_template = "create_table_migration.rb" end end @@ -44,7 +48,10 @@ module ActiveRecord end private - + def attributes_with_index + attributes.select { |a| !a.reference? && a.has_index? } + end + def validate_file_name! unless file_name =~ /^[_a-z0-9]+$/ raise IllegalMigrationNameError.new(file_name) diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb new file mode 100644 index 0000000000..3a3cf86d73 --- /dev/null +++ b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb @@ -0,0 +1,15 @@ +class <%= migration_class_name %> < ActiveRecord::Migration + def change + create_table :<%= table_name %> do |t| +<% attributes.each do |attribute| -%> + t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %> +<% end -%> +<% if options[:timestamps] %> + t.timestamps +<% end -%> + end +<% attributes_with_index.each do |attribute| -%> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> +<% end -%> + end +end diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 5f36181694..40e134e626 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -15,7 +15,7 @@ module ActiveRecord def create_migration_file return unless options[:migration] && options[:parent].nil? attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false - migration_template "migration.rb", "db/migrate/create_#{table_name}.rb" + migration_template "../../migration/templates/create_table_migration.rb", "db/migrate/create_#{table_name}.rb" end def create_model_file diff --git a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb b/activerecord/lib/rails/generators/active_record/model/templates/migration.rb deleted file mode 100644 index 3a3cf86d73..0000000000 --- a/activerecord/lib/rails/generators/active_record/model/templates/migration.rb +++ /dev/null @@ -1,15 +0,0 @@ -class <%= migration_class_name %> < ActiveRecord::Migration - def change - create_table :<%= table_name %> do |t| -<% attributes.each do |attribute| -%> - t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %> -<% end -%> -<% if options[:timestamps] %> - t.timestamps -<% end -%> - end -<% attributes_with_index.each do |attribute| -%> - add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> -<% end -%> - end -end diff --git a/guides/source/migrations.md b/guides/source/migrations.md index d738d847e9..89ae564c24 100644 --- a/guides/source/migrations.md +++ b/guides/source/migrations.md @@ -179,6 +179,27 @@ class AddDetailsToProducts < ActiveRecord::Migration end ``` +If the migration name is of the form "CreateXXX" and is +followed by a list of column names and types then a migration creating the table +XXX with the columns listed will be generated. For example: + +```bash +$ rails generate migration CreateProducts name:string part_number:string +``` + +generates + +```ruby +class CreateProducts < ActiveRecord::Migration + def change + create_table :products do |t| + t.string :name + t.string :part_number + end + end +end +``` + As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit by editing the `db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb` file. diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 62d9d1f06a..d876597944 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -172,8 +172,19 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end - def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove - migration = "create_books" + def test_create_table_migration + run_generator ["create_books", "title:string", "content:text"] + assert_migration "db/migrate/create_books.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :books/, change) + assert_match(/ t\.string :title/, change) + assert_match(/ t\.text :content/, change) + end + end + end + + def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create + migration = "delete_books" run_generator [migration, "title:string", "content:text"] assert_migration "db/migrate/#{migration}.rb" do |content| @@ -182,7 +193,7 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end end - + def test_properly_identifies_usage_file assert generator_class.send(:usage_path) end -- cgit v1.2.3