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

Projects
None yet
@jmccartie
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

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot 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.

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

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Sep 25, 2015

Member

This has falling tests

Member

sgrif commented Sep 25, 2015

This has falling tests

@jmccartie

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Sep 25, 2015

Contributor

@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)

Contributor

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)

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Oct 20, 2015

Member

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

Member

sgrif commented Oct 20, 2015

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

@jmccartie

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Oct 20, 2015

Contributor

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

Contributor

jmccartie commented Oct 20, 2015

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

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Oct 20, 2015

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Oct 20, 2015

Contributor

@sgrif All checks passed. Merge conflict. :)

Contributor

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

continuous-integration/travis-ci/pr The Travis CI build passed
Details

sgrif added a commit that referenced this pull request Oct 20, 2015

Merge pull request #21762 from jmccartie/jm/uuid
Set active_record config for always creating uuids in generators
@vincentopensourcetaiwan

This comment has been minimized.

Show comment
Hide comment
@vincentopensourcetaiwan

vincentopensourcetaiwan commented on fb42c49 Oct 21, 2015

Great!

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Oct 21, 2015

Contributor

Thanks @vincentopensourcetaiwan! I hope it helps you!

Contributor

jmccartie replied Oct 21, 2015

Thanks @vincentopensourcetaiwan! I hope it helps you!

This comment has been minimized.

Show comment
Hide comment
@samnang

samnang replied Oct 22, 2015

Nice job! @jmccartie

This comment has been minimized.

Show comment
Hide comment
@rizidoro

rizidoro replied Oct 22, 2015

👏

This comment has been minimized.

Show comment
Hide comment
@emaiax

emaiax Oct 23, 2015

incredible! 👏

emaiax replied Oct 23, 2015

incredible! 👏

This comment has been minimized.

Show comment
Hide comment
@jpwynn

jpwynn Jun 10, 2016

Rail 5 only, is that correct?

jpwynn replied Jun 10, 2016

Rail 5 only, is that correct?

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca
Member

rafaelfranca replied Jun 11, 2016

Yes

This comment has been minimized.

Show comment
Hide comment
@helperhaps

helperhaps 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?

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.

Show comment
Hide comment
@pixeltrix

pixeltrix Sep 27, 2016

Member

@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.

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.

Show comment
Hide comment
@helperhaps

helperhaps Sep 27, 2016

@pixeltrix Thanks for your comment

helperhaps replied Sep 27, 2016

@pixeltrix Thanks for your comment

@samnang

This comment has been minimized.

Show comment
Hide comment
@samnang

samnang Oct 22, 2015

@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.

@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.

Show comment
Hide comment
@jmccartie

jmccartie Oct 22, 2015

Contributor

@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.

Contributor

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.

Show comment
Hide comment
@samnang

samnang Oct 22, 2015

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

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.

Show comment
Hide comment
@pixeltrix

pixeltrix Oct 22, 2015

Member

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

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.

Show comment
Hide comment
@jmccartie

jmccartie Oct 22, 2015

Contributor

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

Contributor

jmccartie replied Oct 22, 2015

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

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Oct 22, 2015

Contributor

@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
Contributor

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.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Oct 22, 2015

Member

That test that should be fixed.

Member

rafaelfranca replied Oct 22, 2015

That test that should be fixed.

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Oct 22, 2015

Contributor

Opened #22033

Contributor

jmccartie replied Oct 22, 2015

Opened #22033

@vincentopensourcetaiwan

This comment has been minimized.

Show comment
Hide comment
@vincentopensourcetaiwan

vincentopensourcetaiwan commented on fb42c49 Oct 21, 2015

Great!

This comment has been minimized.

Show comment
Hide comment
@jmccartie

jmccartie Oct 21, 2015

Contributor

Thanks @vincentopensourcetaiwan! I hope it helps you!

Contributor

jmccartie replied Oct 21, 2015

Thanks @vincentopensourcetaiwan! I hope it helps you!

This comment has been minimized.

Show comment
Hide comment
@samnang

samnang replied Oct 22, 2015

Nice job! @jmccartie

This comment has been minimized.

Show comment
Hide comment
@rizidoro

rizidoro replied Oct 22, 2015

👏

This comment has been minimized.

Show comment
Hide comment
@emaiax

emaiax Oct 23, 2015

incredible! 👏

emaiax replied Oct 23, 2015

incredible! 👏

This comment has been minimized.

Show comment
Hide comment
@jpwynn

jpwynn Jun 10, 2016

Rail 5 only, is that correct?

jpwynn replied Jun 10, 2016

Rail 5 only, is that correct?

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca
Member

rafaelfranca replied Jun 11, 2016

Yes

This comment has been minimized.

Show comment
Hide comment
@helperhaps

helperhaps 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?

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.

Show comment
Hide comment
@pixeltrix

pixeltrix Sep 27, 2016

Member

@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.

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.

Show comment
Hide comment
@helperhaps

helperhaps Sep 27, 2016

@pixeltrix Thanks for your comment

helperhaps replied Sep 27, 2016

@pixeltrix Thanks for your comment

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