Skip to content

Commit

Permalink
fix(core): Use consistent timezone-aware timestamps in postgres (#6948)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Oct 17, 2023
1 parent 258e362 commit f766f6d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
23 changes: 9 additions & 14 deletions packages/cli/src/databases/entities/AbstractEntity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ColumnOptions } from 'typeorm';
import { BeforeUpdate, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import config from '@/config';

const dbType = config.getEnv('database.type');
Expand All @@ -14,22 +14,17 @@ const timestampSyntax = {
export const jsonColumnType = dbType === 'sqlite' ? 'simple-json' : 'json';
export const datetimeColumnType = dbType === 'postgresdb' ? 'timestamptz' : 'datetime';

const tsColumnOptions: ColumnOptions = {
precision: 3,
default: () => timestampSyntax,
type: datetimeColumnType,
};

export abstract class AbstractEntity {
@CreateDateColumn({
precision: 3,
default: () => timestampSyntax,
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
@CreateDateColumn(tsColumnOptions)
createdAt: Date;

@UpdateDateColumn({
precision: 3,
default: () => timestampSyntax,
onUpdate: timestampSyntax,
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
@UpdateDateColumn(tsColumnOptions)
updatedAt: Date;

@BeforeUpdate()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 MigrateToTimestampTz1694091729095 implements IrreversibleMigration {
async up({ queryRunner, tablePrefix }: MigrationContext) {
const changeColumnType = async (tableName: string, columnName: string, setDefault: boolean) => {
const alterColumnQuery = `ALTER TABLE "${tablePrefix}${tableName}" ALTER COLUMN "${columnName}"`;
await queryRunner.query(`${alterColumnQuery} TYPE TIMESTAMP(3) WITH TIME ZONE`);
if (setDefault)
await queryRunner.query(`${alterColumnQuery} SET DEFAULT CURRENT_TIMESTAMP(3)`);
};

for (const tableName of tablesWithDefaultTimestamps) {
for (const columnName of defaultTimestampColumns) {
await changeColumnType(tableName, columnName, true);
}
}

for (const [tableName, columnNames] of Object.entries(additionalColumns)) {
for (const columnName of columnNames) {
await changeColumnType(tableName, columnName, false);
}
}
}
}
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 @@ -39,6 +39,7 @@ import { AddUserActivatedProperty1681134145996 } from './1681134145996-AddUserAc
import { MigrateIntegerKeysToString1690000000000 } from './1690000000000-MigrateIntegerKeysToString';
import { SeparateExecutionData1690000000020 } from './1690000000020-SeparateExecutionData';
import { AddMissingPrimaryKeyOnExecutionData1690787606731 } from './1690787606731-AddMissingPrimaryKeyOnExecutionData';
import { MigrateToTimestampTz1694091729095 } from './1694091729095-MigrateToTimestampTz';

export const postgresMigrations: Migration[] = [
InitialMigration1587669153312,
Expand Down Expand Up @@ -81,4 +82,5 @@ export const postgresMigrations: Migration[] = [
MigrateIntegerKeysToString1690000000000,
SeparateExecutionData1690000000020,
AddMissingPrimaryKeyOnExecutionData1690787606731,
MigrateToTimestampTz1694091729095,
];

0 comments on commit f766f6d

Please sign in to comment.