From 912719cc08de732f9e52c4132606282073a6c622 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 4 Sep 2024 14:50:27 +0200 Subject: [PATCH] feat: refs #7562 Added deprecate funcion --- myt-clean.js | 2 +- myt-dump.js | 2 +- myt-push.js | 4 +-- myt-version.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++--- myt.js | 2 +- 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/myt-clean.js b/myt-clean.js index db37150..a83c2ec 100644 --- a/myt-clean.js +++ b/myt-clean.js @@ -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' } }; diff --git a/myt-dump.js b/myt-dump.js index 19b13f0..4c447c1 100644 --- a/myt-dump.js +++ b/myt-dump.js @@ -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' }; diff --git a/myt-push.js b/myt-push.js index 041baf7..f87b9d4 100644 --- a/myt-push.js +++ b/myt-push.js @@ -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' }; diff --git a/myt-version.js b/myt-version.js index 219bcaf..8e2677f 100644 --- a/myt-version.js +++ b/myt-version.js @@ -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' }, 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.' }; 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); @@ -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() { const color = random(colors); let plant = random(plants); diff --git a/myt.js b/myt.js index 128c3b8..5b4007c 100755 --- a/myt.js +++ b/myt.js @@ -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' }