Skip to content

Commit

Permalink
Revert "[db] Make hand-written transformers more robust against odd D…
Browse files Browse the repository at this point in the history
…B values" (#6889)

This reverts commit 3be9435.
  • Loading branch information
geropl committed Nov 25, 2021
1 parent b558f9d commit 348d534
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class DBPrebuildInfo {
prebuildId: string;

@Column({
type: 'text',
type: 'simple-json',
transformer: (() => {
return {
to(value: any): any {
Expand All @@ -27,7 +27,6 @@ export class DBPrebuildInfo {
const obj = JSON.parse(value);
return PrebuildInfo.is(obj) ? obj : undefined;
} catch (error) {
return undefined;
}
}
};
Expand Down
46 changes: 7 additions & 39 deletions components/gitpod-db/src/typeorm/entity/db-workspace-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { PrimaryColumn, Column, Entity, Index } from "typeorm";
import { AdmissionConstraint, AdmissionPreference, TLSConfig, WorkspaceCluster, WorkspaceClusterState } from "@gitpod/gitpod-protocol/lib/workspace-cluster";
import { ValueTransformer } from "typeorm/decorator/options/ValueTransformer";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";

@Entity()
export class DBWorkspaceCluster implements WorkspaceCluster {
Expand All @@ -21,7 +20,7 @@ export class DBWorkspaceCluster implements WorkspaceCluster {
url: string;

@Column({
type: "text",
type: "simple-json",
transformer: (() => {
const defaultValue = {};
const jsonifiedDefault = JSON.stringify(defaultValue);
Expand All @@ -34,20 +33,11 @@ export class DBWorkspaceCluster implements WorkspaceCluster {
return JSON.stringify(value);
},
// <tls> | "{}" => tls | undefined
// also: "" | NULL => undefined (to make sure to not break on odd DB values)
from(value: any): any {
if (!value || value === jsonifiedDefault) {
return undefined;
}

try {
return JSON.parse(value);
} catch (err) {
// ideally we want typeorm to skip this complete row, but can't.
// errors make the whole query fail: too risky here!
log.error("error parsing value ws-cluster from DB", err, { value });
if (value === jsonifiedDefault) {
return undefined;
}
return JSON.parse(value);
}
};
})()
Expand All @@ -71,7 +61,7 @@ export class DBWorkspaceCluster implements WorkspaceCluster {
govern: boolean;

@Column({
type: "text",
type: "simple-json",
transformer: (() => {
const defaultValue: AdmissionConstraint[] = [];
const jsonifiedDefault = JSON.stringify(defaultValue);
Expand All @@ -82,29 +72,16 @@ export class DBWorkspaceCluster implements WorkspaceCluster {
}
return JSON.stringify(value);
},
// "[...]" | "[]" => []
// also: "" | NULL => undefined (to make sure to not break on odd DB values)
from(value: any): any {
if (!value) {
return undefined;
}

try {
return JSON.parse(value);
} catch (err) {
// ideally we want typeorm to skip this complete row, but can't.
// errors make the whole query fail: too risky here!
log.error("error parsing value ws-cluster from DB", err, { value });
return undefined;
}
return JSON.parse(value);
}
};
})()
})
admissionConstraints?: AdmissionConstraint[];

@Column({
type: "text",
type: "simple-json",
transformer: (() => {
const defaultValue: AdmissionPreference[] = [];
const jsonifiedDefault = JSON.stringify(defaultValue);
Expand All @@ -115,20 +92,11 @@ export class DBWorkspaceCluster implements WorkspaceCluster {
}
return JSON.stringify(value);
},
// "[...]" | "[]" => []
// also: "" | NULL => undefined (to make sure to not break on odd DB values)
from(value: any): any {
if (!value) {
return undefined;
}
try {
return JSON.parse(value);
} catch (err) {
// ideally we want typeorm to skip this complete row, but can't.
// errors make the whole query fail: too risky here!
log.error("error parsing value ws-cluster from DB", err, { value });
return undefined;
}
return JSON.parse(value);
}
};
})()
Expand Down

0 comments on commit 348d534

Please sign in to comment.