From 4757ce11a34bfc5064a6a38db41283f7da55986d Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 8 Jul 2024 12:35:29 +0200 Subject: [PATCH 1/4] refactor: refs #7567 Fix and improvement --- db/routines/srt/events/moving_clean.sql | 6 +- db/routines/srt/procedures/moving_clean.sql | 84 ++++++++++--------- .../11141-azureRoebelini/00-firstScript.sql | 1 + 3 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 db/versions/11141-azureRoebelini/00-firstScript.sql diff --git a/db/routines/srt/events/moving_clean.sql b/db/routines/srt/events/moving_clean.sql index a6f7792a2..650c15c62 100644 --- a/db/routines/srt/events/moving_clean.sql +++ b/db/routines/srt/events/moving_clean.sql @@ -5,9 +5,5 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `srt`.`moving_clean` ON COMPLETION PRESERVE ENABLE COMMENT 'Llama a srt.moving_clean para que elimine y notifique de registr' -DO BEGIN - - CALL srt.moving_clean(); - -END$$ +DO CALL srt.moving_clean()$$ DELIMITER ; diff --git a/db/routines/srt/procedures/moving_clean.sql b/db/routines/srt/procedures/moving_clean.sql index b8fae7ff4..446ad3588 100644 --- a/db/routines/srt/procedures/moving_clean.sql +++ b/db/routines/srt/procedures/moving_clean.sql @@ -3,61 +3,69 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `srt`.`moving_clean`() BEGIN /** * Elimina movimientos por inactividad - * */ DECLARE vExpeditionFk INT; DECLARE vBufferToFk INT; DECLARE vBufferFromFk INT; - DECLARE done BOOL DEFAULT FALSE; - - DECLARE cur CURSOR FOR + DECLARE vDone BOOL DEFAULT FALSE; + DECLARE vSorter CURSOR FOR SELECT m.expeditionFk, m.bufferToFk, m.bufferFromFk - FROM srt.moving m - JOIN srt.config c - JOIN (SELECT bufferFk, SUM(isActive) hasBox - FROM srt.photocell - GROUP BY bufferFk) sub ON sub.bufferFk = m.bufferFromFk - WHERE m.created < TIMESTAMPADD(MINUTE, - c.movingMaxLife , util.VN_NOW()) + FROM moving m + JOIN ( + SELECT bufferFk, SUM(isActive) hasBox + FROM photocell + GROUP BY bufferFk + ) sub ON sub.bufferFk = m.bufferFromFk + WHERE m.created < (util.VN_NOW() - INTERVAL (SELECT movingMaxLife FROM config) MINUTE) AND NOT sub.hasBox; - DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; - OPEN cur; + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + ROLLBACK; + RESIGNAL; + END; - bucle: LOOP + OPEN vSorter; + l: LOOP + SET vDone = FALSE; + FETCH vSorter INTO vExpeditionFk, vBufferToFk, vBufferFromFk; - FETCH cur INTO vExpeditionFk, vBufferToFk, vBufferFromFk; - - IF done THEN - LEAVE bucle; + IF vDone THEN + LEAVE l; END IF; - DELETE FROM srt.moving + START TRANSACTION; + + SELECT id + FROM moving + WHERE expeditionFk = vExpeditionFk + FOR UPDATE; + + DELETE FROM moving WHERE expeditionFk = vExpeditionFk; - UPDATE srt.expedition e - JOIN srt.expeditionState es ON es.description = 'OUT' - SET - bufferFk = NULL, + SELECT id + FROM expedition + WHERE id = vExpeditionFk + OR (bufferFk = vBufferFromFk AND `position` > 0) + FOR UPDATE; + + UPDATE expedition + SET bufferFk = NULL, `position` = NULL, - stateFk = es.id - WHERE e.id = vExpeditionFk; + stateFk = (SELECT id FROM expeditionState WHERE `description` = 'OUT') + WHERE id = vExpeditionFk; - UPDATE srt.expedition e - SET e.`position` = e.`position` - 1 - WHERE e.bufferFk = vBufferFromFk - AND e.`position` > 0; + UPDATE expedition + SET `position` = `position` - 1 + WHERE bufferFk = vBufferFromFk + AND `position` > 0; - CALL vn.mail_insert( - 'pako@verdnatura.es, carles@verdnatura.es', - NULL, - CONCAT('Moving_clean. Expedition: ', vExpeditionFk, ' estaba parada'), - CONCAT('Expedition: ', vExpeditionFk,' vBufferToFk: ', vBufferToFk) - ); - - END LOOP bucle; - - CLOSE cur; + COMMIT; + END LOOP l; + CLOSE vSorter; END$$ DELIMITER ; diff --git a/db/versions/11141-azureRoebelini/00-firstScript.sql b/db/versions/11141-azureRoebelini/00-firstScript.sql new file mode 100644 index 000000000..fd6d79cfb --- /dev/null +++ b/db/versions/11141-azureRoebelini/00-firstScript.sql @@ -0,0 +1 @@ +ALTER TABLE srt.moving DROP INDEX moving_fk1_idx; From 625b3a5608b03537bc1eec42bcfccfea857abd14 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 18 Jul 2024 14:12:37 +0200 Subject: [PATCH 2/4] refactor: refs #7567 Minor change --- db/routines/srt/procedures/moving_clean.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/db/routines/srt/procedures/moving_clean.sql b/db/routines/srt/procedures/moving_clean.sql index 446ad3588..ab16aac99 100644 --- a/db/routines/srt/procedures/moving_clean.sql +++ b/db/routines/srt/procedures/moving_clean.sql @@ -7,6 +7,8 @@ BEGIN DECLARE vExpeditionFk INT; DECLARE vBufferToFk INT; DECLARE vBufferFromFk INT; + DECLARE vStateOutFk INT + DEFAULT (SELECT id FROM expeditionState WHERE `description` = 'OUT'); DECLARE vDone BOOL DEFAULT FALSE; DECLARE vSorter CURSOR FOR SELECT m.expeditionFk, m.bufferToFk, m.bufferFromFk @@ -52,10 +54,10 @@ BEGIN OR (bufferFk = vBufferFromFk AND `position` > 0) FOR UPDATE; - UPDATE expedition + UPDATE expedition SET bufferFk = NULL, `position` = NULL, - stateFk = (SELECT id FROM expeditionState WHERE `description` = 'OUT') + stateFk = vStateOutFk WHERE id = vExpeditionFk; UPDATE expedition @@ -64,7 +66,6 @@ BEGIN AND `position` > 0; COMMIT; - END LOOP l; CLOSE vSorter; END$$ From 62176b8517c83481070681b6b6953bb223262a79 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 21 Aug 2024 09:35:42 +0200 Subject: [PATCH 3/4] feat: refs #7567 Changed time to call event --- db/routines/srt/events/moving_clean.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/routines/srt/events/moving_clean.sql b/db/routines/srt/events/moving_clean.sql index 650c15c62..18644a9f8 100644 --- a/db/routines/srt/events/moving_clean.sql +++ b/db/routines/srt/events/moving_clean.sql @@ -1,6 +1,6 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `srt`.`moving_clean` - ON SCHEDULE EVERY 5 MINUTE + ON SCHEDULE EVERY 15 MINUTE STARTS '2022-01-21 00:00:00.000' ON COMPLETION PRESERVE ENABLE From 582fa5faebe80873c8db956b82d693342738a256 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 21 Aug 2024 14:42:03 +0200 Subject: [PATCH 4/4] feat: refs #7567 Requested changes --- db/routines/srt/procedures/moving_clean.sql | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/db/routines/srt/procedures/moving_clean.sql b/db/routines/srt/procedures/moving_clean.sql index ab16aac99..a5bbc7e70 100644 --- a/db/routines/srt/procedures/moving_clean.sql +++ b/db/routines/srt/procedures/moving_clean.sql @@ -5,13 +5,12 @@ BEGIN * Elimina movimientos por inactividad */ DECLARE vExpeditionFk INT; - DECLARE vBufferToFk INT; DECLARE vBufferFromFk INT; DECLARE vStateOutFk INT DEFAULT (SELECT id FROM expeditionState WHERE `description` = 'OUT'); - DECLARE vDone BOOL DEFAULT FALSE; + DECLARE vDone BOOL; DECLARE vSorter CURSOR FOR - SELECT m.expeditionFk, m.bufferToFk, m.bufferFromFk + SELECT m.expeditionFk, m.bufferFromFk FROM moving m JOIN ( SELECT bufferFk, SUM(isActive) hasBox @@ -32,7 +31,7 @@ BEGIN OPEN vSorter; l: LOOP SET vDone = FALSE; - FETCH vSorter INTO vExpeditionFk, vBufferToFk, vBufferFromFk; + FETCH vSorter INTO vExpeditionFk, vBufferFromFk; IF vDone THEN LEAVE l;