1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move default uuid generation to active_record

This commit is contained in:
Jon McCartie 2015-10-22 09:11:42 -07:00 committed by Rafael Mendonça França
parent f94e328cf8
commit f0323288da
5 changed files with 16 additions and 11 deletions

View file

@ -7,8 +7,9 @@
* 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
config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
*Jon McCartie*

View file

@ -12,6 +12,14 @@ module ActiveRecord
def self.base_root
File.dirname(__FILE__)
end
private
def primary_key_type
key_type = Rails::Generators.options[:active_record][:primary_key_type]
", id: :#{key_type}" if key_type
end
end
end
end

View file

@ -1,6 +1,6 @@
class <%= migration_class_name %> < ActiveRecord::Migration
def change
create_table :<%= table_name %><%= id_kind %> do |t|
create_table :<%= table_name %><%= primary_key_type %> do |t|
<% attributes.each do |attribute| -%>
<% if attribute.password_digest? -%>
t.string :password_digest<%= attribute.inject_options %>

View file

@ -30,11 +30,6 @@ 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

View file

@ -222,8 +222,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
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
previous_value = Rails.application.config.generators.active_record[:primary_key_type]
Rails.application.config.generators.active_record[:primary_key_type] = :uuid
run_generator ["create_books"]
assert_migration "db/migrate/create_books.rb" do |content|
assert_method :change, content do |change|
@ -231,7 +232,7 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
Rails.application.config.active_record.primary_key = previous_value
Rails.application.config.generators.active_record[:primary_key_type] = previous_value
end
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create