From fe719676283c4b8264fa6fbfaeb52942bbc98d67 Mon Sep 17 00:00:00 2001 From: Gero Posmyk-Leinemann Date: Mon, 17 May 2021 12:07:04 +0000 Subject: [PATCH 1/3] [db] Turn --explicit-defaults-for-timestamp=OFF explicitly as we rely on this --- chart/values.yaml | 6 ++++++ components/gitpod-db/src/typeorm/transformer.ts | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/chart/values.yaml b/chart/values.yaml index bfe17fd7074ce9..279616fcc89b6f 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -494,6 +494,12 @@ mysql: fullnameOverride: mysql image: tag: 5.7 + primary: + extraEnvVars: + # We rely on this in our DB implementations: NULL (re-)sets configured columns to be initialized with CURRENT_TIMESTAMP. + # OFF is the default as documented [here](https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp) (we also see this in GCP), but not for this chart. + - name: MYSQL_EXTRA_FLAGS + value: --explicit-defaults-for-timestamp=OFF auth: existingSecret: db-password serviceAccount: diff --git a/components/gitpod-db/src/typeorm/transformer.ts b/components/gitpod-db/src/typeorm/transformer.ts index 031e66e6e2cfc9..5b259da9774b69 100644 --- a/components/gitpod-db/src/typeorm/transformer.ts +++ b/components/gitpod-db/src/typeorm/transformer.ts @@ -26,7 +26,11 @@ export namespace Transformer { export const MAP_ISO_STRING_TO_TIMESTAMP_DROP: ValueTransformer = { to(value: any): any { - // DROP all input values as they are set by the DB 'ON UPDATE'/ as default value + // DROP all input values as they are set by the DB 'ON UPDATE'/ as default value. + // We're relying on the system variable explicit-defaults-for-timestamp here (link: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp): + // `undefined` get's converted to NULL, which on the DB is turned into the configured default value for the column. + // In our case, that's 100% CURRENT_TIMESTAMP. + // This was done initially so we don't have to make fields like `creationTime` optional, or have to use two types for the same table. return undefined; }, from(value: any): any { From a5d15d4d1a9b825230f88c4b29e731e5703d3d3e Mon Sep 17 00:00:00 2001 From: Gero Posmyk-Leinemann Date: Mon, 17 May 2021 12:11:19 +0000 Subject: [PATCH 2/3] [db] Adjust typeorm default values to actual table config --- components/gitpod-db/src/typeorm/entity/db-app-installation.ts | 2 ++ components/gitpod-db/src/typeorm/entity/db-layout-data.ts | 1 + components/gitpod-db/src/typeorm/entity/db-license-key.ts | 1 + .../gitpod-db/src/typeorm/entity/db-prebuilt-workspace.ts | 1 + components/gitpod-db/src/typeorm/entity/db-snapshot.ts | 1 + .../gitpod-db/src/typeorm/entity/db-user-message-view-entry.ts | 1 + 6 files changed, 7 insertions(+) diff --git a/components/gitpod-db/src/typeorm/entity/db-app-installation.ts b/components/gitpod-db/src/typeorm/entity/db-app-installation.ts index 0c7a94cf77d393..7475b22ac27b50 100644 --- a/components/gitpod-db/src/typeorm/entity/db-app-installation.ts +++ b/components/gitpod-db/src/typeorm/entity/db-app-installation.ts @@ -33,6 +33,7 @@ export class DBAppInstallation implements AppInstallation { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) creationTime: string; @@ -40,6 +41,7 @@ export class DBAppInstallation implements AppInstallation { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) lastUpdateTime: string; diff --git a/components/gitpod-db/src/typeorm/entity/db-layout-data.ts b/components/gitpod-db/src/typeorm/entity/db-layout-data.ts index 466b6503336a00..d81a8944a0d08d 100644 --- a/components/gitpod-db/src/typeorm/entity/db-layout-data.ts +++ b/components/gitpod-db/src/typeorm/entity/db-layout-data.ts @@ -19,6 +19,7 @@ export class DBLayoutData implements LayoutData { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) lastUpdatedTime: string; diff --git a/components/gitpod-db/src/typeorm/entity/db-license-key.ts b/components/gitpod-db/src/typeorm/entity/db-license-key.ts index 8b2f45f644865d..c6e298f9172af8 100644 --- a/components/gitpod-db/src/typeorm/entity/db-license-key.ts +++ b/components/gitpod-db/src/typeorm/entity/db-license-key.ts @@ -18,6 +18,7 @@ export class DBLicenseKey { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) installationTime: string; diff --git a/components/gitpod-db/src/typeorm/entity/db-prebuilt-workspace.ts b/components/gitpod-db/src/typeorm/entity/db-prebuilt-workspace.ts index 725c5fdb59db14..83497cf78b0d0f 100644 --- a/components/gitpod-db/src/typeorm/entity/db-prebuilt-workspace.ts +++ b/components/gitpod-db/src/typeorm/entity/db-prebuilt-workspace.ts @@ -31,6 +31,7 @@ export class DBPrebuiltWorkspace implements PrebuiltWorkspace { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) creationTime: string; diff --git a/components/gitpod-db/src/typeorm/entity/db-snapshot.ts b/components/gitpod-db/src/typeorm/entity/db-snapshot.ts index d03831455fb784..0d1dd1866908b1 100644 --- a/components/gitpod-db/src/typeorm/entity/db-snapshot.ts +++ b/components/gitpod-db/src/typeorm/entity/db-snapshot.ts @@ -20,6 +20,7 @@ export class DBSnapshot implements Snapshot { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) creationTime: string; diff --git a/components/gitpod-db/src/typeorm/entity/db-user-message-view-entry.ts b/components/gitpod-db/src/typeorm/entity/db-user-message-view-entry.ts index cb773292435d36..2497022e46630b 100644 --- a/components/gitpod-db/src/typeorm/entity/db-user-message-view-entry.ts +++ b/components/gitpod-db/src/typeorm/entity/db-user-message-view-entry.ts @@ -20,6 +20,7 @@ export class DBUserMessageViewEntry { @Column({ type: 'timestamp', precision: 6, + default: () => 'CURRENT_TIMESTAMP(6)', transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP }) viewedAt: string; From 26ebd1483bbb230f57c5c434ee71cfa12964a3bb Mon Sep 17 00:00:00 2001 From: Gero Posmyk-Leinemann <32448529+geropl@users.noreply.github.com> Date: Mon, 17 May 2021 16:58:08 +0200 Subject: [PATCH 3/3] Typo in transformer.ts Co-authored-by: Jan Keromnes --- components/gitpod-db/src/typeorm/transformer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/gitpod-db/src/typeorm/transformer.ts b/components/gitpod-db/src/typeorm/transformer.ts index 5b259da9774b69..68a74d3cb98b63 100644 --- a/components/gitpod-db/src/typeorm/transformer.ts +++ b/components/gitpod-db/src/typeorm/transformer.ts @@ -28,7 +28,7 @@ export namespace Transformer { to(value: any): any { // DROP all input values as they are set by the DB 'ON UPDATE'/ as default value. // We're relying on the system variable explicit-defaults-for-timestamp here (link: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp): - // `undefined` get's converted to NULL, which on the DB is turned into the configured default value for the column. + // `undefined` gets converted to NULL, which on the DB is turned into the configured default value for the column. // In our case, that's 100% CURRENT_TIMESTAMP. // This was done initially so we don't have to make fields like `creationTime` optional, or have to use two types for the same table. return undefined; @@ -76,4 +76,4 @@ export class CompositeValueTransformer implements ValueTransformer { from(value: any): any { return this.upper.from(this.lower.from(value)); } -} \ No newline at end of file +}