fix(core): Use consistent timezone-aware timestamps in postgres (#6948)

Fixes:
* ENG-51 / N8N-2490
* PAY-397
* #2178
* #2810
* #3855

Supersedes #2813

[DB
Tests](https://github.com/n8n-io/n8n/actions/runs/6000780146/job/16273596338)
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-09-27 18:44:47 +02:00
committed by GitHub
parent ebce6fe1b0
commit 0132514f8b
3 changed files with 50 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
import type { ColumnOptions } from 'typeorm';
import {
BeforeInsert,
BeforeUpdate,
@@ -5,7 +6,6 @@ import {
PrimaryColumn,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import config from '@/config';
import { generateNanoId } from '../utils/generators';
@@ -21,9 +21,10 @@ const timestampSyntax = {
export const jsonColumnType = dbType === 'sqlite' ? 'simple-json' : 'json';
export const datetimeColumnType = dbType === 'postgresdb' ? 'timestamptz' : 'datetime';
const tsColumnOptions = {
const tsColumnOptions: ColumnOptions = {
precision: 3,
default: () => timestampSyntax,
type: datetimeColumnType,
};
type Constructor<T> = new (...args: any[]) => T;
@@ -46,16 +47,9 @@ function mixinStringId<T extends Constructor<{}>>(base: T) {
function mixinTimestamps<T extends Constructor<{}>>(base: T) {
class Derived extends base {
@CreateDateColumn(tsColumnOptions)
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
...tsColumnOptions,
onUpdate: timestampSyntax,
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
@UpdateDateColumn(tsColumnOptions)
updatedAt: Date;
@BeforeUpdate()