mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Set active_record config for always creating uuids in generators
This commit is contained in:
parent
7b92798d2f
commit
fb42c492a7
4 changed files with 26 additions and 1 deletions
|
@ -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*
|
||||
|
||||
* Qualify column name inserted by `group` in calculation
|
||||
|
||||
Giving `group` an unqualified column name now works, even if the relation
|
||||
|
|
|
@ -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 %>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in a new issue