salix/db/routines/vn/procedures/clean.sql

226 lines
6.8 KiB
MySQL
Raw Normal View History

2023-01-27 06:56:51 +00:00
DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`clean`()
2023-01-27 06:56:51 +00:00
BEGIN
/**
* Purges outdated data to optimize performance.
* Exercise caution when executing.
*/
DECLARE v2Months DATE DEFAULT util.VN_CURDATE() - INTERVAL 2 MONTH;
DECLARE v3Months DATE DEFAULT util.VN_CURDATE() - INTERVAL 3 MONTH;
DECLARE v18Months DATE DEFAULT util.VN_CURDATE() - INTERVAL 18 MONTH;
DECLARE v26Months DATE DEFAULT util.VN_CURDATE() - INTERVAL 26 MONTH;
DECLARE v1Years DATE DEFAULT util.VN_CURDATE() - INTERVAL 1 YEAR;
DECLARE v2Years DATE DEFAULT util.VN_CURDATE() - INTERVAL 2 YEAR;
DECLARE v4Years DATE DEFAULT util.VN_CURDATE() - INTERVAL 4 YEAR;
2024-03-27 07:12:05 +00:00
DECLARE v5Years DATE DEFAULT util.VN_CURDATE() - INTERVAL 5 YEAR;
2023-01-27 06:56:51 +00:00
DECLARE vTrashId VARCHAR(15);
DECLARE vCompanyBlk INT;
2023-01-27 06:56:51 +00:00
2024-02-21 11:09:52 +00:00
DELETE FROM workerActivity WHERE created < v2Years;
DELETE FROM ticketParking WHERE created < v2Months;
DELETE FROM routesMonitor WHERE dated < v2Months;
DELETE FROM `message` WHERE sendDate < v2Months;
DELETE FROM messageInbox WHERE sendDate < v2Months;
DELETE FROM messageInbox WHERE sendDate < v2Months;
DELETE FROM workerTimeControl WHERE timed < v4Years;
2023-01-27 06:56:51 +00:00
DELETE FROM itemShelving WHERE created < util.VN_CURDATE() AND visible = 0;
DELETE FROM ticketDown WHERE created < util.yesterday();
DELETE IGNORE FROM expedition WHERE created < v26Months;
2024-07-15 12:55:11 +00:00
DELETE cs
2024-07-15 10:02:00 +00:00
FROM sms s
JOIN clientSms cs ON cs.smsFk = s.id
2024-07-15 12:25:42 +00:00
WHERE s.created < v18Months;
DELETE FROM sms WHERE created < v18Months;
DELETE FROM saleTracking WHERE created < v1Years;
2024-03-27 07:12:05 +00:00
DELETE FROM productionError WHERE dated < v1Years;
DELETE FROM ticketTracking WHERE created < v18Months;
2024-03-27 07:12:05 +00:00
DELETE tobs FROM ticketObservation tobs
2024-02-26 08:50:03 +00:00
JOIN ticket t ON tobs.ticketFk = t.id
WHERE t.shipped < v5Years;
DELETE sc.* FROM saleCloned sc JOIN sale s ON s.id = sc.saleClonedFk JOIN ticket t ON t.id = s.ticketFk WHERE t.shipped < v1Years;
DELETE FROM sharingCart where ended < v2Months;
DELETE FROM sharingClient where ended < v2Months;
2023-01-27 06:56:51 +00:00
DELETE tw.* FROM ticketWeekly tw
LEFT JOIN sale s ON s.ticketFk = tw.ticketFk
LEFT JOIN ticketRequest tr ON tr.ticketFk = tw.ticketFk
LEFT JOIN ticketService ts ON ts.ticketFk = tw.ticketFk
WHERE s.id IS NULL
AND tr.id IS NULL
AND ts.id IS NULL;
DELETE FROM claim WHERE ticketCreated < v4Years;
-- Robert ubicacion anterior de travelLog comentario para debug
DELETE FROM zoneEvent WHERE `type` = 'day' AND dated < v3Months;
2023-01-27 06:56:51 +00:00
DELETE bm
FROM buyMark bm
JOIN buy b ON b.id = bm.id
JOIN entry e ON e.id = b.entryFk
JOIN travel t ON t.id = e.travelFk
WHERE t.landed <= v2Months;
DELETE b FROM buy b
JOIN entryConfig e ON e.defaultEntry = b.entryFk
WHERE b.created < v2Months;
DELETE FROM stockBuyed WHERE creationDate < v2Months;
DELETE FROM printQueue WHERE statusCode = 'printed' AND created < v2Months;
2023-01-27 06:56:51 +00:00
-- Equipos duplicados
DELETE w.*
FROM workerTeam w
JOIN (
SELECT id, team, workerFk, COUNT(*) - 1 duplicated
2023-01-27 06:56:51 +00:00
FROM workerTeam
GROUP BY team,workerFk
HAVING duplicated
) d ON d.team = w.team
AND d.workerFk = w.workerFk
AND d.id <> w.id;
2023-01-27 06:56:51 +00:00
DELETE sc
FROM saleComponent sc
JOIN sale s ON s.id= sc.saleFk
JOIN ticket t ON t.id= s.ticketFk
WHERE t.shipped < v18Months;
2023-01-27 06:56:51 +00:00
DELETE c
FROM claim c
JOIN claimState cs ON cs.id = c.claimStateFk
2024-02-29 06:32:19 +00:00
WHERE cs.description = 'Anulado'
AND c.created < v2Months;
DELETE FROM roadmapStop WHERE eta < v3Months;
DELETE FROM XDiario WHERE FECHA < v3Months OR FECHA IS NULL;
-- Borrar travels sin entradas
CREATE OR REPLACE TEMPORARY TABLE tThermographToDelete
2023-01-27 06:56:51 +00:00
SELECT th.id,th.dmsFk
FROM travel t
LEFT JOIN entry e ON e.travelFk = t.id
JOIN travelThermograph th ON th.travelFk = t.id
2024-02-29 06:32:19 +00:00
WHERE t.shipped < v3Months
AND e.travelFk IS NULL;
2023-01-27 06:56:51 +00:00
SELECT dt.id INTO vTrashId
FROM dmsType dt
2023-01-27 06:56:51 +00:00
WHERE dt.code = 'trash';
UPDATE tThermographToDelete th
2024-02-29 06:32:19 +00:00
JOIN dms d ON d.id = th.dmsFk
2023-01-27 06:56:51 +00:00
SET d.dmsTypeFk = vTrashId;
DELETE th
FROM tThermographToDelete tmp
2024-02-29 06:32:19 +00:00
JOIN travelThermograph th ON th.id = tmp.id;
2023-01-27 06:56:51 +00:00
DELETE t
FROM travel t
2024-02-29 06:32:19 +00:00
LEFT JOIN entry e ON e.travelFk = t.id
WHERE t.shipped < v3Months AND e.travelFk IS NULL;
2024-03-27 07:12:05 +00:00
2023-01-27 06:56:51 +00:00
UPDATE dms d
2024-03-27 07:12:05 +00:00
JOIN dmsType dt ON dt.id = d.dmsTypeFk
SET d.dmsTypeFk = vTrashId
2024-02-29 06:32:19 +00:00
WHERE created < util.VN_CURDATE() - INTERVAL dt.monthToDelete MONTH;
2024-03-27 07:12:05 +00:00
2023-01-27 06:56:51 +00:00
-- borrar entradas sin compras
CREATE OR REPLACE TEMPORARY TABLE tEntryToDelete
2023-01-27 06:56:51 +00:00
SELECT e.*
FROM entry e
2024-02-29 06:32:19 +00:00
LEFT JOIN buy b ON b.entryFk = e.id
JOIN entryConfig ec ON e.id <> ec.defaultEntry
WHERE e.dated < v3Months
AND b.entryFK IS NULL;
2023-01-27 06:56:51 +00:00
DELETE e
FROM entry e
JOIN tEntryToDelete tmp ON tmp.id = e.id
2024-05-02 11:33:25 +00:00
WHERE NOT e.isBooked;
2023-01-27 06:56:51 +00:00
-- borrar de route registros menores a 4 años
CREATE OR REPLACE TEMPORARY TABLE tRouteToDelete
2023-01-27 06:56:51 +00:00
SELECT *
2024-02-29 06:32:19 +00:00
FROM route r
2024-03-27 07:12:05 +00:00
WHERE created < v4Years;
2023-01-27 06:56:51 +00:00
UPDATE tRouteToDelete tmp
2024-02-29 06:32:19 +00:00
JOIN dms d ON d.id = tmp.gestdocFk
2023-01-27 06:56:51 +00:00
SET d.dmsTypeFk = vTrashId;
DELETE r
FROM tRouteToDelete tmp
2024-02-29 06:32:19 +00:00
JOIN route r ON r.id = tmp.id;
2023-01-27 06:56:51 +00:00
-- borrar registros de dua y awb menores a 2 años
CREATE OR REPLACE TEMPORARY TABLE tDuaToDelete
2023-01-27 06:56:51 +00:00
SELECT *
FROM dua
2024-02-29 06:32:19 +00:00
WHERE operated < v2Years;
2023-01-27 06:56:51 +00:00
2024-02-29 06:32:19 +00:00
UPDATE tDuaToDelete tm
JOIN dms d ON d.id = tm.gestdocFk
2023-01-27 06:56:51 +00:00
SET d.dmsTypeFk = vTrashId;
DELETE d
FROM tDuaToDelete tmp
2024-02-29 06:32:19 +00:00
JOIN dua d ON d.id = tmp.id;
2023-01-27 06:56:51 +00:00
2024-02-15 09:04:15 +00:00
DELETE a
FROM awb a
LEFT JOIN travel t ON t.awbFk = a.id
2024-02-15 09:04:15 +00:00
WHERE a.created < v2Years
AND t.id IS NULL;
2023-01-27 06:56:51 +00:00
-- Borra los registros de collection y ticketcollection
2024-02-29 06:32:19 +00:00
DELETE FROM collection WHERE created < v2Months;
2023-01-27 06:56:51 +00:00
CALL shelving_clean();
2023-01-27 06:56:51 +00:00
DELETE FROM chat WHERE dated < v5Years;
DELETE tt FROM ticketTracking tt
2024-02-29 06:32:19 +00:00
JOIN ticket t ON tt.ticketFk = t.id
WHERE t.shipped <= v2Months;
DELETE FROM mail WHERE creationDate < v2Months;
DELETE FROM split WHERE dated < v18Months;
DELETE FROM remittance WHERE dated < v18Months;
2024-03-27 07:12:05 +00:00
CREATE OR REPLACE TEMPORARY TABLE tTicketDelete
SELECT DISTINCT tl.originFk ticketFk
FROM ticketLog tl
JOIN (
SELECT MAX(tl.id)ids
FROM ticket t
JOIN ticketLog tl ON tl.originFk = t.id
2024-03-11 11:21:54 +00:00
LEFT JOIN ticketWeekly tw ON tw.ticketFk = t.id
2024-03-27 07:12:05 +00:00
WHERE t.shipped BETWEEN '2000-01-01' AND '2000-12-31'
AND t.isDeleted
2024-03-11 11:21:54 +00:00
AND tw.ticketFk IS NULL
GROUP BY t.id
2024-03-27 07:12:05 +00:00
) sub ON sub.ids = tl.id
2024-03-11 11:21:54 +00:00
WHERE tl.creationDate <= v2Months;
DELETE t
FROM ticket t
JOIN tTicketDelete tmp ON tmp.ticketFk = t.id;
DELETE sl
FROM saleLabel sl
JOIN sale s ON s.id = sl.saleFk
JOIN ticket t ON t.id = s.ticketFk
WHERE t.shipped < v2Months;
-- Tickets Nulos PAK 11/10/2016
SELECT id INTO vCompanyBlk FROM company WHERE code = 'BLK';
UPDATE ticket
SET companyFk = vCompanyBlk
WHERE clientFk = (SELECT id FROM client WHERE name = 'AUTOCONSUMO')
AND companyFk <> vCompanyBlk;
DROP TEMPORARY TABLE tTicketDelete,
tThermographToDelete,
tEntryToDelete,
tDuaToDelete,
tRouteToDelete;
-- Other schemas
DELETE FROM hedera.`order` WHERE date_send < v18Months;
DELETE FROM pbx.cdr WHERE call_date < v18Months;
2023-01-27 06:56:51 +00:00
END$$
DELIMITER ;