diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 16:15:25 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 16:15:25 -0600 |
commit | 7df83b5d9505136afdca20840c67fc4deb7a1adf (patch) | |
tree | 8a788b8a2eeaf3f66d3722abdb4c61e713729ae8 | |
parent | ea9bc06c9a47b839d5e2db94ba6bf7e29c8f0ae9 (diff) | |
parent | fb42c492a7eff58f45867ea50440d938648cdb48 (diff) | |
download | rails-7df83b5d9505136afdca20840c67fc4deb7a1adf.tar.gz rails-7df83b5d9505136afdca20840c67fc4deb7a1adf.tar.bz2 rails-7df83b5d9505136afdca20840c67fc4deb7a1adf.zip |
Merge pull request #21762 from jmccartie/jm/uuid
Set active_record config for always creating uuids in generators
4 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0c3c21202f..258b2be899 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* Add ability to default to `uuid` as primary key when generating database migrations + + Set `Rails.application.config.active_record.primary_key = :uuid` + or `config.active_record.primary_key = :uuid` in config/application.rb + + *Jon McCartie* + * Don't cache arguments in #find_by if they are an ActiveRecord::Relation Fixes #20817 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 index 5b3e57dcf6..4a7deb3c75 100644 --- 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 @@ -1,6 +1,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration def change - create_table :<%= table_name %> do |t| + create_table :<%= table_name %><%= id_kind %> do |t| <% attributes.each do |attribute| -%> <% if attribute.password_digest? -%> t.string :password_digest<%= attribute.inject_options %> diff --git a/railties/lib/rails/generators/migration.rb b/railties/lib/rails/generators/migration.rb index 87f2e1d42b..a755471a4b 100644 --- a/railties/lib/rails/generators/migration.rb +++ b/railties/lib/rails/generators/migration.rb @@ -30,6 +30,11 @@ module Rails end end + def id_kind + kind = Rails.application.config.active_record.primary_key rescue nil + ", id: :#{kind}" if kind + end + def create_migration(destination, data, config = {}, &block) action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config) end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 57bc220558..a7f807b747 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -221,6 +221,19 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_add_uuid_to_create_table_migration + previous_value = Rails.application.config.active_record.primary_key + Rails.application.config.active_record.primary_key = :uuid + run_generator ["create_books"] + assert_migration "db/migrate/create_books.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :books, id: :uuid/, change) + end + end + + Rails.application.config.active_record.primary_key = previous_value + 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"] |