Browse Source

fix(nocodb): pg xcdb & ext datetime logic

pull/5642/head
Wing-Kam Wong 1 year ago
parent
commit
98997396fc
  1. 43
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 12
      packages/nocodb/src/models/Model.ts

43
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -1631,16 +1631,25 @@ class BaseModelSqlv2 {
}`,
],
);
break;
} else if (this.isPg) {
// TODO
res[sanitize(column.title || column.column_name)] = sanitize(
`${alias || this.model.table_name}.${column.column_name}`,
);
} else {
res[sanitize(column.title || column.column_name)] = sanitize(
`${alias || this.model.table_name}.${column.column_name}`,
);
// if there is no timezone info, convert it to UTC
if (column.dt !== 'timestamp with time zone') {
res[sanitize(column.title || column.column_name)] =
this.dbDriver.raw(
`?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`,
[
`${sanitize(alias || this.model.table_name)}.${
column.column_name
}`,
],
);
break;
}
}
res[sanitize(column.title || column.column_name)] = sanitize(
`${alias || this.model.table_name}.${column.column_name}`,
);
break;
case 'LinkToAnotherRecord':
case 'Lookup':
@ -2191,10 +2200,6 @@ class BaseModelSqlv2 {
}
}
// set the session timezone
// see the comments in function for details
await this.setUtcTimezone(trx);
const response =
this.isPg || this.isMssql
? await trx
@ -2256,10 +2261,6 @@ class BaseModelSqlv2 {
transaction = await this.dbDriver.transaction();
// set the session timezone
// see the comments in function for details
await this.setUtcTimezone(transaction);
for (const o of toBeUpdated) {
await transaction(this.tnPath).update(o.d).where(o.wherePk);
}
@ -3225,10 +3226,6 @@ class BaseModelSqlv2 {
query = sanitize(query);
}
// set the session timezone
// see the comments in function for details
await this.setUtcTimezone(this.dbDriver);
let data =
this.isPg || this.isSnowflake
? (await this.dbDriver.raw(query))?.rows
@ -3391,12 +3388,6 @@ class BaseModelSqlv2 {
}
return data;
}
private async setUtcTimezone(knex) {
if (this.isPg) {
await knex.raw(`SET TIME ZONE 'UTC'`);
}
}
}
function extractSortsObject(

12
packages/nocodb/src/models/Model.ts

@ -489,13 +489,8 @@ export default class Model implements TableType {
.utcOffset(d.getTimezoneOffset(), true)
.format('YYYY-MM-DD HH:mm:ss');
} else if (isPg) {
if (col.dt !== 'timestamp with time zone') {
// e.g. 2023-05-10T08:49:32.000Z -> 2023-05-10 08:49:32-08:00
val = dayjs
.utc(val)
.utcOffset(d.getTimezoneOffset(), true)
.format('YYYY-MM-DD HH:mm:ssZ');
}
// e.g. 2023-01-01T12:00:00.000Z -> 2023-01-01 20:00:00+08:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ');
} else if (isMssql) {
// e.g. 2023-05-10T08:49:32.000Z -> 2023-05-10 08:49:32-08:00
val = dayjs
@ -527,7 +522,8 @@ export default class Model implements TableType {
}
}
} else {
val = dayjs(val).utc().format('YYYY-MM-DD HH:mm:ssZ');
// e.g. 2023-01-01T12:00:00.000Z -> 2023-01-01 20:00:00+08:00
val = dayjs(val).format('YYYY-MM-DD HH:mm:ssZ');
}
}
}

Loading…
Cancel
Save