Skip to content

Commit

Permalink
Merge pull request #14509 from lukesteensen/pg-array-defaults
Browse files Browse the repository at this point in the history
remove calls to sql_type on pg columns
  • Loading branch information
rafaelfranca committed Mar 30, 2014
2 parents fd5739c + 5098bca commit 4d441e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Fixed error when specifying a non-empty default value on a PostgreSQL array column.

Fixes #10613.

*Luke Steensen*

* Make possible to change `record_timestamps` inside Callbacks.

*Tieg Zaharia*
Expand Down
Expand Up @@ -64,7 +64,7 @@ def type_to_sql(type, limit, precision, scale)
end

def add_column_options!(sql, options)
sql << " DEFAULT #{@conn.quote(options[:default], options[:column])}" if options_include_default?(options)
sql << " DEFAULT #{quote_value(options[:default], options[:column])}" if options_include_default?(options)
# must explicitly check for :null to allow change_column to work on migrations
if options[:null] == false
sql << " NOT NULL"
Expand All @@ -75,6 +75,12 @@ def add_column_options!(sql, options)
sql
end

def quote_value(value, column)
column.sql_type ||= type_to_sql(column.type, column.limit, column.precision, column.scale)

@conn.quote(value, column)
end

def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end
Expand Down
Expand Up @@ -15,7 +15,7 @@ class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :
# are typically created by methods in TableDefinition, and added to the
# +columns+ attribute of said TableDefinition object, in order to be used
# for generating a number of table creation or table changing SQL statements.
class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :primary_key) #:nodoc:
class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after, :primary_key, :sql_type) #:nodoc:

def primary_key?
primary_key || type.to_sym == :primary_key
Expand Down
Expand Up @@ -30,7 +30,7 @@ def visit_ChangeColumnDefinition(o)
options = o.options
sql_type = type_to_sql(o.type, options[:limit], options[:precision], options[:scale])
change_column_sql = "CHANGE #{quote_column_name(column.name)} #{quote_column_name(options[:name])} #{sql_type}"
add_column_options!(change_column_sql, options)
add_column_options!(change_column_sql, options.merge(column: column))
add_column_position!(change_column_sql, options)
end

Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/adapters/postgresql/array_test.rb
Expand Up @@ -48,6 +48,17 @@ def test_default
PgArray.reset_column_information
end

def test_default_strings
@connection.add_column 'pg_arrays', 'names', :string, array: true, default: ["foo", "bar"]
PgArray.reset_column_information
column = PgArray.columns_hash["names"]

assert_equal(["foo", "bar"], column.default)
assert_equal(["foo", "bar"], PgArray.new.names)
ensure
PgArray.reset_column_information
end

def test_change_column_with_array
@connection.add_column :pg_arrays, :snippets, :string, array: true, default: []
@connection.change_column :pg_arrays, :snippets, :text, array: true, default: "{}"
Expand Down

0 comments on commit 4d441e3

Please sign in to comment.