Skip to content

Commit

Permalink
Add primary_key_default option to create table migrations
Browse files Browse the repository at this point in the history
This allows setting the `default:` argument when creating a migration

    rails generate migration create_books --primary_key_type=uuid
    --primary_key_default="uuid_generate_v4()"

    class CreateBooks < ActiveRecord::Migration[7.0]
      def change
        create_table :books, id: :uuid, default: "uuid_generate_v4()" do |t|
        end
      end
    end

It also allows you to create an initializer to configure the generator to
use the `primary_key_default` value for all new create table migrations.

    Rails.application.config.generators do |g|
      g.orm(:active_record, primary_key_type: :uuid)
      g.orm(:active_record, primary_key_default: "\"uuid_generate_v4()\"")
    end
  • Loading branch information
paulyoder committed Nov 2, 2022
1 parent 7db044f commit 82592dc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
31 changes: 31 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
* Add `primary-key-default` option to create table migrations.

This allows setting the `default` argument when creating a migration

```bash
rails generate migration create_books --primary-key-type=uuid --primary-key-default="uuid_generate_v4()"
```

```ruby
class CreateBooks < ActiveRecord::Migration[7.0]
def change
create_table :books, id: :uuid, default: "uuid_generate_v4()" do |t|

t.timestamps
end
end
end
```

It also allows you to create an initializer to configure the generator to
use the `primary_key_default` value for all new create table migrations.

```ruby
Rails.application.config.generators do |g|
g.orm(:active_record, primary_key_type: :uuid)
g.orm(:active_record, primary_key_default: "\"uuid_generate_v4()\"")
end
```

*Paul Yoder*

* Clear locking column on #dup

This change fixes not to duplicate locking_column like id and timestamps.
Expand Down
5 changes: 5 additions & 0 deletions activerecord/lib/rails/generators/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def primary_key_type
", id: :#{key_type}" if key_type
end

def primary_key_default
key_default = options[:primary_key_default]
", default: #{key_default}" if key_default
end

def foreign_key_type
key_type = options[:primary_key_type]
", type: :#{key_type}" if key_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MigrationGenerator < Base # :nodoc:

class_option :timestamps, type: :boolean
class_option :primary_key_type, type: :string, desc: "The type for primary key"
class_option :primary_key_default, type: :string, desc: "The default value 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."

def create_migration_file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
def change
create_table :<%= table_name %><%= primary_key_type %> do |t|
create_table :<%= table_name %><%= primary_key_type %><%= primary_key_default %> do |t|
<% attributes.each do |attribute| -%>
<% if attribute.password_digest? -%>
t.string :password_digest<%= attribute.inject_options %>
Expand Down
21 changes: 21 additions & 0 deletions guides/source/active_record_postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,27 @@ When building a model with a foreign key that will reference this UUID, treat
rails generate model Case device_id:uuid
```

To set the default value on the primary key, pass it in the
`--primary-key-default={default value}` argument to the model generator.

For example:

```ruby
rails generate model Device --primary-key-type=uuid --primary-key-default="uuid_generate_v4()" kind:string
```

```ruby
# db/migrate/20221028135255_create_devices.rb
class CreateDevices < ActiveRecord::Migration[7.0]
def change
create_table :devices, id: :uuid, default: "uuid_generate_v4()" do |t|
t.string :kind
t.timestamps
end
end
end
```


Generated Columns
-----------------
Expand Down
9 changes: 9 additions & 0 deletions railties/test/generators/migration_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ def test_add_migration_with_references_options_when_primary_key_uuid
end
end

def test_add_primary_key_default_value_to_create_table_migration
run_generator ["create_books", "--primary_key_type=uuid", "--primary_key_default=\"uuid_generate_v4()\""]
assert_migration "db/migrate/create_books.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :books, id: :uuid, default: "uuid_generate_v4\(\)"/, change)
end
end
end

def test_database_puts_migrations_in_configured_folder
with_database_configuration do
run_generator ["create_books", "--database=secondary"]
Expand Down

0 comments on commit 82592dc

Please sign in to comment.