fix(postgres): remove invalid cast block and safely guard bean property checks
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 10s

This commit is contained in:
vickytechkey 2026-10-19 23:03:49 +05:30
parent 873c295e35
commit 3cae940252
2 changed files with 11 additions and 43 deletions

View file

@ -503,7 +503,6 @@ class Database {
CREATE OR REPLACE FUNCTION int_eq_bool(integer, boolean) RETURNS boolean STRICT IMMUTABLE LANGUAGE SQL AS $$ SELECT ($1 != 0) = $2; $$;
CREATE OR REPLACE FUNCTION bool_ne_int(boolean, integer) RETURNS boolean STRICT IMMUTABLE LANGUAGE SQL AS $$ SELECT $1 != ($2 != 0); $$;
CREATE OR REPLACE FUNCTION int_ne_bool(integer, boolean) RETURNS boolean STRICT IMMUTABLE LANGUAGE SQL AS $$ SELECT ($1 != 0) != $2; $$;
CREATE OR REPLACE FUNCTION int_to_bool(integer) RETURNS boolean STRICT IMMUTABLE LANGUAGE SQL AS $$ SELECT ($1 != 0); $$;
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_operator WHERE oprname = '=' AND oprleft = 'boolean'::regtype AND oprright = 'integer'::regtype) THEN
CREATE OPERATOR = (LEFTARG = boolean, RIGHTARG = integer, PROCEDURE = bool_eq_int, COMMUTATOR = =);
@ -517,13 +516,6 @@ class Database {
IF NOT EXISTS (SELECT 1 FROM pg_operator WHERE oprname = '<>' AND oprleft = 'integer'::regtype AND oprright = 'boolean'::regtype) THEN
CREATE OPERATOR <> (LEFTARG = integer, RIGHTARG = boolean, PROCEDURE = int_ne_bool, COMMUTATOR = <>);
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_cast WHERE castsource = 'integer'::regtype AND casttarget = 'boolean'::regtype) THEN
BEGIN
CREATE CAST (integer AS boolean) WITH FUNCTION int_to_bool(integer) AS IMPLICIT;
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END IF;
END $$;
`);
} catch (e) {

View file

@ -60,14 +60,20 @@ function patchRedBeanPostgres() {
}
function sanitizePgBeanBooleans(bean) {
if (!bean) {
if (!bean || typeof bean !== "object") {
return;
}
for (const col of PG_BOOLEAN_COLUMNS) {
if (bean[col] === 0 || bean[col] === "0") {
bean[col] = false;
} else if (bean[col] === 1 || bean[col] === "1") {
bean[col] = true;
try {
if (col in bean) {
if (bean[col] === 0 || bean[col] === "0") {
bean[col] = false;
} else if (bean[col] === 1 || bean[col] === "1") {
bean[col] = true;
}
}
} catch (e) {
// ignore
}
}
}
@ -127,36 +133,6 @@ function patchRedBeanPostgres() {
}
};
const origGetAll = RedBeanNode.prototype.getAll;
if (origGetAll) {
RedBeanNode.prototype.getAll = function (sql, data) {
if (this.dbType === "pg") {
sql = cleanPgSql(sql);
}
return origGetAll.call(this, sql, data);
};
}
const origGetCell = RedBeanNode.prototype.getCell;
if (origGetCell) {
RedBeanNode.prototype.getCell = function (sql, data) {
if (this.dbType === "pg") {
sql = cleanPgSql(sql);
}
return origGetCell.call(this, sql, data);
};
}
const origGetCol = RedBeanNode.prototype.getCol;
if (origGetCol) {
RedBeanNode.prototype.getCol = function (sql, data) {
if (this.dbType === "pg") {
sql = cleanPgSql(sql);
}
return origGetCol.call(this, sql, data);
};
}
const origFindCore = RedBeanNode.prototype.findCore;
RedBeanNode.prototype.findCore = function (type, clause, data = []) {
if (this.dbType === "pg") {