Skip to content

Commit

Permalink
pg, support default values for enum types. Closes #7814.
Browse files Browse the repository at this point in the history
This is an intermediate solution. It is related to the refactoring @sgrif
is making and will change in the future.
  • Loading branch information
senny committed May 30, 2014
1 parent d6c1205 commit b9eeb03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* PostgreSQL support default values for enum types. Fixes #7814.

*Yves Senn*

* PostgreSQL `default_sequence_name` respects schema. Fixes #7516.

*Yves Senn*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def columns(table_name)
# 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def extract_limit(sql_type) # :nodoc:
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,
Expand All @@ -507,6 +507,13 @@ def extract_value_from_default(default) # :nodoc:
# 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
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/adapters/postgresql/enum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def test_column
assert_not column.array
end

def test_enum_defaults
@connection.add_column 'postgresql_enums', 'good_mood', :mood, default: 'happy'
PostgresqlEnum.reset_column_information
column = PostgresqlEnum.columns_hash["good_mood"]

assert_equal "happy", column.default
assert_equal "happy", PostgresqlEnum.new.good_mood
ensure
PostgresqlEnum.reset_column_information
end

def test_enum_mapping
@connection.execute "INSERT INTO postgresql_enums VALUES (1, 'sad');"
enum = PostgresqlEnum.first
Expand Down

0 comments on commit b9eeb03

Please sign in to comment.