aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-05-30 13:48:05 +0200
committerYves Senn <yves.senn@gmail.com>2014-05-30 13:48:05 +0200
commitb9eeb0339df7bd746273d680a26258df78dbd262 (patch)
tree14f7f0a2c23f4da975711173843e34bdf9c0bf8b /activerecord/lib/active_record/connection_adapters
parentd6c1205584b1ba597db4071b168681678b1e9875 (diff)
downloadrails-b9eeb0339df7bd746273d680a26258df78dbd262.tar.gz
rails-b9eeb0339df7bd746273d680a26258df78dbd262.tar.bz2
rails-b9eeb0339df7bd746273d680a26258df78dbd262.zip
pg, support default values for enum types. Closes #7814.
This is an intermediate solution. It is related to the refactoring @sgrif is making and will change in the future.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb9
2 files changed, 9 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
index 09db337a82..b2aeb3a058 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -179,7 +179,7 @@ module ActiveRecord
# Limit, precision, and scale are all handled by the superclass.
column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod|
oid = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
- default_value = extract_value_from_default(default)
+ default_value = extract_value_from_default(oid, default)
default_function = extract_default_function(default_value, default)
new_column(column_name, default_value, oid, type, notnull == 'f', default_function)
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 027169ae3c..23c1a8de2f 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -498,7 +498,7 @@ module ActiveRecord
end
# Extracts the value from a PostgreSQL column default definition.
- def extract_value_from_default(default) # :nodoc:
+ def extract_value_from_default(oid, default) # :nodoc:
# This is a performance optimization for Ruby 1.9.2 in development.
# If the value is nil, we return nil straight away without checking
# the regular expressions. If we check each regular expression,
@@ -507,6 +507,13 @@ module ActiveRecord
# makes this method very very slow.
return default unless default
+ # TODO: The default extraction is related to the cast-type.
+ # we should probably make a type_map lookup and cast the default-
+ # expression accordingly
+ if oid.type == :enum && default =~ /\A'(.*)'::/
+ return $1
+ end
+
case default
when /\A'(.*)'::(num|date|tstz|ts|int4|int8)range\z/m
$1