Repository navigation
Conversation
There was a problem hiding this comment.
Could extract this implementation (and the regexp constants) to a standalone PostgreSQLIntervalParser
There was a problem hiding this comment.
It was extracted into #16917 and merged a long, long time ago. For some reason this thread wasn't marked as stale.
There was a problem hiding this comment.
Guessing the interval output format every time is expensive.
How about checking intervalstyle to see what output to expect? http://www.postgresql.org/docs/9.4/static/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT
Or setting intervalstyle to iso8601 when we connect.
There was a problem hiding this comment.
Setting intervalstyle is much better idea, this can be achieved on connecting issuing command SET intervalstyle = iso_8601; (seems like it take effect on entire session until disconnect).
Where this command's issue should be placed?
|
Changed implementation to set output format to ISO 8601 on connect. Removed all stuff that became unnecessary. |
|
I wish this could make it into 4.2, I use interval columns regularly |
|
@ollym This PR isn't backward compatible and would not be merged in 4.x |
|
@Envek amazing work. Thanks! Right now I'm storing my interval columns as strings. What steps do you recommend for upgrading to using native interval column types using this patch? Also any chance you might turn it into a gem? |
|
@ollym if your interval columns are indeed have interval type in database your need only to insert this initializer in app, run But you will be faced with another problem: How to work with durations between HTML, JS and Rails app. ISO 8601 Durations support is still inconsistent and requires quite a lot of effort to organize interchange between backend and frontend. Currently, as I'm need short durations in my app (hours) I'm use usual |
|
@Envek what's wrong with the ISO8601 duration specification? That's the format I'm storing them in the database at the moment (as strings), and that's the format they're being sent to the API (my application). |
|
Everything is fine with specification, but implementation on browser side (js libraries) is still weak and inconsistent. And it's up to you to handle values coming from clients — string values are currently not parsed and passed to database as is: Event.new(duration: "PT5H10M") # => #<Event:0x007fcfe6358a78 id: nil, duration: "PT5H10M">
Event.new(duration: ActiveSupport::Duration.parse("PT5H10M")) # => #<Event:0x007fcfe6358a78 id: nil, duration: 5 hours and 10 minutes>If you know better approach to handle values from scary real world — please tell it here. |
|
@Envek I would type cast value from the user as well as from the database. I assume this will involve creating a new type here: And then raising an exception if the format is invalid. |
|
Implemented |
|
@Envek which gist did you update? Can't seem to get the 4.2 initializer (you posted earlier) working... |
There was a problem hiding this comment.
Unfortunately this is a breaking change, so we can't actually do this without a deprecation cycle. We should continue to treat it as a string and emit a warning, since w/ the attributes API you can now specify manually in the mean time.
There was a problem hiding this comment.
Can you point me on some example how to do that (deprecation warning and some kind of switch to enable this feature immediately)?
There was a problem hiding this comment.
Basically, do this:
m.register_type 'interval' do |*args, sql_type|
unless ActiveRecord::Base.real_interval_types
ActiveSupport::Deprecation.warn(<<-MSG.squish)
blah blah blah deprecated
MSG
m.lookup(varchar, *args)
else
# what you have now
end
end|
@voltechs I have tested initializer today in 4.2.4 app, it's working. Here it's current version: https://gist.githubusercontent.com/Envek/7077bfc36b17233f60ad/raw/25d12443c0fdf0dccf0f51275688c1e0264c55ec/pg_interval_support_4_2.rb If you still can't get it working, please contact me directly, as this conversation is for pull request related staff only. Thanks. |
|
@Envek are you still interested in working on this? It'll need some more work to be mergeable into master. |
|
@sgrif sure I still interested in all my PRs. Will add deprecation warning tomorrow. Please note that this PR is based on top of #16917, so it should be merged first (I have split ActiveSupport and ActiveRecord stuff on separate PRs). Could you please take a look on it? I will do my best to finish both these PRs. Let's merge them! 😄 |
|
This is really important for me too, have a load of hacks in our code base that's keeping this together. |
|
@Envek I highly recommend you check out the excellent work @jeremyevans is has done with the PG interval column within sequel here: It even contains a full PostgreSQL Interval parser. |
There was a problem hiding this comment.
Do we expect values other than a string or nil here? I'd rather not let those slip through. What do you think of changing to this instead?
def type_cast_from_database(value)
return if value.nil?
::ActiveSupport::Duration.parse(value)
end|
I’d like to get interval support into Rails. How can I help get this pull request ready to merge? |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
It would still be nice to get this merged (once conflicts are resolved). This is a fairly important outstanding issue, so I don't really want the bots to close it. |
|
We will need a volunteer to resolve the conflicts and push the PR to the finish line, otherwise in 3 months the bot will mark the PR as stale again and we'll have to close. |
|
@rafaelfranca Now that you’ve clarified that, perhaps we stand a better chance of having it happen. :) (I might give it a try, but I’m already overcommitted, so I may not get to it soon...) |
|
It seems as though we need some dedicated support/eyes from the internal rails core team. I'm sensing rebase fatigue from the contributors here. Feels a little tone-deaf to doom this PR to the dustbin simply because there hasn't been a lot of support from the merge guardians. Is anyone prepared to review/accept this PR if contributors step up? Nobody likes wasting other people's time, let alone their own. Thanks for stepping up, @marnen. I too am considering helping with this, but I'm also fairly overcommitted. I at least wanted to chime in and refresh my support (for what little it's worth) for this. |
|
@voltechs I agree. Note that I'm trying not to have to step up, first because I don't actually have time, and second because I'm putting a little more effort into crashtech/torque-postgresql#41 , which sort of covers the same ground. But I do need this functionality in my Rails applications, and so I will ultimately attempt to deal with this if no one with more time (and more skill hacking ActiveRecord!) comes forward to do it. |
|
I have refreshed this pull request. Can someone review it again? @rafaelfranca, or can you maybe recommend someone to ask for a review? Due to many merge conflicts, I had to squash all commits (that addressed previous reviews) into one, or it was seemed to be impossible to resolve all the conflicts. However, none of the conflicts required significant changes, so in reality, this pull request stayed mostly the same. I even restored some tests that I deleted earlier. And, sure, I bumped version numbers in deprecation warning and changelog. Thanks everyone for your support comments – they helped me not to forget about this change, and I returned to it as soon as there was a spare minute. |
|
Updated gist with initializers for adding support for interval data type into existing applications. Added initializer for both Rails 5.2 and 6.0: https://gist.github.com/Envek/7077bfc36b17233f60ad#file-pg_interval_support_6_0-rb |
|
Hey there, me and @h-lame were looking at using this and found that when using the gist example, we needed to add a little more code to support schema dumping for default values. We added: to |
|
@AgentAntelope, nice catch, thank you! Added method from you to the datatype along with a test case. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
Ongoing issue, should still be open. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
Let's get this in. This effort is old enough to be entering grade school. Thank you for your patience & persistence @Envek. |
Add support for PostgreSQL `interval` data type with conversion to
`ActiveSupport::Duration` when loading records from database and
serialization to ISO 8601 formatted duration string on save.
Add support to define a column in migrations and get it in a schema dump.
Optional column precision is supported.
To use this in 6.1, you need to place the next string to your model file:
attribute :duration, :interval
To keep old behavior until 6.2 is released:
attribute :duration, :string
This is fantastic! 🥳 Thank you! I rebased PR on top of current |
|
Merged @ 0475215 |
Add native support for PostgreSQL
intervaldatatype with conversion from all PostgreSQL formats toActiveSupport::Durationon record load and transformation fromActiveSupport::Durationto 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.ActiveSupport::Durationvalue (that magic2.days)ActiveSupport::Durationto ISO 8601 duration format on saveNumericvalues, treating them as number of seconds (as some calculations with Time objects returns not durations but floats)Example:
I've proposed this in Rails-core Google Group here: https://groups.google.com/forum/#!topic/rubyonrails-core/zcscu1p4ucA