fix: refs #6661 #5123 #5483 clean, run triggers, push logging, code clean

This commit is contained in:
Juan Ferrer 2024-01-14 15:49:49 +01:00
parent 19b368b219
commit 55a47ec99c
7 changed files with 125 additions and 92 deletions

View File

@ -19,16 +19,19 @@ class Clean extends Command {
async run(myt, opts) { async run(myt, opts) {
await myt.dbConnect(); await myt.dbConnect();
const version = await myt.fetchDbVersion() || {}; const dbVersion = await myt.fetchDbVersion() || {};
const number = version.number; const number = parseInt(dbVersion.number);
const oldVersions = []; const oldVersions = [];
const versionDirs = await fs.readdir(opts.versionsDir); const versionDirs = await fs.readdir(opts.versionsDir);
for (const versionDir of versionDirs) { for (const versionDir of versionDirs) {
const dirVersion = myt.parseVersionDir(versionDir); const version = await myt.loadVersion(versionDir);
if (!dirVersion) continue; const shouldArchive = version
&& version.matchRegex
&& !version.apply
&& parseInt(version.number) < number;
if (parseInt(dirVersion.number) < parseInt(number)) if (shouldArchive)
oldVersions.push(versionDir); oldVersions.push(versionDir);
} }
@ -46,9 +49,9 @@ class Clean extends Command {
path.join(archiveDir, oldVersion) path.join(archiveDir, oldVersion)
); );
console.log(`Old versions deleted: ${oldVersions.length}`); console.log(`Old versions archived: ${oldVersions.length}`);
} else } else
console.log(`No versions to delete.`); console.log(`No versions to archive.`);
} }
} }

View File

@ -90,17 +90,17 @@ class Push extends Command {
// Get database version // Get database version
const version = await myt.fetchDbVersion() || {}; const dbVersion = await myt.fetchDbVersion() || {};
console.log( console.log(
`Database information:` `Database information:`
+ `\n -> Version: ${version.number}` + `\n -> Version: ${dbVersion.number}`
+ `\n -> Commit: ${version.gitCommit}` + `\n -> Commit: ${dbVersion.gitCommit}`
); );
if (!version.number) if (!dbVersion.number)
version.number = String('0').padStart(opts.versionDigits, '0'); dbVersion.number = String('0').padStart(opts.versionDigits, '0');
if (!/^[0-9]*$/.test(version.number)) if (!/^[0-9]*$/.test(dbVersion.number))
throw new Error('Wrong database version'); throw new Error('Wrong database version');
// Prevent push to production by mistake // Prevent push to production by mistake
@ -142,16 +142,25 @@ class Push extends Command {
let silent = true; let silent = true;
const versionsDir = opts.versionsDir; const versionsDir = opts.versionsDir;
function logVersion(version, name, error) { function logVersion(version, name, action, error) {
console.log('', version.bold, name); let actionMsg;
switch(action) {
case 'apply':
actionMsg = '[A]'.green;
break;
case 'ignore':
actionMsg = '[I]'.blue;
break;
default:
actionMsg = '[W]'.yellow;
}
console.log('', (actionMsg + version).bold, name);
} }
function logScript(type, message, error) { function logScript(type, message, error) {
console.log(' ', type.bold, message); console.log(' ', type.bold, message);
} }
function isUndoScript(script) {
return /\.undo\.sql$/.test(script);
}
const skipFiles = new Set([ const skipFiles = new Set([
'README.md', 'README.md',
'.archive' '.archive'
@ -159,87 +168,49 @@ class Push extends Command {
if (await fs.pathExists(versionsDir)) { if (await fs.pathExists(versionsDir)) {
const versionDirs = await fs.readdir(versionsDir); const versionDirs = await fs.readdir(versionsDir);
const [[realm]] = await this.conn.query(
`SELECT realm
FROM versionConfig`
);
for (const versionDir of versionDirs) { for (const versionDir of versionDirs) {
if (skipFiles.has(versionDir)) continue; if (skipFiles.has(versionDir)) continue;
const version = await myt.loadVersion(versionDir);
const dirVersion = myt.parseVersionDir(versionDir); if (!version) {
if (!dirVersion) { logVersion('[?????]'.yellow, versionDir, 'warn',
logVersion('[?????]'.yellow, versionDir,
`Wrong directory name.` `Wrong directory name.`
); );
continue; continue;
} }
if (version.number.length != dbVersion.number.length) {
const versionNumber = dirVersion.number; logVersion('[*****]'.gray, versionDir, 'warn'
const versionName = dirVersion.name; `Bad version length, should have ${dbVersion.number.length} characters.`
if (versionNumber.length != version.number.length) {
logVersion('[*****]'.gray, versionDir,
`Bad version length, should have ${version.number.length} characters.`
); );
continue; continue;
} }
const scriptsDir = `${versionsDir}/${versionDir}`; const {apply} = version;
const scripts = await fs.readdir(scriptsDir); if (apply) silent = false;
const [versionLog] = await conn.query(
`SELECT file FROM versionLog
WHERE code = ?
AND number = ?
AND errorNumber IS NULL`,
[opts.code, versionNumber]
);
for (const script of scripts)
if (!isUndoScript(script)
&& versionLog.findIndex(x => x.file == script) === -1) {
silent = false;
break;
}
if (silent) continue; if (silent) continue;
logVersion(`[${versionNumber}]`.cyan, versionName);
for (const script of scripts) { const action = apply ? 'apply' : 'ignore';
const match = script.match(/^[0-9]{2}-[a-zA-Z0-9_]+(?:\.(?!undo)([a-zA-Z0-9_]+))?(?:\.undo)?\.sql$/); logVersion(`[${version.number}]`.cyan, version.name, action);
if (!apply) continue;
for (const script of version.scripts) {
const scriptFile = script.file;
if (!match) { if (!script.matchRegex) {
logScript('[W]'.yellow, script, `Wrong file name.`); logScript('[W]'.yellow, scriptFile, `Wrong file name.`);
continue; continue;
} }
const skipRealm = match[1] && match[1] !== realm; const actionMsg = script.apply ? '[+]'.green : '[I]'.blue;
logScript(actionMsg, scriptFile);
if (isUndoScript(script) || skipRealm)
continue; if (!script.apply) continue;
const [[row]] = await conn.query(
`SELECT errorNumber FROM versionLog
WHERE code = ?
AND number = ?
AND file = ?`,
[
opts.code,
versionNumber,
script
]
);
const apply = !row || row.errorNumber;
const actionMsg = apply ? '[+]'.green : '[I]'.blue;
logScript(actionMsg, script);
if (!apply) continue;
let err; let err;
try { try {
await connExt.queryFromFile(pushConn, await connExt.queryFromFile(pushConn,
`${scriptsDir}/${script}`); `${versionsDir}/${versionDir}/${scriptFile}`);
} catch (e) { } catch (e) {
err = e; err = e;
} }
@ -260,8 +231,8 @@ class Push extends Command {
errorMessage = VALUES(errorMessage)`, errorMessage = VALUES(errorMessage)`,
[ [
opts.code, opts.code,
versionNumber, version.number,
script, scriptFile,
err && err.errno, err && err.errno,
err && err.message err && err.message
] ]
@ -271,7 +242,7 @@ class Push extends Command {
nChanges++; nChanges++;
} }
await this.updateVersion('number', versionNumber); await this.updateVersion('number', version.number);
} }
} }
@ -282,7 +253,7 @@ class Push extends Command {
const gitExists = await fs.pathExists(`${opts.workspace}/.git`); const gitExists = await fs.pathExists(`${opts.workspace}/.git`);
let nRoutines = 0; let nRoutines = 0;
const changes = await this.changedRoutines(version.gitCommit); const changes = await this.changedRoutines(dbVersion.gitCommit);
const routines = []; const routines = [];
for (const change of changes) for (const change of changes)
@ -359,7 +330,7 @@ class Push extends Command {
const typeMsg = `[${change.type.abbr}]`[change.type.color]; const typeMsg = `[${change.type.abbr}]`[change.type.color];
console.log('', console.log('',
(statusMsg + actionMsg).bold, (actionMsg + statusMsg).bold,
typeMsg.bold, typeMsg.bold,
change.fullName change.fullName
); );
@ -419,7 +390,7 @@ class Push extends Command {
const repo = await nodegit.Repository.open(this.opts.workspace); const repo = await nodegit.Repository.open(this.opts.workspace);
const head = await repo.getHeadCommit(); const head = await repo.getHeadCommit();
if (head && version.gitCommit !== head.sha()) if (head && dbVersion.gitCommit !== head.sha())
await this.updateVersion('gitCommit', head.sha()); await this.updateVersion('gitCommit', head.sha());
} }

View File

@ -140,7 +140,7 @@ class Run extends Command {
const hasTriggers = await fs.exists(`${dumpDataDir}/triggers.sql`); const hasTriggers = await fs.exists(`${dumpDataDir}/triggers.sql`);
Object.assign(opts, { Object.assign(opts, {
triggers: hasTriggers, triggers: !hasTriggers,
commit: true, commit: true,
dbConfig dbConfig
}); });

View File

@ -74,9 +74,9 @@ class Version extends Command {
const versionNames = new Set(); const versionNames = new Set();
const versionDirs = await fs.readdir(opts.versionsDir); const versionDirs = await fs.readdir(opts.versionsDir);
for (const versionDir of versionDirs) { for (const versionDir of versionDirs) {
const dirVersion = myt.parseVersionDir(versionDir); const version = myt.parseVersionDir(versionDir);
if (!dirVersion) continue; if (!version) continue;
versionNames.add(dirVersion.name); versionNames.add(version.name);
} }
if (!versionName) { if (!versionName) {

59
myt.js
View File

@ -11,6 +11,8 @@ const mysql = require('mysql2/promise');
const nodegit = require('nodegit'); const nodegit = require('nodegit');
const camelToSnake = require('./lib/util').camelToSnake; const camelToSnake = require('./lib/util').camelToSnake;
const scriptRegex = /^[0-9]{2}-[a-zA-Z0-9_]+(?:\.(?!undo)([a-zA-Z0-9_]+))?(\.undo)?\.sql$/;
class Myt { class Myt {
static usage = { static usage = {
description: 'Utility for database versioning', description: 'Utility for database versioning',
@ -305,6 +307,11 @@ class Myt {
`${__dirname}/assets/structure.sql`, 'utf8'); `${__dirname}/assets/structure.sql`, 'utf8');
await conn.query(structure); await conn.query(structure);
} }
const [[realm]] = await conn.query(
`SELECT realm FROM versionConfig`
);
this.realm = realm;
} }
return this.conn; return this.conn;
@ -332,6 +339,58 @@ class Myt {
}; };
} }
async loadVersion(versionDir) {
const {opts} = this;
const info = this.parseVersionDir(versionDir);
if (!info) return null;
const versionsDir = opts.versionsDir;
const scriptsDir = `${versionsDir}/${versionDir}`;
const scriptList = await fs.readdir(scriptsDir);
const [res] = await this.conn.query(
`SELECT file, errorNumber IS NOT NULL hasError
FROM versionLog
WHERE code = ?
AND number = ?`,
[opts.code, info.number]
);
const versionLog = new Map();
res.map(x => versionLog.set(x.file, x));
let applyVersion = false;
const scripts = [];
for (const file of scriptList) {
const match = file.match(scriptRegex);
if (match) {
const scriptRealm = match[1];
const isUndo = !!match[2];
if ((scriptRealm && scriptRealm !== this.realm) || isUndo)
continue;
}
const logInfo = versionLog.get(file);
const apply = !logInfo || logInfo.hasError;
if (apply) applyVersion = true;
scripts.push({
file,
matchRegex: !!match,
apply
});
}
return {
number: info.number,
name: info.name,
scripts,
apply: applyVersion
};
}
async openRepo() { async openRepo() {
const {opts} = this; const {opts} = this;

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@verdnatura/myt", "name": "@verdnatura/myt",
"version": "1.5.22", "version": "1.5.24",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@verdnatura/myt", "name": "@verdnatura/myt",
"version": "1.5.22", "version": "1.5.24",
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"@sqltools/formatter": "^1.2.5", "@sqltools/formatter": "^1.2.5",

View File

@ -1,6 +1,6 @@
{ {
"name": "@verdnatura/myt", "name": "@verdnatura/myt",
"version": "1.5.23", "version": "1.5.24",
"author": "Verdnatura Levante SL", "author": "Verdnatura Levante SL",
"description": "MySQL version control", "description": "MySQL version control",
"license": "GPL-3.0", "license": "GPL-3.0",