<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
        <title>labria'a code blog</title>
        <description>labria'a code blog - Dimitri Krassovski</description>
        <link>http://labria.github.io</link>
        <link>http://labria.github.io</link>
        <lastBuildDate>2013-04-27T16:45:11-07:00</lastBuildDate>
        <pubDate>2013-04-27T16:45:11-07:00</pubDate>
        <ttl>1800</ttl>


        <item>
                <title>Rails 4 Postgres UUID Primary Key Guide</title>
                <description>&lt;h2 id='basic_setup'&gt;Basic setup&lt;/h2&gt;

&lt;p&gt;As noted in my previous post, Rails 4 now natively supports UUID primary keys in PostgreSQL. To create a table with a UUID simply create a migration (even simpler than I posted, &lt;a href='http://twitter.com/tenderlove'&gt;Aaron Patterson&lt;/a&gt; pointed that I can just pass &lt;code&gt;id: :uuid&lt;/code&gt; to the create_table method):&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;create_table&lt;/span&gt; &lt;span class='ss'&gt;:users&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='nb'&gt;id&lt;/span&gt;&lt;span class='p'&gt;:&lt;/span&gt; &lt;span class='ss'&gt;:uuid&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;string&lt;/span&gt; &lt;span class='ss'&gt;:name&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;timestamps&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will execute the following SQL:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='sql'&gt;&lt;span class='k'&gt;CREATE&lt;/span&gt; &lt;span class='k'&gt;TABLE&lt;/span&gt; &lt;span class='ss'&gt;&amp;quot;users&amp;quot;&lt;/span&gt; &lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;&amp;quot;id&amp;quot;&lt;/span&gt; &lt;span class='n'&gt;uuid&lt;/span&gt; &lt;span class='k'&gt;PRIMARY&lt;/span&gt; &lt;span class='k'&gt;KEY&lt;/span&gt; &lt;span class='k'&gt;DEFAULT&lt;/span&gt; &lt;span class='n'&gt;uuid_generate_v4&lt;/span&gt;&lt;span class='p'&gt;(),&lt;/span&gt; &lt;span class='ss'&gt;&amp;quot;name&amp;quot;&lt;/span&gt; &lt;span class='nb'&gt;character&lt;/span&gt; &lt;span class='nb'&gt;varying&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='mi'&gt;255&lt;/span&gt;&lt;span class='p'&gt;),&lt;/span&gt; &lt;span class='ss'&gt;&amp;quot;created_at&amp;quot;&lt;/span&gt; &lt;span class='k'&gt;timestamp&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;&amp;quot;updated_at&amp;quot;&lt;/span&gt; &lt;span class='k'&gt;timestamp&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While this is good enough to get started, there are some quirks along the way that we&amp;#8217;ll need to overcome.&lt;/p&gt;

&lt;h2 id='sorting'&gt;Sorting&lt;/h2&gt;

&lt;p&gt;If you&amp;#8217;re not new to Rails, then you&amp;#8217;re used to the fact the calling &lt;code&gt;User.first&lt;/code&gt; will fetch the first User object ever created. This is due to the fact that the default sorting is by &lt;code&gt;id&lt;/code&gt;, which is usually a &lt;code&gt;AUTO_INCEREMENT&lt;/code&gt; integer column, so the first one will have the smallest id. When we switch to UUID for PK, this property will be lost, because the UUID is generated randomly (by default) and does not guarantee sequentiality. There are two ways to overcome this (if you &lt;em&gt;do&lt;/em&gt; need to overcome this):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a default scope to sort by &lt;code&gt;created_at&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Switch to a different (from the default) UUID generation algorithm&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While the first one is obvious, the second one needs some explanation. There are &lt;a href='https://en.wikipedia.org/wiki/UUID'&gt;5 methods for generating a UUID&lt;/a&gt;. The default version used in Rails is version 4, a completely random ID. We can switch to version 1, which is the combination of the machine&amp;#8217;s MAC address and a timestamp, to get sequential UUID stings. This algorithm is somewhat less secure, because it exposes the generator machine MAC address and creation time, but this is not a really big security threat if you ask me. To switch the UUID generation to this algorithm we need to modify our migration:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;create_table&lt;/span&gt; &lt;span class='ss'&gt;:users&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='nb'&gt;id&lt;/span&gt;&lt;span class='p'&gt;:&lt;/span&gt; &lt;span class='kp'&gt;false&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;primary_key&lt;/span&gt; &lt;span class='ss'&gt;:id&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:uuid&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:default&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;uuid_generate_v1()&amp;#39;&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;string&lt;/span&gt; &lt;span class='ss'&gt;:name&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;timestamps&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will force the generated UUIDs to be sequential and preserve the sorting we&amp;#8217;re used to. By the way, switching to sequential UUIDs also positively affects DB performance.&lt;/p&gt;

&lt;h2 id='relations'&gt;Relations&lt;/h2&gt;

&lt;p&gt;Since UUID primary keys are per-table and not a global setting (there is a plugin in the works to make it global), Rails has no way to tell if the primary key of a table is a integer or a UUID. Because of this, &lt;code&gt;t.references :user&lt;/code&gt; will still generate a integer &lt;code&gt;user_id&lt;/code&gt; field, which will obviously not work. The solution is simple:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;create_table&lt;/span&gt; &lt;span class='ss'&gt;:posts&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='nb'&gt;id&lt;/span&gt;&lt;span class='p'&gt;:&lt;/span&gt; &lt;span class='ss'&gt;:uuid&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;string&lt;/span&gt; &lt;span class='ss'&gt;:title&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;uuid&lt;/span&gt; &lt;span class='ss'&gt;:user_id&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;timestamps&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The same goes for polymorphic relations.&lt;/p&gt;

&lt;h2 id='schema_dumping'&gt;Schema dumping&lt;/h2&gt;

&lt;p&gt;As Aaron mentioned on HN, ruby schema dumping does not work as expected yet (I noticed that it does not dump the :default for the primary key), you&amp;#8217;ll have to use SQL dumping for now.&lt;/p&gt;

&lt;h2 id='enabling_the_extension'&gt;Enabling the extension&lt;/h2&gt;

&lt;p&gt;If your PostgreSQL user is a superuser, you can enable the extension right from a migration:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;enable_extension&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;uuid-ossp&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
                <link>http://labria.github.io/2013/04/28/rails-4-postgres-uuid-pk-guide</link>
                <guid>http://labria.github.io/2013/04/28/rails-4-postgres-uuid-pk-guide</guid>
                <pubDate>2013-04-28T00:00:00-07:00</pubDate>
        </item>

        <item>
                <title>Rails 4 UUID primary keys with PostgreSQL</title>
                <description>&lt;p&gt;UPD: See my second, more detailed &lt;a href='/2013/04/28/rails-4-postgres-uuid-pk-guide/'&gt;post&lt;/a&gt; about the matter.&lt;/p&gt;

&lt;p&gt;Aaron recently added &lt;a href='https://github.com/rails/rails/commit/bc8ebefe9825dbff2cffa29ff015a1e7a31f9812'&gt;uuid primary key support&lt;/a&gt; to Rails4 edge. To try it in your app simply create a migration like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;create_table&lt;/span&gt; &lt;span class='ss'&gt;:users&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='nb'&gt;id&lt;/span&gt;&lt;span class='p'&gt;:&lt;/span&gt; &lt;span class='kp'&gt;false&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;primary_key&lt;/span&gt; &lt;span class='ss'&gt;:id&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:uuid&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;string&lt;/span&gt; &lt;span class='ss'&gt;:name&lt;/span&gt;
  &lt;span class='n'&gt;t&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;timestamps&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Rails now automatically sets the default value to &lt;code&gt;uuid_generate_v4()&lt;/code&gt; for the primary_key.&lt;/p&gt;

&lt;p&gt;You&amp;#8217;ll of course need to run &lt;code&gt;CREATE EXTENSION &amp;quot;uuid-ossp&amp;quot;;&lt;/code&gt; in Postgres, either manually or with a migration via a &lt;code&gt;execute&lt;/code&gt; statement.&lt;/p&gt;</description>
                <link>http://labria.github.io/2013/04/27/rails-4-uuid-primary-keys-with-postgresql</link>
                <guid>http://labria.github.io/2013/04/27/rails-4-uuid-primary-keys-with-postgresql</guid>
                <pubDate>2013-04-27T00:00:00-07:00</pubDate>
        </item>


</channel>
</rss>
