diff --git a/server/database.js b/server/database.js index 616b3b3..6e0de10 100644 --- a/server/database.js +++ b/server/database.js @@ -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) { diff --git a/server/utils/patch-redbean-postgres.js b/server/utils/patch-redbean-postgres.js index a3092d4..92b5217 100644 --- a/server/utils/patch-redbean-postgres.js +++ b/server/utils/patch-redbean-postgres.js @@ -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") {