Merge pull request #11382 from kennyj/fix_10751-2

Dump UUID default functions to schema.rb [2nd version]. Fixes #10751.

Conflicts:
	activerecord/CHANGELOG.md
This commit is contained in:
Rafael Mendonça França 2013-09-22 16:01:01 -03:00
commit afcca464db
3 changed files with 13 additions and 3 deletions

View File

@ -1,3 +1,9 @@
* Migration dump UUID default functions to schema.rb.
Fixes #10751.
*kennyj*
* Fixed a bug in `ActiveRecord::Associations::CollectionAssociation#find_by_scan`
when using `has_many` association with `:inverse_of` option and UUID primary key.

View File

@ -45,16 +45,18 @@ module ActiveRecord
module ConnectionAdapters
# PostgreSQL-specific extensions to column definitions in a table.
class PostgreSQLColumn < Column #:nodoc:
attr_accessor :array
attr_accessor :array, :default_function
# Instantiates a new PostgreSQL column definition in a table.
def initialize(name, default, oid_type, sql_type = nil, null = true)
@oid_type = oid_type
default_value = self.class.extract_value_from_default(default)
@default_function = default if !default_value && default && default =~ /.+\(.*\)/
if sql_type =~ /\[\]$/
@array = true
super(name, self.class.extract_value_from_default(default), sql_type[0..sql_type.length - 3], null)
super(name, default_value, sql_type[0..sql_type.length - 3], null)
else
@array = false
super(name, self.class.extract_value_from_default(default), sql_type, null)
super(name, default_value, sql_type, null)
end
end
@ -439,6 +441,7 @@ module ActiveRecord
def prepare_column_options(column, types)
spec = super
spec[:array] = 'true' if column.respond_to?(:array) && column.array
spec[:default] = "\"#{column.default_function}\"" if column.respond_to?(:default_function) && column.default_function
spec
end

View File

@ -61,6 +61,7 @@ class PostgresqlUUIDTest < ActiveRecord::TestCase
schema = StringIO.new
ActiveRecord::SchemaDumper.dump(@connection, schema)
assert_match(/\bcreate_table "pg_uuids", id: :uuid\b/, schema.string)
assert_match(/t\.uuid "other_uuid", default: "uuid_generate_v4\(\)"/, schema.string)
end
end