aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-03-14 18:21:00 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-03-14 18:21:00 -0700
commitd7587943711e96e0335c40452d77869a479c1722 (patch)
treed74e7472c20062ffaf49f08ad6ffe97d63b53823 /activerecord
parentcf065777d0a57f5975b2a6d1657ae8e492b855dc (diff)
downloadrails-d7587943711e96e0335c40452d77869a479c1722.tar.gz
rails-d7587943711e96e0335c40452d77869a479c1722.tar.bz2
rails-d7587943711e96e0335c40452d77869a479c1722.zip
you can provide uuid_generate_v4 as the default value for uuid columns
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb3
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb8
-rw-r--r--activerecord/test/cases/adapters/postgresql/uuid_test.rb43
3 files changed, 52 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 805832f01e..21dda17f2f 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -29,6 +29,7 @@ module ActiveRecord
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 @@ module ActiveRecord
private
def add_column_options!(sql, options)
- base.add_column_options!(sql, options.merge(:column => self))
+ base.add_column_options!(sql, options)
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index eb541b0215..dfa4c3967a 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -632,7 +632,13 @@ module ActiveRecord
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
diff --git a/activerecord/test/cases/adapters/postgresql/uuid_test.rb b/activerecord/test/cases/adapters/postgresql/uuid_test.rb
new file mode 100644
index 0000000000..53002c5265
--- /dev/null
+++ b/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