Custom UUID generator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Custom UUID generator

johnpearson555
Hi, I was reading the docs for primary key on postgresql  and I saw that there was a way to add a custom stored procedure that returns a UUID. There is a test case as well that uses a custom uuid generator https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193 but I wasn't sure how to to implement this myself. 

Like in the test case where do I place 'my_uuid_generator()' and how to implement it in my rails application?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/06bd9a3c-eafa-496b-a611-2d35906517a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom UUID generator

Matt Hickman
If you look my_uuid_generator() is added to postgres at https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L186

You can add a stored procedure in a similar fashion in a migration.

--
Matt Hickman

On Sun, Jul 9, 2017 at 6:56 PM, <[hidden email]> wrote:
Hi, I was reading the docs for primary key on postgresql  and I saw that there was a way to add a custom stored procedure that returns a UUID. There is a test case as well that uses a custom uuid generator https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193 but I wasn't sure how to to implement this myself. 

Like in the test case where do I place 'my_uuid_generator()' and how to implement it in my rails application?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/06bd9a3c-eafa-496b-a611-2d35906517a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CALzpcTjCrkzdjNgYFv498m7WZ7v8gSQineoMS_gSuWOuw9cqhA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom UUID generator

johnpearson555
That's what I thought but I wasn't sure where to place the uuid_function that the migration refers to. So that go in a helper file or part of the migration?

On Monday, July 10, 2017 at 4:38:06 AM UTC-7, Matt Hickman wrote:
If you look my_uuid_generator() is added to postgres at <a href="https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L186" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F650ea5e5cf50d8a7242499463cf1762922d330a8%2Factiverecord%2Ftest%2Fcases%2Fadapters%2Fpostgresql%2Fuuid_test.rb%23L186\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEOdEjY6UvLLdaNncZt8MkWOzXSVA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F650ea5e5cf50d8a7242499463cf1762922d330a8%2Factiverecord%2Ftest%2Fcases%2Fadapters%2Fpostgresql%2Fuuid_test.rb%23L186\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEOdEjY6UvLLdaNncZt8MkWOzXSVA&#39;;return true;">https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L186

You can add a stored procedure in a similar fashion in a migration.

--
Matt Hickman

On Sun, Jul 9, 2017 at 6:56 PM, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3DMMUoMzCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">johnpea...@...> wrote:
Hi, I was reading the docs for <a href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQL/ColumnMethods.html#method-i-primary_key" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FConnectionAdapters%2FPostgreSQL%2FColumnMethods.html%23method-i-primary_key\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFXInh0DCzEOkeXo913uMklX_RPsg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FConnectionAdapters%2FPostgreSQL%2FColumnMethods.html%23method-i-primary_key\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFXInh0DCzEOkeXo913uMklX_RPsg&#39;;return true;">primary key on postgresql  and I saw that there was a way to add a custom stored procedure that returns a UUID. There is a test case as well that uses a custom uuid generator <a href="https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F650ea5e5cf50d8a7242499463cf1762922d330a8%2Factiverecord%2Ftest%2Fcases%2Fadapters%2Fpostgresql%2Fuuid_test.rb%23L193\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGu_W-KAMM1WQTYRFfYjUZQWwYC-g&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F650ea5e5cf50d8a7242499463cf1762922d330a8%2Factiverecord%2Ftest%2Fcases%2Fadapters%2Fpostgresql%2Fuuid_test.rb%23L193\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGu_W-KAMM1WQTYRFfYjUZQWwYC-g&#39;;return true;">https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193 but I wasn't sure how to to implement this myself. 

Like in the test case where do I place 'my_uuid_generator()' and how to implement it in my rails application?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="3DMMUoMzCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="3DMMUoMzCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/06bd9a3c-eafa-496b-a611-2d35906517a3%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/06bd9a3c-eafa-496b-a611-2d35906517a3%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/06bd9a3c-eafa-496b-a611-2d35906517a3%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/06bd9a3c-eafa-496b-a611-2d35906517a3%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ea4c0dbb-317d-428a-9534-80ca70b1e2f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom UUID generator

Matt Hickman
uuid_function is defined - https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L14 - it just switches on if postgres supports gen_random_uuid() otherwise it uses uuid_generate_v4()

gen_random_uuid() is part of the pgcrypto module - https://www.postgresql.org/docs/9.5/static/pgcrypto.html

uuid_generate_v4() is part of the uuid-ossp module - https://www.postgresql.org/docs/9.5/static/uuid-ossp.html
July 10, 2017 at 12:39 PM via Postbox
That's what I thought but I wasn't sure where to place the uuid_function that the migration refers to. So that go in a helper file or part of the migration?

On Monday, July 10, 2017 at 4:38:06 AM UTC-7, Matt Hickman wrote:
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ea4c0dbb-317d-428a-9534-80ca70b1e2f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5963ED2A.8060808%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom UUID generator

johnpearson555
My question, if this is my migration:

migration.rb
def change

connection.execute <<-SQL
CREATE OR REPLACE FUNCTION john_uuid_generator() RETURNS uuid
AS $$ SELECT * FROM #{uuid_function} $$
LANGUAGE SQL VOLATILE;
SQL

end


Where do I define john_uuid_generator()?

Thanks!


On Mon, Jul 10, 2017 at 2:10 PM, Matt Hickman <[hidden email]> wrote:
uuid_function is defined - https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L14 - it just switches on if postgres supports gen_random_uuid() otherwise it uses uuid_generate_v4()

gen_random_uuid() is part of the pgcrypto module - https://www.postgresql.org/docs/9.5/static/pgcrypto.html

uuid_generate_v4() is part of the uuid-ossp module - https://www.postgresql.org/docs/9.5/static/uuid-ossp.html
July 10, 2017 at 12:39 PM via Postbox
That's what I thought but I wasn't sure where to place the uuid_function that the migration refers to. So that go in a helper file or part of the migration?

On Monday, July 10, 2017 at 4:38:06 AM UTC-7, Matt Hickman wrote:
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ea4c0dbb-317d-428a-9534-80ca70b1e2f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/5Y-7-LLFKGw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5963ED2A.8060808%40gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKNtY_x1qm_VXqcUdMXQ8pytHQF2tFuLAmgnv%2Bvf-8uZGXBMqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom UUID generator

Matt Hickman
That statement you pasted defines it, it's stored as a function in postgres. See https://www.postgresql.org/docs/9.5/static/xfunc-sql.html for docs on how this works in postgres.

July 10, 2017 at 2:36 PM via Postbox
My question, if this is my migration:

migration.rb
def change

connection.execute <<-SQL
CREATE OR REPLACE FUNCTION john_uuid_generator() RETURNS uuid
AS $$ SELECT * FROM #{uuid_function} $$
LANGUAGE SQL VOLATILE;
SQL

end


Where do I define john_uuid_generator()?

Thanks!



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKNtY_x1qm_VXqcUdMXQ8pytHQF2tFuLAmgnv%2Bvf-8uZGXBMqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5963FD32.8070208%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom UUID generator

gorav
In reply to this post by johnpearson555
You can also do following with pg:

class FirstSetOfTables < ActiveRecord::Migration[5.1]
  enable_extension
'pgcrypto' unless extensions.include?('pgcrypto')
 
  create_table
:tokens, id: :uuid do |t|
    t
.integer    :action,               null: false, default: 1
    t
.integer    :status,               null: false, default: 1
    t
.datetime   :expires_at
    t
.datetime   :confirmed_at
    t
.timestamps                        null: false
 
end
end




On Monday, July 10, 2017 at 9:56:56 AM UTC+5:30, [hidden email] wrote:
Hi, I was reading the docs for <a href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQL/ColumnMethods.html#method-i-primary_key" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FConnectionAdapters%2FPostgreSQL%2FColumnMethods.html%23method-i-primary_key\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFXInh0DCzEOkeXo913uMklX_RPsg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveRecord%2FConnectionAdapters%2FPostgreSQL%2FColumnMethods.html%23method-i-primary_key\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFXInh0DCzEOkeXo913uMklX_RPsg&#39;;return true;">primary key on postgresql  and I saw that there was a way to add a custom stored procedure that returns a UUID. There is a test case as well that uses a custom uuid generator <a href="https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F650ea5e5cf50d8a7242499463cf1762922d330a8%2Factiverecord%2Ftest%2Fcases%2Fadapters%2Fpostgresql%2Fuuid_test.rb%23L193\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGu_W-KAMM1WQTYRFfYjUZQWwYC-g&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F650ea5e5cf50d8a7242499463cf1762922d330a8%2Factiverecord%2Ftest%2Fcases%2Fadapters%2Fpostgresql%2Fuuid_test.rb%23L193\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGu_W-KAMM1WQTYRFfYjUZQWwYC-g&#39;;return true;">https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193 but I wasn't sure how to to implement this myself. 

Like in the test case where do I place 'my_uuid_generator()' and how to implement it in my rails application?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/24a91b19-01d5-4c97-bab3-a9c031e557a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...