diff --git a/app/migrations/202611271400_print-file-version.sql b/app/migrations/202611271400_print-file-version.sql index decb818..18d3f64 100644 --- a/app/migrations/202611271400_print-file-version.sql +++ b/app/migrations/202611271400_print-file-version.sql @@ -7,13 +7,18 @@ ALTER TABLE dtf_local.print_files ADD COLUMN IF NOT EXISTS generation integer NO -- file's first upload), so the other parts never reached the folders (#114, -- #128, #130). For the orders still before Imprimindo, the missing parts join -- the parts the Prumo already approved. -INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) -SELECT gen_random_uuid(), p.order_id, p.upload_id, p.item_index, 'final', 'Parte completada (prova)', 'Prumo' -FROM dtf_local.print_parts p JOIN dtf_local.orders o ON o.id=p.order_id -WHERE o.state IN ('rec','tra') AND o.cancelled_at IS NULL AND o.parent_id IS NULL - AND EXISTS (SELECT 1 FROM dtf_local.order_files f JOIN dtf_local.print_parts q - ON q.order_id=f.order_id AND q.item_index=f.item_index AND q.upload_id=f.upload_id - WHERE f.order_id=p.order_id AND f.item_index=p.item_index AND f.kind='final' AND f.active - AND f.created_by='Prumo') - AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=p.order_id - AND f.item_index=p.item_index AND f.upload_id=p.upload_id); +-- The order's version goes up, so the board and the folders see the new finals. +WITH added AS ( + INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) + SELECT gen_random_uuid(), p.order_id, p.upload_id, p.item_index, 'final', 'Parte completada (prova)', 'Prumo' + FROM dtf_local.print_parts p JOIN dtf_local.orders o ON o.id=p.order_id + WHERE o.state IN ('rec','tra') AND o.cancelled_at IS NULL AND o.parent_id IS NULL + AND EXISTS (SELECT 1 FROM dtf_local.order_files f JOIN dtf_local.print_parts q + ON q.order_id=f.order_id AND q.item_index=f.item_index AND q.upload_id=f.upload_id + WHERE f.order_id=p.order_id AND f.item_index=p.item_index AND f.kind='final' AND f.active + AND f.created_by='Prumo') + AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=p.order_id + AND f.item_index=p.item_index AND f.upload_id=p.upload_id) + RETURNING order_id +) +UPDATE dtf_local.orders SET version=version+1, updated_at=now() WHERE id IN (SELECT order_id FROM added);