feat: refs #7562 deleteDeprecatedObjects #6

Merged
guillermo merged 7 commits from 7562-deleteDeprecatedObjects into master 2024-09-12 08:06:15 +00:00
5 changed files with 77 additions and 9 deletions
Showing only changes of commit 912719cc08 - Show all commits

View File

@ -10,7 +10,7 @@ class Clean extends Command {
static usage = {
description: 'Cleans old applied versions',
params: {
purge: 'Wether to remove non-existent scripts from DB log'
purge: 'Whether to remove non-existent scripts from DB log'
}
};

View File

@ -9,7 +9,7 @@ class Dump extends Command {
description: 'Dumps structure and fixtures from remote',
params: {
lock: 'Whether to lock tables on dump',
triggers: 'Wether to include triggers into dump'
triggers: 'Whether to include triggers into dump'
},
operand: 'remote'
};

View File

@ -15,9 +15,9 @@ class Push extends Command {
description: 'Apply changes into database',
params: {
force: 'Answer yes to all questions',
commit: 'Wether to save the commit SHA into database',
commit: 'Whether to save the commit SHA into database',
sums: 'Save SHA sums of pushed objects',
triggers: 'Wether to exclude triggers, used to generate local DB'
triggers: 'Whether to exclude triggers, used to generate local DB'
},
operand: 'remote'
};

View File

@ -9,18 +9,23 @@ class Version extends Command {
static usage = {
description: 'Creates a new version',
params: {
name: 'Name for the new version'
name: 'Name for the new version',
deprecate: 'Whether to generate sql to delete deprecated objects'
},
operand: 'name'
};
static opts = {
alias: {
name: 'n'
name: 'n',
deprecate: 'kk'
guillermo marked this conversation as resolved
Review

Los alias solo tienen que tener una letra y hacer referencia a una palabra inglesa, ej: -p

Los alias solo tienen que tener una letra y hacer referencia a una palabra inglesa, ej: `-p`
},
string: [
'name'
],
boolean: [
'deprecate'
],
default: {
remote: 'production'
}
@ -36,7 +41,8 @@ class Version extends Command {
},
versionCreated: function(versionName) {
console.log(`New version created: ${versionName}`);
}
},
deprecate: 'Generating sql.'
};
guillermo marked this conversation as resolved Outdated
Outdated
Review

Generating SQL for deprecated objects deletion

_Generating SQL for deprecated objects deletion_
async run(myt, opts) {
@ -123,7 +129,9 @@ class Version extends Command {
await fs.mkdir(newVersionDir);
await fs.writeFile(
`${newVersionDir}/00-firstScript.sql`,
'-- Place your SQL code here\n'
opts.deprecate
? this.emit('deprecate') && await deprecate()
: '-- Place your SQL code here\n'
);
this.emit('versionCreated', versionFolder);
guillermo marked this conversation as resolved
Review

else y } deben estar en la misma linea: } else

`else` y `}` deben estar en la misma linea: `} else`
@ -137,6 +145,66 @@ class Version extends Command {
}
}
async function deprecate() {
const [[config]] = await conn.query(`
SELECT dateRegex,
deprecatedMarkRegex,
VN_CURDATE() - INTERVAL daysKeepDeprecatedObjects DAY dated
FROM config
`);
const sql = await conn.query(`
guillermo marked this conversation as resolved Outdated
Outdated
Review

Usar el fichero de configuración de Myt para estos parámetros

Usar el fichero de configuración de Myt para estos parámetros
WITH variables AS (
SELECT ? markRegex, ? dateRegex, ? dated
)
SELECT CONCAT('ALTER TABLE ', c.TABLE_SCHEMA, '.', c.TABLE_NAME, ' DROP PRIMARY KEY;') sql
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
JOIN variables var
WHERE c.COLUMN_NAME REGEXP var.markRegex COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, var.dateRegex COLLATE utf8mb4_unicode_ci) < var.dated
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
JOIN variables var
WHERE c.COLUMN_NAME REGEXP var.markRegex COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, var.dateRegex COLLATE utf8mb4_unicode_ci) < var.dated
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
JOIN variables var
WHERE c.COLUMN_NAME REGEXP var.markRegex COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, var.dateRegex COLLATE utf8mb4_unicode_ci) < var.dated
AND v.TABLE_NAME IS NULL
UNION
SELECT CONCAT('DROP TABLE ', t.TABLE_SCHEMA, '.', t.TABLE_NAME, ';')
FROM information_schema.TABLES t
JOIN variables var
WHERE t.TABLE_NAME REGEXP var.markRegex COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(t.TABLE_COMMENT, var.dateRegex COLLATE utf8mb4_unicode_ci) < var.dated
`, [config.deprecatedMarkRegex, config.dateRegex, config.dated]);
return sql.map(row => row.sql).join('\n');
}
function randomName() {
guillermo marked this conversation as resolved Outdated
Outdated
Review
  • Generar el SQL con Javascript en lugar de usar CONCATs SQL
  • Escapar identificadores (nombres de esquemas, tablas, columnas...)
  • Hacer comprobación estricta del patron @deprecated YYYY-MM-DD
- Generar el SQL con Javascript en lugar de usar CONCATs SQL - Escapar identificadores (nombres de esquemas, tablas, columnas...) - Hacer comprobación **estricta** del patron `@deprecated YYYY-MM-DD`
const color = random(colors);
let plant = random(plants);

2
myt.js
View File

@ -19,7 +19,7 @@ class Myt {
params: {
remote: 'Name of remote to use',
workspace: 'The base directory of the project',
debug: 'Wether to enable debug mode',
debug: 'Whether to enable debug mode',
version: 'Display the version number and exit',
help: 'Display this help message'
}