Skip to content
Browse files

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

remove calls to sql_type on pg columns
Conflicts:
	activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
	activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
	activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
	activerecord/test/cases/adapters/postgresql/array_test.rb
  • Loading branch information...
1 parent 6fc7708 commit 1dc609ce715ba2d3bf6439307321c2b471acfe08 @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
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 string_to_binary(value)
value
end
View
6 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -869,6 +869,12 @@ def rename_column_indexes(table_name, column_name, new_column_name)
end
end
+ def quote_value(value, column)
+ column.sql_type ||= type_to_sql(column.type, column.limit, column.precision, column.scale)
+
+ quote(value, column)
+ end
+
private
def create_table_definition(name, temporary, options)
TableDefinition.new native_database_types, name, temporary, options
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 1dc609c

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