Skip to content

Commit

Permalink
fix(core): Use consistent timezone-aware timestamps in postgres
Browse files Browse the repository at this point in the history
Fixes:
* ENG-51 / N8N-2490
* PAY-397
* #2178
* #2810
* #3855

Supersedes #2813
  • Loading branch information
netroy committed Aug 16, 2023
1 parent 0913e0d commit 89d9ab0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
20 changes: 7 additions & 13 deletions packages/cli/src/databases/entities/AbstractEntity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
BeforeInsert,
BeforeUpdate,
CreateDateColumn,
PrimaryColumn,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import type { ColumnOptions } from 'typeorm';
import { BeforeInsert, BeforeUpdate, Column, PrimaryColumn } from 'typeorm';
import { IsOptional } from 'class-validator';
import config from '@/config';
import { generateNanoId } from '../utils/generators';

Expand All @@ -21,9 +16,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;
Expand All @@ -45,17 +41,15 @@ function mixinStringId<T extends Constructor<{}>>(base: T) {

function mixinTimestamps<T extends Constructor<{}>>(base: T) {
class Derived extends base {
@CreateDateColumn(tsColumnOptions)
@Column(tsColumnOptions)
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;

@UpdateDateColumn({
@Column({
...tsColumnOptions,
onUpdate: timestampSyntax,
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;

@BeforeUpdate()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { IrreversibleMigration, MigrationContext } from '@/databases/types';

const defaultTimestampColumns = ['createdAt', 'updatedAt'];
const tablesWithDefaultTimestamps = [
'auth_identity',
'credentials_entity',
'event_destinations',
'installed_packages',
'role',
'shared_credentials',
'shared_workflow',
'tag_entity',
'user',
'workflow_entity',
];

const additionalColumns = {
auth_provider_sync_history: ['endedAt', 'startedAt'],
execution_entity: ['startedAt', 'stoppedAt', 'waitTill'],
workflow_statistics: ['latestEvent'],
};

export class MigrateToTimestampZ1692207852593 implements IrreversibleMigration {
async up({ queryRunner, tablePrefix }: MigrationContext) {
// You cannot define a TIMESTAMPTZ column with a specific precision for fractional seconds other than 6.
const changeColumnType = async (tableName: string, columnName: string) =>
queryRunner.query(
`ALTER TABLE "${tablePrefix}${tableName}" ALTER COLUMN "${columnName}" TYPE TIMESTAMPTZ(3)`,
);

for (const tableName of tablesWithDefaultTimestamps) {
for (const columnName of defaultTimestampColumns) {
await changeColumnType(tableName, columnName);
}
}
for (const [tableName, columnNames] of Object.entries(additionalColumns)) {
for (const columnName of columnNames) {
await changeColumnType(tableName, columnName);
}
}
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/postgresdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { RemoveSkipOwnerSetup1681134145997 } from './1681134145997-RemoveSkipOwn
import { RemoveResetPasswordColumns1690000000030 } from '../common/1690000000030-RemoveResetPasswordColumns';
import { AddMissingPrimaryKeyOnExecutionData1690787606731 } from './1690787606731-AddMissingPrimaryKeyOnExecutionData';
import { CreateWorkflowNameIndex1691088862123 } from '../common/1691088862123-CreateWorkflowNameIndex';
import { MigrateToTimestampZ1692207852593 } from './1692207852593-MigrateToTimestampZ1692207852593';

export const postgresMigrations: Migration[] = [
InitialMigration1587669153312,
Expand Down Expand Up @@ -87,4 +88,5 @@ export const postgresMigrations: Migration[] = [
RemoveResetPasswordColumns1690000000030,
AddMissingPrimaryKeyOnExecutionData1690787606731,
CreateWorkflowNameIndex1691088862123,
MigrateToTimestampZ1692207852593,
];

0 comments on commit 89d9ab0

Please sign in to comment.