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

Set active_record config for always creating uuids in generators #21762

Merged
merged 1 commit into from Oct 20, 2015

Conversation

@jmccartie
Copy link
Contributor

jmccartie commented Sep 25, 2015

UUID's are awesome (and they improve performance). I want them everywhere, but it's annoying to have to modify my DB migrations every time. I wish I could make an application config to force the generators to always add id: :uuid whenever I create a table.

Throw this in your application.rb file...

config.active_record.primary_key = :uuid

...and id: :uuid will be added to every create_table generation:

@rails-bot
Copy link

rails-bot commented Sep 25, 2015

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@sgrif
Copy link
Contributor

sgrif commented Sep 25, 2015

This has falling tests

@jmccartie
Copy link
Contributor Author

jmccartie commented Sep 25, 2015

@sgrif Thanks for the heads up. Tests passing.

I'm pretty unfamiliar with the AR/Railties code, but I think I figured out how these generators work (kinda). I left the primary_key setting wide open for now. Let me know if this is where it should go. (and if this is even a decent idea in the first place .... though it certainly scratches an itch I've had recently)

@@ -221,6 +221,16 @@ def test_create_table_migration
end
end

def test_add_uuid_to_create_table_migration
Rails.application.config.active_record.primary_key = :uuid

This comment has been minimized.

@sgrif

sgrif Oct 20, 2015 Contributor

Do we need to set this back to the old value at the end of this test?

@sgrif
Copy link
Contributor

sgrif commented Oct 20, 2015

Can you squash and rebase after addressing that comment? This will need a changelog entry as well.

@jmccartie jmccartie force-pushed the jmccartie:jm/uuid branch from 8470519 Oct 20, 2015
@jmccartie jmccartie force-pushed the jmccartie:jm/uuid branch to fb42c49 Oct 20, 2015
@jmccartie
Copy link
Contributor Author

jmccartie commented Oct 20, 2015

@sgrif Done. I'll watch the tests and let you know once it's all green...

@sgrif
Copy link
Contributor

sgrif commented Oct 20, 2015

Thanks. I'm going through the backlog so you won't be able to keep up with the merge conflicts I think. Just ping me once the tests are green and I'll merge manually.

@jmccartie
Copy link
Contributor Author

jmccartie commented Oct 20, 2015

@sgrif All checks passed. Merge conflict. :)

@sgrif sgrif merged commit fb42c49 into rails:master Oct 20, 2015
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
sgrif added a commit that referenced this pull request Oct 20, 2015
Set active_record config for always creating uuids in generators
@vincentopensourcetaiwan

This comment has been minimized.

Copy link

vincentopensourcetaiwan commented on fb42c49 Oct 21, 2015

Great!

This comment has been minimized.

Copy link
Contributor Author

jmccartie replied Oct 21, 2015

Thanks @vincentopensourcetaiwan! I hope it helps you!

This comment has been minimized.

Copy link

samnang replied Oct 22, 2015

Nice job! @jmccartie

This comment has been minimized.

Copy link

rizidoro replied Oct 22, 2015

👏

This comment has been minimized.

Copy link

emaiax replied Oct 23, 2015

incredible! 👏

This comment has been minimized.

Copy link

jpwynn replied Jun 10, 2016

Rail 5 only, is that correct?

This comment has been minimized.

Copy link
Member

rafaelfranca replied Jun 11, 2016

Yes

This comment has been minimized.

Copy link

helperhaps replied Sep 27, 2016

@jmccartie Couse the default generate method of uuid is uuid_generate_v4 (ref:#20518) ,when use postgresql's pgcrypto extension, the migration will failed with function uuid_generate_v4() does not exist obviously. To fix that need to edit the edit phase by hands (add " default: 'gen_random_uuid()' by hands")

create_table :tables, id: :uuid, default: 'gen_random_uuid()' do |t|

So if use pgcrypto to generate uuid,

config.generators do |g|
    g.orm :active_record, primary_key_type: :uuid
end

this is not so good, why not make it like

config.generators do |g|
    g.orm :active_record, primary_key_type: :uuid, generate_method: 'gen_random_uuid()'
end

Or it can handle that but i did't find?

This comment has been minimized.

Copy link
Member

pixeltrix replied Sep 27, 2016

@helperhaps you'd be better off opening a ticket than commenting on an 12 month old commit. We should probably handle that in the PostgreSQL adapter rather than adding another config.

This comment has been minimized.

Copy link

helperhaps replied Sep 27, 2016

@pixeltrix Thanks for your comment

@samnang

This comment has been minimized.

@jmccartie is there any reason why we need rescue nil here? or I am missing something here? Because what I thought, Rails.application.config.active_record.primary_key either return assigned value or nil. rescue nil will catch everything even NoMethodError or NameError which usually happen because of typo.

This comment has been minimized.

Copy link
Contributor Author

jmccartie replied Oct 22, 2015

@samnang Trying to remember now ... I think there was an edge case where Rails.application.config.active_record was not set (maybe in engines? or when AR is not included?), so I was avoiding chaining try's together. I suppose it could specifically catch an undefined method error in a future iteration.

This comment has been minimized.

Copy link

samnang replied Oct 22, 2015

@jmccartie Oh, I see. In that case, we probably need to put a guard clause there. 👍

This comment has been minimized.

Copy link
Member

pixeltrix replied Oct 22, 2015

👍 for removing rescue nil - we've been bitten by that too many times before.

This comment has been minimized.

Copy link
Contributor Author

jmccartie replied Oct 22, 2015

@pixeltrix @samnang Thanks for your feedback -- I'll make that change real quick

This comment has been minimized.

Copy link
Contributor Author

jmccartie replied Oct 22, 2015

@pixeltrix @samnang Found it. One of the tests (test/generators/plugin_generator_test.rb) fails because Rails.application is not set. Thoughts on this?

def id_kind
  return unless Rails.application.try(:config)
  kind = Rails.application.config.active_record.primary_key
  ", id: :#{kind}" if kind
end

This comment has been minimized.

Copy link
Member

rafaelfranca replied Oct 22, 2015

That test that should be fixed.

This comment has been minimized.

Copy link
Contributor Author

jmccartie replied Oct 22, 2015

Opened #22033

@vincentopensourcetaiwan

This comment has been minimized.

Copy link

vincentopensourcetaiwan commented on fb42c49 Oct 21, 2015

Great!

This comment has been minimized.

Copy link
Contributor Author

jmccartie replied Oct 21, 2015

Thanks @vincentopensourcetaiwan! I hope it helps you!

This comment has been minimized.

Copy link

samnang replied Oct 22, 2015

Nice job! @jmccartie

This comment has been minimized.

Copy link

rizidoro replied Oct 22, 2015

👏

This comment has been minimized.

Copy link

emaiax replied Oct 23, 2015

incredible! 👏

This comment has been minimized.

Copy link

jpwynn replied Jun 10, 2016

Rail 5 only, is that correct?

This comment has been minimized.

Copy link
Member

rafaelfranca replied Jun 11, 2016

Yes

This comment has been minimized.

Copy link

helperhaps replied Sep 27, 2016

@jmccartie Couse the default generate method of uuid is uuid_generate_v4 (ref:#20518) ,when use postgresql's pgcrypto extension, the migration will failed with function uuid_generate_v4() does not exist obviously. To fix that need to edit the edit phase by hands (add " default: 'gen_random_uuid()' by hands")

create_table :tables, id: :uuid, default: 'gen_random_uuid()' do |t|

So if use pgcrypto to generate uuid,

config.generators do |g|
    g.orm :active_record, primary_key_type: :uuid
end

this is not so good, why not make it like

config.generators do |g|
    g.orm :active_record, primary_key_type: :uuid, generate_method: 'gen_random_uuid()'
end

Or it can handle that but i did't find?

This comment has been minimized.

Copy link
Member

pixeltrix replied Sep 27, 2016

@helperhaps you'd be better off opening a ticket than commenting on an 12 month old commit. We should probably handle that in the PostgreSQL adapter rather than adding another config.

This comment has been minimized.

Copy link

helperhaps replied Sep 27, 2016

@pixeltrix Thanks for your comment

sonalkr132 added a commit to sonalkr132/rubygems.org that referenced this pull request Dec 20, 2018
`id: :serial` is added because `id: :uuid` support was introduced.
rails/rails#21762
`using: :btree` was redundant rails/rails#27981
Standardaized column spacing was removed to avoid diffs about
aligining columns.
rails/rails@df84e98#r22545295

```diff
-   t.string "zipcode", limit: 5
+   t.string "zipcode",           limit: 5
+   t.string "extra_information", limit: 255
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

You can’t perform that action at this time.