Skip to content
This repository
Browse code

add uuid primary key support

  • Loading branch information...
commit bc8ebefe9825dbff2cffa29ff015a1e7a31f9812 1 parent d25e407
Aaron Patterson authored March 22, 2013
5  activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -64,8 +64,9 @@ def columns; @columns_hash.values; end
64 64
 
65 65
       # Appends a primary key definition to the table definition.
66 66
       # Can be called multiple times, but this is probably not a good idea.
67  
-      def primary_key(name)
68  
-        column(name, :primary_key, primary_key: true)
  67
+      def primary_key(name, type = :primary_key, options = {})
  68
+        options[:primary_key] = true
  69
+        column(name, type, options)
69 70
       end
70 71
 
71 72
       # Returns a ColumnDefinition for the column with name +name+.
2  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -178,7 +178,7 @@ def create_table(table_name, options = {})
178 178
             Base.get_primary_key table_name.to_s.singularize
179 179
           }
180 180
 
181  
-          td.primary_key pk
  181
+          td.primary_key pk, options.fetch(:id, :primary_key), options
182 182
         end
183 183
 
184 184
         yield td if block_given?
9  activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -10,6 +10,15 @@ def visit_AddColumn(o)
10 10
           add_column_options!(sql, column_options(o))
11 11
         end
12 12
 
  13
+        def visit_ColumnDefinition(o)
  14
+          sql = super
  15
+          if o.primary_key? && o.type == :uuid
  16
+            sql << " PRIMARY KEY "
  17
+            add_column_options!(sql, column_options(o))
  18
+          end
  19
+          sql
  20
+        end
  21
+
13 22
         def add_column_options!(sql, options)
14 23
           if options[:array] || options[:column].try(:array)
15 24
             sql << '[]'
7  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -330,6 +330,13 @@ def json(name, options = {})
330 330
       class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
331 331
         include ColumnMethods
332 332
 
  333
+        def primary_key(name, type = :primary_key, options = {})
  334
+          return super unless type == :uuid
  335
+          options[:default] ||= 'uuid_generate_v4()'
  336
+          options[:primary_key] = true
  337
+          column name, type, options
  338
+        end
  339
+
333 340
         def column(name, type = nil, options = {})
334 341
           super
335 342
           column = self[name]
10  activerecord/test/cases/adapters/postgresql/uuid_test.rb
@@ -35,6 +35,16 @@ def teardown
35 35
     @connection.execute 'drop table if exists pg_uuids'
36 36
   end
37 37
 
  38
+  def test_id_is_uuid
  39
+    assert_equal :uuid, UUID.columns_hash['id'].type
  40
+    assert UUID.primary_key
  41
+  end
  42
+
  43
+  def test_id_has_a_default
  44
+    u = UUID.create
  45
+    assert_not_nil u.id
  46
+  end
  47
+
38 48
   def test_auto_create_uuid
39 49
     u = UUID.create
40 50
     u.reload

2 notes on commit bc8ebef

Please sign in to comment.
Something went wrong with that request. Please try again.