salix/db/routines/util/procedures/log_clean.sql

55 lines
1.3 KiB
MySQL
Raw Normal View History

2024-06-18 06:40:33 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`log_clean`()
BEGIN
/**
* Hace limpieza de los datos de las tablas log,
* dejando únicamente los días de retención configurados.
*/
DECLARE vSchemaName VARCHAR(65);
2024-07-19 07:29:15 +00:00
DECLARE vSchemaNameQuoted VARCHAR(65);
2024-06-18 06:40:33 +00:00
DECLARE vTableName VARCHAR(65);
2024-07-19 07:29:15 +00:00
DECLARE vTableNameQuoted VARCHAR(65);
2024-06-18 06:40:33 +00:00
DECLARE vRetentionDays INT;
2024-07-19 06:58:51 +00:00
DECLARE vStarted DATETIME;
2024-06-18 06:40:33 +00:00
DECLARE vDated DATE;
DECLARE vDone BOOL;
DECLARE vQueue CURSOR FOR
SELECT schemaName, tableName, retentionDays
FROM logCleanMultiConfig
2024-06-18 06:40:33 +00:00
ORDER BY `order`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
OPEN vQueue;
l: LOOP
SET vDone = FALSE;
FETCH vQueue INTO vSchemaName, vTableName, vRetentionDays;
2024-07-19 06:58:51 +00:00
IF vDone THEN
LEAVE l;
END IF;
2024-07-19 06:58:51 +00:00
IF vRetentionDays THEN
2024-07-19 07:29:42 +00:00
SET vStarted = VN_NOW();
SET vSchemaNameQuoted = quoteIdentifier(vSchemaName);
SET vTableNameQuoted = quoteIdentifier(vTableName);
SET vDated = VN_CURDATE() - INTERVAL vRetentionDays DAY;
2024-07-19 07:29:15 +00:00
EXECUTE IMMEDIATE CONCAT(
'DELETE FROM ', vSchemaNameQuoted,
'.', vTableNameQuoted,
" WHERE creationDate < '", vDated, "'"
2024-07-19 07:29:15 +00:00
);
UPDATE logCleanMultiConfig
2024-07-19 06:58:51 +00:00
SET `started` = vStarted,
`finished` = VN_NOW()
WHERE schemaName = vSchemaName
AND tableName = vTableName;
END IF;
2024-06-18 06:40:33 +00:00
END LOOP;
CLOSE vQueue;
END$$
DELIMITER ;