Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primary_key_default option to create table migrations #46353

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

paulyoder
Copy link
Contributor

@paulyoder paulyoder commented Oct 26, 2022

Motivation / Background

This Pull Request has been created because I want to automatically set the default: argument on create table migrations to "uuid_generate_v4()". The primary key type on my tables are :uuid and I want to use the uuid_generate_v4() method from Postgres to generate the id values.

Detail

It adds the primary_key_default option on the migration generator, which allows an initializer to set the value to automatically use on all 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
rails generate migration create_books
class CreateBooks < ActiveRecord::Migration[7.0]
  def change
    create_table :books, id: :uuid, default: "uuid_generate_v4()" do |t|

      t.timestamps
    end
  end
end

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
  • CI is passing.

@bdewater
Copy link
Contributor

@paulyoder
Copy link
Contributor Author

Thanks @bdewater I'll work on updating that documentation as well

@paulyoder
Copy link
Contributor Author

@bdewater I updated the active record postgres documentation.

@paulyoder paulyoder force-pushed the primary_key_default branch 3 times, most recently from efade76 to 5262fda Compare November 2, 2022 15:06
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
@saiqulhaq
Copy link
Contributor

please fix the conflict 😅

@zzak
Copy link
Member

zzak commented Feb 10, 2023

This makes sense to me, if we have --primary-key-type to also have --primary-key-default 🤔

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default: syntax in create_table method is it an existing option? or just introduced in this PR?
when I checked in https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
there is no default setting for create_table method
if it's just introduced in this PR, I would prefer to use clearer name, maybe default_uuid
what do you think?

Copy link
Contributor

@bdewater bdewater Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are options passed through for table_definition.set_primary_key and uses this allowlist:

def valid_primary_key_options # :nodoc:
[:limit, :default, :precision]
end

which ultimately is just a column definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants