Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

you can provide uuid_generate_v4 as the default value for uuid columns

  • Loading branch information...
commit d7587943711e96e0335c40452d77869a479c1722 1 parent cf06577
@tenderlove tenderlove authored
View
3  activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -29,6 +29,7 @@ def to_sql
column_options = {}
column_options[:null] = null unless null.nil?
column_options[:default] = default unless default.nil?
+ column_options[:column] = self
add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
column_sql
end
@@ -36,7 +37,7 @@ def to_sql
private
def add_column_options!(sql, options)
- base.add_column_options!(sql, options.merge(:column => self))
+ base.add_column_options!(sql, options)
end
end
View
8 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -632,7 +632,13 @@ def add_column_options!(sql, options)
if options[:array] || options[:column].try(:array)
sql << '[]'
end
- super
+
+ column = options.fetch(:column) { return super }
+ if column.type == :uuid && options[:default] =~ /\(\)/
+ sql << " DEFAULT #{options[:default]}"
+ else
+ super
+ end
end
# Set the authorized user for this session
View
43 activerecord/test/cases/adapters/postgresql/uuid_test.rb
@@ -0,0 +1,43 @@
+# encoding: utf-8
+
+require "cases/helper"
+require 'active_record/base'
+require 'active_record/connection_adapters/postgresql_adapter'
+
+class PostgresqlUUIDTest < ActiveRecord::TestCase
+ class UUID < ActiveRecord::Base
+ self.table_name = 'pg_uuids'
+ end
+
+ def setup
+ @connection = ActiveRecord::Base.connection
+
+ unless @connection.supports_extensions?
+ return skip "do not test on PG without uuid-ossp"
+ end
+
+ unless @connection.extension_enabled?('uuid-ossp')
+ @connection.enable_extension 'uuid-ossp'
+ @connection.commit_db_transaction
+ end
+
+ @connection.reconnect!
+
+ @connection.transaction do
+ @connection.create_table('pg_uuids', id: :uuid) do |t|
+ t.string 'name'
+ t.uuid 'other_uuid', default: 'uuid_generate_v4()'
+ end
+ end
+ end
+
+ def teardown
+ @connection.execute 'drop table if exists pg_uuids'
+ end
+
+ def test_auto_create_uuid
+ u = UUID.create
+ u.reload
+ assert_not_nil u.other_uuid
+ end
+end

1 comment on commit d758794

@mattetti

:+1: thank you!

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