Skip to content
Browse files

Merge pull request #14509 from lukesteensen/pg-array-defaults

remove calls to sql_type on pg columns
Conflicts:
	activerecord/test/cases/adapters/postgresql/array_test.rb
  • Loading branch information...
1 parent c40bfc6 commit 2340c74d5f04a37c022da60677258f337288ba36 @rafaelfranca rafaelfranca committed Mar 30, 2014
View
6 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*
View
8 activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
@@ -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"
@@ -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
View
2 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -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
View
2 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -17,7 +17,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
View
22 activerecord/test/cases/adapters/postgresql/array_test.rb
@@ -34,6 +34,28 @@ def test_column
assert_not ratings_column.number?
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
@connection.add_column :pg_arrays, :snippets, :string, array: true, default: []
@connection.change_column :pg_arrays, :snippets, :text, array: true, default: "{}"

0 comments on commit 2340c74

Please sign in to comment.
Something went wrong with that request. Please try again.