rails/rails — pull request #16919 by Envek (2014-09-14 20:49:53+00:00)
Title: PostgreSQL interval datatype support

Add native support for PostgreSQL `interval` datatype with conversion from all PostgreSQL formats to `ActiveSupport::Duration` on record load and transformation from `ActiveSupport::Duration` to ISO 8601 on save (no more strings). Also added support to define column in migrations and get it in schema dump, optional column precision is supported.

- Parses all 4 formats in that result may be present (see [Output formats])
- Constructs `ActiveSupport::Duration` value (that magic `2.days`)
- Converts `ActiveSupport::Duration` to [ISO 8601 duration] format on save
- Allows to use old raw values on save — just pass the string in attribute writer
- Allows to use `Numeric` values, treating them as number of seconds

Example:

create_table :events do |t|
t.string :name
t.interval :duration
end

Event.create!(name: 'Rock Fest', duration: 2.days)
Event.last.duration # => 2 days
Event.last.duration.iso8601 # => "P2D"

Event.create!(duration: '2 mons 1 day') # Raw values still works

I've proposed this in Rails-core Google Group here: https://groups.google.com/forum/#!topic/rubyonrails-core/zcscu1p4ucA

**ATTENTION**: This PR is built on top of #16917 and require it to be merged first. As it's not merged for now, commits and changes from it appears here, sorry for that hurry — I just want it to be included in upcoming 4.2 release, if it still possible (pleeeaase!). As #16917 will be progressed, I will rebase this branch on top of it (or anything else, what core team will say).

[ISO 8601 duration]: http://en.wikipedia.org/wiki/ISO_8601#Durations
[Output formats]: http://www.postgresql.org/docs/9.4/static/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT