feat: refs #7562 Added deprecate funcion

This commit is contained in:
Guillermo Bonet 2024-09-04 14:50:27 +02:00
parent dfa9570ea4
commit 912719cc08
5 changed files with 77 additions and 9 deletions

View File

@ -10,7 +10,7 @@ class Clean extends Command {
static usage = { static usage = {
description: 'Cleans old applied versions', description: 'Cleans old applied versions',
params: { 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', description: 'Dumps structure and fixtures from remote',
params: { params: {
lock: 'Whether to lock tables on dump', lock: 'Whether to lock tables on dump',
triggers: 'Wether to include triggers into dump' triggers: 'Whether to include triggers into dump'
}, },
operand: 'remote' operand: 'remote'
}; };

View File

@ -15,9 +15,9 @@ class Push extends Command {
description: 'Apply changes into database', description: 'Apply changes into database',
params: { params: {
force: 'Answer yes to all questions', 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', 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' operand: 'remote'
}; };

View File

@ -9,18 +9,23 @@ class Version extends Command {
static usage = { static usage = {
description: 'Creates a new version', description: 'Creates a new version',
params: { params: {
name: 'Name for the new version' name: 'Name for the new version',
deprecate: 'Whether to generate sql to delete deprecated objects'
}, },
operand: 'name' operand: 'name'
}; };
static opts = { static opts = {
alias: { alias: {
name: 'n' name: 'n',
deprecate: 'kk'
}, },
string: [ string: [
'name' 'name'
], ],
boolean: [
'deprecate'
],
default: { default: {
remote: 'production' remote: 'production'
} }
@ -36,7 +41,8 @@ class Version extends Command {
}, },
versionCreated: function(versionName) { versionCreated: function(versionName) {
console.log(`New version created: ${versionName}`); console.log(`New version created: ${versionName}`);
} },
deprecate: 'Generating sql.'
}; };
async run(myt, opts) { async run(myt, opts) {
@ -123,7 +129,9 @@ class Version extends Command {
await fs.mkdir(newVersionDir); await fs.mkdir(newVersionDir);
await fs.writeFile( await fs.writeFile(
`${newVersionDir}/00-firstScript.sql`, `${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); this.emit('versionCreated', versionFolder);
@ -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(`
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() { function randomName() {
const color = random(colors); const color = random(colors);
let plant = random(plants); let plant = random(plants);

2
myt.js
View File

@ -19,7 +19,7 @@ class Myt {
params: { params: {
remote: 'Name of remote to use', remote: 'Name of remote to use',
workspace: 'The base directory of the project', 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', version: 'Display the version number and exit',
help: 'Display this help message' help: 'Display this help message'
} }