feat: refs #7562 Created deleteDeprecatedObjects #2627

Merged
guillermo merged 10 commits from 7562-deleteDeprecatedObjects into dev 2024-09-12 08:08:17 +00:00
2 changed files with 0 additions and 102 deletions
Showing only changes of commit 2ba391fc85 - Show all commits

View File

@ -1,8 +0,0 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `util`.`deleteDeprecatedObjects`
ON SCHEDULE EVERY 1 DAY
STARTS '2024-06-09 00:01:00.000'
ON COMPLETION PRESERVE
ENABLE
DO CALL deleteDeprecatedObjects$$
DELIMITER ;

View File

@ -1,94 +0,0 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`deleteDeprecatedObjects`()
MODIFIES SQL DATA
BEGIN
/**
* Elimina objetos deprecados de la base de datos
*/
DECLARE vQuery TEXT;
DECLARE vDated DATE;
DECLARE vDateRegex VARCHAR(255);
DECLARE vMarkRegex VARCHAR(255);
DECLARE vDone BOOL;
DECLARE vObjects CURSOR FOR
SELECT CONCAT('ALTER TABLE ', c.TABLE_SCHEMA, '.', c.TABLE_NAME, ' DROP PRIMARY KEY;')
FROM information_schema.`COLUMNS` c
LEFT JOIN information_schema.`VIEWS` v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
AND v.TABLE_NAME = c.TABLE_NAME
JOIN information_schema.STATISTICS s ON s.TABLE_SCHEMA = c.TABLE_SCHEMA
AND s.TABLE_NAME = c.TABLE_NAME
AND s.COLUMN_NAME = c.COLUMN_NAME
WHERE c.COLUMN_NAME REGEXP vMarkRegex COLLATE utf8mb3_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, vDateRegex COLLATE utf8mb3_unicode_ci) < vDated
AND v.TABLE_NAME IS NULL
AND s.INDEX_NAME = 'PRIMARY'
UNION
SELECT CONCAT('ALTER TABLE ', c.TABLE_SCHEMA, '.', c.TABLE_NAME, ' DROP FOREIGN KEY ', kcu.CONSTRAINT_NAME, ';')
FROM information_schema.`COLUMNS` c
LEFT JOIN information_schema.`VIEWS` v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
AND v.TABLE_NAME = c.TABLE_NAME
JOIN information_schema.KEY_COLUMN_USAGE kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
AND kcu.TABLE_NAME = c.TABLE_NAME
AND kcu.COLUMN_NAME = c.COLUMN_NAME
WHERE c.COLUMN_NAME REGEXP vMarkRegex COLLATE utf8mb3_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, vDateRegex COLLATE utf8mb3_unicode_ci) < vDated
AND v.TABLE_NAME IS NULL
AND kcu.REFERENCED_COLUMN_NAME IS NOT NULL
UNION
SELECT CONCAT('ALTER TABLE ', c.TABLE_SCHEMA, '.', c.TABLE_NAME, ' DROP COLUMN ', c.COLUMN_NAME, ';')
FROM information_schema.`COLUMNS` c
LEFT JOIN information_schema.`VIEWS` v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
AND v.TABLE_NAME = c.TABLE_NAME
LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
AND kcu.TABLE_NAME = c.TABLE_NAME
AND kcu.COLUMN_NAME = c.COLUMN_NAME
WHERE c.COLUMN_NAME REGEXP vMarkRegex COLLATE utf8mb3_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, vDateRegex COLLATE utf8mb3_unicode_ci) < vDated
AND v.TABLE_NAME IS NULL
UNION
SELECT CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ';')
FROM information_schema.TABLES
WHERE TABLE_NAME REGEXP vMarkRegex COLLATE utf8mb3_unicode_ci
AND REGEXP_SUBSTR(TABLE_COMMENT, vDateRegex COLLATE utf8mb3_unicode_ci) < vDated;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
CALL vn.mail_insert(
'cau@verdnatura.es',
NULL,
'Error en la eliminación automática de objetos deprecados',
CONCAT('<pre>', vQuery, '</pre>',
'<p>Revisa la tabla <strong>util.eventLog</strong> para más detalles.</p>')
);
RESIGNAL;
END;
SELECT dateRegex,
deprecatedMarkRegex,
VN_CURDATE() - INTERVAL daysKeepDeprecatedObjects DAY
INTO vDateRegex,
vMarkRegex,
vDated
FROM config;
IF vDateRegex IS NULL OR vMarkRegex IS NULL OR vDated IS NULL THEN
CALL throw('Some config parameters are not set');
END IF;
OPEN vObjects;
l: LOOP
SET vDone = FALSE;
FETCH vObjects INTO vQuery;
IF vDone THEN
LEAVE l;
END IF;
CALL `exec`(vQuery);
END LOOP;
CLOSE vObjects;
END$$
DELIMITER ;