From bb61a7de7d85bcce5da6a182e297ae6936b47d0c Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 4 Jan 2024 13:19:21 +0000 Subject: [PATCH] fix: handle created/lastmodified column as datetime in formula --- .../src/db/formulav2/formulaQueryBuilderv2.ts | 85 ++++++++++--------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts b/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts index 665f6bd6d9..c2166a2dc9 100644 --- a/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts +++ b/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts @@ -20,6 +20,7 @@ import { CacheGetType, CacheScope } from '~/utils/globals'; import { convertDateFormatForConcat } from '~/helpers/formulaFnHelper'; import FormulaColumn from '~/models/FormulaColumn'; import { Base, BaseUser } from '~/models'; +import { getRefColumnIfAlias } from '~/helpers'; const logger = new Logger('FormulaQueryBuilderv2'); @@ -637,49 +638,55 @@ async function _formulaQueryBuilder( }; }; break; + case UITypes.CreateTime: + case UITypes.LastModifiedTime: case UITypes.DateTime: - if (knex.clientType().startsWith('mysql')) { - aliasToColumn[col.id] = async (): Promise => { - return { - // convert from DB timezone to UTC - builder: knex.raw( - `CONVERT_TZ(??, @@GLOBAL.time_zone, '+00:00')`, - [col.column_name], - ), - }; - }; - } else if ( - knex.clientType() === 'pg' && - col.dt !== 'timestamp with time zone' && - col.dt !== 'timestamptz' - ) { - aliasToColumn[col.id] = async (): Promise => { - return { - // convert from DB timezone to UTC - builder: knex - .raw( - `?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`, + { + const col = await getRefColumnIfAlias(col); + + if (knex.clientType().startsWith('mysql')) { + aliasToColumn[col.id] = async (): Promise => { + return { + // convert from DB timezone to UTC + builder: knex.raw( + `CONVERT_TZ(??, @@GLOBAL.time_zone, '+00:00')`, [col.column_name], - ) - .wrap('(', ')'), + ), + }; }; - }; - } else if ( - knex.clientType() === 'mssql' && - col.dt !== 'datetimeoffset' - ) { - // convert from DB timezone to UTC - aliasToColumn[col.id] = async (): Promise => { - return { - builder: knex.raw( - `CONVERT(DATETIMEOFFSET, ?? AT TIME ZONE 'UTC')`, - [col.column_name], - ), + } else if ( + knex.clientType() === 'pg' && + col.dt !== 'timestamp with time zone' && + col.dt !== 'timestamptz' + ) { + aliasToColumn[col.id] = async (): Promise => { + return { + // convert from DB timezone to UTC + builder: knex + .raw( + `?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`, + [col.column_name], + ) + .wrap('(', ')'), + }; }; - }; - } else { - aliasToColumn[col.id] = () => - Promise.resolve({ builder: col.column_name }); + } else if ( + knex.clientType() === 'mssql' && + col.dt !== 'datetimeoffset' + ) { + // convert from DB timezone to UTC + aliasToColumn[col.id] = async (): Promise => { + return { + builder: knex.raw( + `CONVERT(DATETIMEOFFSET, ?? AT TIME ZONE 'UTC')`, + [col.column_name], + ), + }; + }; + } else { + aliasToColumn[col.id] = () => + Promise.resolve({ builder: col.column_name }); + } } break; case UITypes.User: