Re: Queueing CTE

2022-10-07 Thread Jeremy Evans
On Fri, Oct 7, 2022 at 7:54 AM Bougy Man (bougyman)  wrote:

> I was trying to accomplish this on datasets and could not figure out the
> magic. Using Model.with(:enqueued, Model.where(status:
> 'ready').returning.update(status: 'processing')) does not work as expected
> in this case.
>
> Here's what I'm trying to replicate:
>
> ```with updated_notifications as (
> update notification_events
> set notification_event_status='PROCESSING'
> where id IN (
> select id from notification_events e
> where notification_event_status = 'ENQUEUED'
> order by created_at FOR UPDATE SKIP LOCKED LIMIT 10)
> RETURNING *)
> select * from updated_notifications order by created_at```


This should work:

subselect_ds = DB[:notification_events].
  select(:id).
  where(notification_event_status: 'ENQUEUED').
  order(:created_at).
  for_update.
  skip_locked.
  limit(10)

update_ds = DB[:notification_events].
  where(id: subselect_ds).
  returning.
  with_sql(:update_sql, notification_event_status: 'PROCESSING')

DB[:updated_notifications].
  with(:updated_notifications, update_ds).
  order(:created_at)

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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSchvKP1L4tMfxsjyx%3DL45vNmHjQexnQFjySu8HJyX3KZA%40mail.gmail.com.


Queueing CTE

2022-10-07 Thread Bougy Man (bougyman)
I was trying to accomplish this on datasets and could not figure out the 
magic. Using Model.with(:enqueued, Model.where(status: 
'ready').returning.update(status: 'processing')) does not work as expected 
in this case.

Here's what I'm trying to replicate:

```with updated_notifications as (
update notification_events
set notification_event_status='PROCESSING'
where id IN (
select id from notification_events e
where notification_event_status = 'ENQUEUED'
order by created_at FOR UPDATE SKIP LOCKED LIMIT 10)
RETURNING *)
select * from updated_notifications order by created_at```

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/7899bcee-8394-44e9-8bc6-37b1552031cdn%40googlegroups.com.