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
Conflicts:
	activerecord/test/cases/adapters/postgresql/array_test.rb
  • Loading branch information
rafaelfranca committed Mar 30, 2014
1 parent c40bfc6 commit 2340c74
Show file tree
Hide file tree
Showing 5 changed files with 37 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. * Make possible to change `record_timestamps` inside Callbacks.


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


def add_column_options!(sql, options) 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 # must explicitly check for :null to allow change_column to work on migrations
if options[:null] == false if options[:null] == false
sql << " NOT NULL" sql << " NOT NULL"
Expand All @@ -75,6 +75,12 @@ def add_column_options!(sql, options)
sql sql
end 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) def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?) options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end 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 # are typically created by methods in TableDefinition, and added to the
# +columns+ attribute of said TableDefinition object, in order to be used # +columns+ attribute of said TableDefinition object, in order to be used
# for generating a number of table creation or table changing SQL statements. # 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? def primary_key?
primary_key || type.to_sym == :primary_key primary_key || type.to_sym == :primary_key
Expand Down
Expand Up @@ -17,7 +17,7 @@ def visit_ChangeColumnDefinition(o)
options = o.options options = o.options
sql_type = type_to_sql(o.type, options[:limit], options[:precision], options[:scale]) 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}" 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) add_column_position!(change_column_sql, options)
end end


Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/adapters/postgresql/array_test.rb
Expand Up @@ -34,6 +34,28 @@ def test_column
assert_not ratings_column.number? assert_not ratings_column.number?
end end


def test_default
@connection.add_column 'pg_arrays', 'score', :integer, array: true, default: [4, 4, 2]
PgArray.reset_column_information
column = PgArray.columns_hash["score"]

assert_equal([4, 4, 2], column.default)
assert_equal([4, 4, 2], PgArray.new.score)
ensure
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 def test_change_column_with_array
@connection.add_column :pg_arrays, :snippets, :string, array: true, default: [] @connection.add_column :pg_arrays, :snippets, :string, array: true, default: []
@connection.change_column :pg_arrays, :snippets, :text, array: true, default: "{}" @connection.change_column :pg_arrays, :snippets, :text, array: true, default: "{}"
Expand Down

0 comments on commit 2340c74

Please sign in to comment.