thx, that is helpful. I can see myself implementing a potential plugin 
implementing the aforementioned increment/decrement methods, with or 
without the timestamp updates.

However, in order for these to be implemented at the instance level, the 
value has to be updated as well, as after running the code on an instance, 
calling the ".counter" method will return an expression, not the value (for 
that, I have to refresh the model). Is there a more convenient way to do 
this?

domingo, 26 de Janeiro de 2020 às 16:25:19 UTC, Jeremy Evans escreveu:
>
> On Sunday, January 26, 2020 at 4:48:26 AM UTC-8, Tiago Cardoso wrote:
>>
>> Hi there,
>>
>> I've been struggling to find examples on how to do atomic 
>> increment/decrement operations on a column, which may or may not update the 
>> "updated_at" timestamps.
>>
>> Specifically, something like the AR#increment and AR#decrement methods, 
>> which result in smith like this:
>>
>> "UPDATE this_table SET counter = COALESCE(counter, 0) + 1"
>>
>
> A basic translation of the query:
>
>   DB[:this_table].update(:counter=>Sequel.expr{COALESCE(:counter,0)+1}) 
>
> In general, true updated_at timestamps should be implemented via triggers, 
> in which case you shouldn't need more than the above to also update that 
> column.  However, if you don't want to use triggers:
>
>   DB[:this_table].update(:counter=>Sequel.expr{COALESCE(:counter,0)+1}, 
> :updated_at=>Sequel::CURRENT_TIMESTAMP)
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/3de87ed9-a673-466b-be66-74e2d68c8c9e%40googlegroups.com.

Reply via email to