From 93d2090011b5ccab504b8aeabf6a95b23193a689 Mon Sep 17 00:00:00 2001 From: Michael Duchemin Date: Sun, 27 Jan 2019 00:23:47 -0500 Subject: [PATCH] Add class option timestamps to migration generator Fixes GH#28706. Now rails g migration create_users and rails g model User have the same behavior for timestamps since they implement the same migration template. The expected behavior is that this create table migration will create the table with timestamps unless you pass --no-timestamps or --skip-timestamps to the generator. The expected migration should match what you get when you use the model generator. Using the migration generator, which doesn't have a class_option for timestamps would cause them to not be added to the migration file. Now the migration behavior of the migration generator, create_table only, is aligned with the migration behavior of the model generator. Also modified relevant example of ActiveRecord Migrations Guide. --- activerecord/CHANGELOG.md | 4 ++++ .../migration/migration_generator.rb | 1 + guides/source/active_record_migrations.md | 2 ++ .../test/generators/migration_generator_test.rb | 15 +++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f16a746b91158..618aecfd5d36d 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow generated create_table migrations to include or skip timestamps + + *Michael Duchemin* + * Fix `relation.create` to avoid leaking scope to initialization block and callbacks. Fixes #9894, #17577. 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 dd79bcf542ed8..b9880895b3aca 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 Generators # :nodoc: class MigrationGenerator < Base # :nodoc: argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]" + class_option :timestamps, type: :boolean class_option :primary_key_type, type: :string, desc: "The type for primary key" class_option :database, type: :string, aliases: %i(db), desc: "The database for your migration. By default, the current environment's primary database is used." diff --git a/guides/source/active_record_migrations.md b/guides/source/active_record_migrations.md index 905c76e5c1cae..52ed0de1632bd 100644 --- a/guides/source/active_record_migrations.md +++ b/guides/source/active_record_migrations.md @@ -225,6 +225,8 @@ class CreateProducts < ActiveRecord::Migration[5.0] create_table :products do |t| t.string :name t.string :part_number + + t.timestamps end end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 5812cbdfc9123..b8958f6f99048 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -245,6 +245,21 @@ def test_create_table_migration end end + def test_create_table_migration_with_timestamps + run_generator ["create_books", "title:string", "content:text"] + assert_migration "db/migrate/create_books.rb", /t.timestamps/ + end + + def test_create_table_timestamps_are_skipped + run_generator ["create_books", "title:string", "content:text", "--no-timestamps"] + + assert_migration "db/migrate/create_books.rb" do |m| + assert_method :change, m do |change| + assert_no_match(/t.timestamps/, change) + end + end + end + def test_add_uuid_to_create_table_migration run_generator ["create_books", "--primary_key_type=uuid"] assert_migration "db/migrate/create_books.rb" do |content|