diff options
8 files changed, 33 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb index ac14740cfe..d3e172927d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb @@ -25,7 +25,7 @@ module ActiveRecord spec[:precision] = column.precision.inspect if column.precision spec[:scale] = column.scale.inspect if column.scale spec[:null] = 'false' unless column.null - spec[:default] = default_string(column.default) if column.has_default? + spec[:default] = column.type_cast_for_schema(column.default) if column.has_default? spec end @@ -33,31 +33,6 @@ module ActiveRecord def migration_keys [:name, :limit, :precision, :scale, :default, :null] end - - private - - def default_string(value) - case value - when BigDecimal - value.to_s - when Date, DateTime, Time - "'#{value.to_s(:db)}'" - when Range - # infinity dumps as Infinity, which causes uninitialized constant error - value.inspect.gsub('Infinity', '::Float::INFINITY') - when IPAddr - subnet_mask = value.instance_variable_get(:@mask_addr) - - # If the subnet mask is equal to /32, don't output it - if subnet_mask == (2**32 - 1) - "\"#{value.to_s}\"" - else - "\"#{value.to_s}/#{subnet_mask.to_s(2).count('1')}\"" - end - else - value.inspect - end - end end end end diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index f66e99c9d1..4fbc55a07e 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -18,6 +18,7 @@ module ActiveRecord delegate :type, :precision, :scale, :limit, :klass, :accessor, :text?, :number?, :binary?, :serialized?, :type_cast, :type_cast_for_write, :raw_type_cast_for_write, :type_cast_for_database, + :type_cast_for_schema, to: :cast_type # Instantiates a new column in the table. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb index 507c3a62b0..158468fe5b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb @@ -7,6 +7,17 @@ module ActiveRecord :cidr end + def type_cast_for_schema(value) + subnet_mask = value.instance_variable_get(:@mask_addr) + + # If the subnet mask is equal to /32, don't output it + if subnet_mask == (2**32 - 1) + "\"#{value.to_s}\"" + else + "\"#{value.to_s}/#{subnet_mask.to_s(2).count('1')}\"" + end + end + def cast_value(value) ConnectionAdapters::PostgreSQLColumn.string_to_cidr value end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb index c2262c1599..a0d8a94c74 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb @@ -24,6 +24,10 @@ module ActiveRecord value.respond_to?(:infinite?) && value.infinite? end + def type_cast_for_schema(value) + value.inspect.gsub('Infinity', '::Float::INFINITY') + end + def type_cast_single(value) infinity?(value) ? value : @subtype.type_cast(value) end diff --git a/activerecord/lib/active_record/type/date.rb b/activerecord/lib/active_record/type/date.rb index 45c69460ef..d90a6069b7 100644 --- a/activerecord/lib/active_record/type/date.rb +++ b/activerecord/lib/active_record/type/date.rb @@ -9,6 +9,10 @@ module ActiveRecord ::Date end + def type_cast_for_schema(value) + "'#{value.to_s(:db)}'" + end + private def cast_value(value) diff --git a/activerecord/lib/active_record/type/decimal.rb b/activerecord/lib/active_record/type/decimal.rb index 1c0147a797..6eed005345 100644 --- a/activerecord/lib/active_record/type/decimal.rb +++ b/activerecord/lib/active_record/type/decimal.rb @@ -11,6 +11,10 @@ module ActiveRecord ::BigDecimal end + def type_cast_for_schema(value) + value.to_s + end + private def cast_value(value) diff --git a/activerecord/lib/active_record/type/time_value.rb b/activerecord/lib/active_record/type/time_value.rb index 6cc19b6379..d611d72dd4 100644 --- a/activerecord/lib/active_record/type/time_value.rb +++ b/activerecord/lib/active_record/type/time_value.rb @@ -5,6 +5,10 @@ module ActiveRecord ::Time end + def type_cast_for_schema(value) + "'#{value.to_s(:db)}'" + end + private def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil) diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb index 9a4adc60cc..9c1e9dc01e 100644 --- a/activerecord/lib/active_record/type/value.rb +++ b/activerecord/lib/active_record/type/value.rb @@ -27,6 +27,10 @@ module ActiveRecord type_cast_for_write(value) end + def type_cast_for_schema(value) + value.inspect + end + def text? false end |