refs #5541 Fixes: Cast values before compare, don't cast boolean nulls
gitea/mylogger/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2023-06-15 18:16:23 +02:00
parent 15ebbb05eb
commit 8bb4901cb0
3 changed files with 29 additions and 16 deletions

View File

@ -320,13 +320,12 @@ module.exports = class MyLogger {
return rowExcludeField && row[rowExcludeField];
}
function castValue(col, value) {
switch(tableInfo.castTypes.get(col)) {
case 'boolean':
return !!value;
default:
return value;
}
function cast(value, type) {
if (value == null || !type)
return value;
const fn = castFn[type];
return fn ? fn(value) : value;
}
function equals(a, b) {
@ -350,6 +349,8 @@ module.exports = class MyLogger {
return false;
}
const {castTypes} = tableInfo;
if (action == 'update') {
const cols = tableInfo.columns;
@ -363,13 +364,17 @@ module.exports = class MyLogger {
let nColsChanged = 0;
for (const col in before) {
if (cols.has(col)
&& !equals(after[col], before[col])) {
oldI[col] = castValue(col, before[col]);
newI[col] = castValue(col, after[col]);
if (!cols.has(col)) continue;
const type = castTypes.get(col);
const oldValue = cast(before[col], type);
const newValue = cast(after[col], type);
if (!equals(oldValue, newValue)) {
oldI[col] = oldValue;
newI[col] = newValue;
nColsChanged++;
}
}
if (nColsChanged)
changes.push({row: after, oldI, newI});
}
@ -381,9 +386,11 @@ module.exports = class MyLogger {
const instance = {};
for (const col of cols) {
if (row[col] !== null)
instance[col] = castValue(col, row[col]);
if (row[col] == null) continue;
const type = castTypes.get(col);
instance[col] = cast(row[col], type);
}
changes.push({row, instance});
}
}
@ -619,3 +626,9 @@ const actionColor = {
update: 'yellow',
delete: 'red'
};
const castFn = {
boolean: function(value) {
return !!value;
}
};

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "mylogger",
"version": "1.1.1",
"version": "1.1.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "mylogger",
"version": "1.1.1",
"version": "1.1.2",
"license": "GPL-3.0",
"dependencies": {
"colors": "^1.4.0",

View File

@ -1,6 +1,6 @@
{
"name": "mylogger",
"version": "1.1.1",
"version": "1.1.2",
"author": "Verdnatura Levante SL",
"description": "MySQL and MariaDB logger using binary log",
"license": "GPL-3.0",