From 85af498e15c5bd13c70eeef6f9f9c5e039238f99 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Wed, 17 Jan 2024 15:14:35 +0100 Subject: [PATCH] feat: refs #5123 clean fixes, purge option added --- README.md | 5 ++-- myt-clean.js | 73 +++++++++++++++++++++++++++++++++++++++++++---- myt-run.js | 7 ++--- package-lock.json | 4 +-- package.json | 2 +- 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1f10be3..1ad0035 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,7 @@ $ myt create [-t ] . Cleans all already applied versions older than *maxOldVersions*. ```text -$ myt clean +$ myt clean [-p|--purge] ``` ## Local server commands @@ -276,8 +276,7 @@ $ myt fixtures [] ### run Builds and starts local database server container. It only rebuilds the image -when fixtures have been modified or when the day on which the image was built -is different to today. +dump has been modified. ```text $ myt run [-c|--ci] [-r|--random] diff --git a/myt-clean.js b/myt-clean.js index 93dbca2..03ef6cc 100644 --- a/myt-clean.js +++ b/myt-clean.js @@ -8,26 +8,36 @@ const path = require('path'); */ class Clean extends Command { static usage = { - description: 'Cleans old applied versions' + description: 'Cleans old applied versions', + params: { + purge: 'Wether to remove non-existent scripts from DB log' + } }; static opts = { + alias: { + purge: 'p' + }, + boolean: [ + 'purge' + ], default: { remote: 'production' } }; async run(myt, opts) { - await myt.dbConnect(); + const conn = await myt.dbConnect(); + const versionDirs = await fs.readdir(opts.versionsDir); + const archiveDir = path.join(opts.versionsDir, '.archive'); + const dbVersion = await myt.fetchDbVersion() || {}; const number = parseInt(dbVersion.number); const oldVersions = []; - const versionDirs = await fs.readdir(opts.versionsDir); for (const versionDir of versionDirs) { const version = await myt.loadVersion(versionDir); const shouldArchive = version - && version.matchRegex && !version.apply && parseInt(version.number) < number; @@ -39,7 +49,6 @@ class Clean extends Command { && oldVersions.length > opts.maxOldVersions) { oldVersions.splice(-opts.maxOldVersions); - const archiveDir = path.join(opts.versionsDir, '.archive'); if (!await fs.pathExists(archiveDir)) await fs.mkdir(archiveDir); @@ -52,6 +61,60 @@ class Clean extends Command { console.log(`Old versions archived: ${oldVersions.length}`); } else console.log(`No versions to archive.`); + + if (opts.purge) { + const versionDb = new VersionDb(myt, opts.versionsDir); + versionDb.load(); + + const archiveDb = new VersionDb(myt, archiveDir); + archiveDb.load(); + + const [res] = await conn.query( + `SELECT number, file FROM versionLog + WHERE code = ? + ORDER BY number, file`, + [opts.code] + ); + + for (const script of res) { + const hasVersion = await versionDb.hasScript(script); + const hasArchive = await archiveDb.hasScript(script); + + if (!hasVersion && !hasArchive) { + await conn.query( + `DELETE FROM versionLog + WHERE code = ? AND number = ? AND file = ?`, + [opts.code, script.number, script.file] + ); + } + } + } + } +} + +class VersionDb { + constructor(myt, baseDir) { + Object.assign(this, {myt, baseDir}); + } + + async load() { + const versionMap = this.versionMap = new Map(); + if (await fs.pathExists(this.baseDir)) { + const dirs = await fs.readdir(this.baseDir); + for (const dir of dirs) { + const version = this.myt.parseVersionDir(dir); + if (!version) continue; + versionMap.set(version.number, dir); + } + } + return versionMap; + } + + async hasScript(script) { + const dir = this.versionMap.get(script.number); + if (!dir) return false; + const scriptPath = path.join(this.baseDir, dir, script.file); + return await fs.pathExists(scriptPath); } } diff --git a/myt-run.js b/myt-run.js index 9af22e1..2915949 100644 --- a/myt-run.js +++ b/myt-run.js @@ -9,10 +9,9 @@ const connExt = require('./lib/conn'); const SqlString = require('sqlstring'); /** - * Builds the database image and runs a container. It only rebuilds the - * image when fixtures have been modified or when the day on which the - * image was built is different to today. Some workarounds have been used - * to avoid a bug with OverlayFS driver on MacOS. + * Builds the database image and runs a container. It only rebuilds the image + * when dump has been modified. Some workarounds have been used to avoid a bug + * with OverlayFS driver on MacOS. */ class Run extends Command { static usage = { diff --git a/package-lock.json b/package-lock.json index 7df89b6..795db60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@verdnatura/myt", - "version": "1.5.24", + "version": "1.5.25", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@verdnatura/myt", - "version": "1.5.24", + "version": "1.5.25", "license": "GPL-3.0", "dependencies": { "@sqltools/formatter": "^1.2.5", diff --git a/package.json b/package.json index 48d2b72..95ece9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@verdnatura/myt", - "version": "1.5.24", + "version": "1.5.25", "author": "Verdnatura Levante SL", "description": "MySQL version control", "license": "GPL-3.0",