Skip to content

Commit 2220a24

Browse files
authored
[migrations] Make workspace cluster name the PK, drop composite PK on (name, appCluster) (#16769)
* [migrations] Make workspace cluster name the PK, drop composite PK on (name, appCluster) * fix * fix * fix
1 parent bdff903 commit 2220a24

File tree

4 files changed

+33
-111
lines changed

4 files changed

+33
-111
lines changed

components/gitpod-db/src/typeorm/entity/db-workspace-cluster.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ export class DBWorkspaceCluster implements WorkspaceCluster {
8888
})
8989
admissionConstraints?: AdmissionConstraint[];
9090

91-
@PrimaryColumn({
91+
@Column({
9292
type: "varchar",
9393
length: 60,
9494
})
9595
applicationCluster: string;
9696

97-
@PrimaryColumn({
97+
@Column({
9898
type: "varchar",
9999
length: 60,
100100
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { MigrationInterface, QueryRunner } from "typeorm";
8+
9+
export class WorkspaceClustersNamePK1678365331415 implements MigrationInterface {
10+
public async up(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query("ALTER TABLE `d_b_workspace_cluster` DROP PRIMARY KEY, ADD PRIMARY KEY (name)");
12+
}
13+
14+
public async down(queryRunner: QueryRunner): Promise<void> {
15+
await queryRunner.query(
16+
"ALTER TABLE `d_b_workspace_cluster` DROP PRIMARY KEY, ADD PRIMARY KEY (name, applicationCluster)",
17+
);
18+
}
19+
}

components/gitpod-db/src/typeorm/workspace-cluster-db-impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class WorkspaceClusterDBImpl implements WorkspaceClusterDB {
2323
return (await this.typeORM.getConnection()).manager;
2424
}
2525

26-
protected async getRepo(): Promise<Repository<DBWorkspaceCluster>> {
26+
public async getRepo(): Promise<Repository<DBWorkspaceCluster>> {
2727
return (await this.getEntityManager()).getRepository(DBWorkspaceCluster);
2828
}
2929

components/gitpod-db/src/workspace-cluster-db.spec.db.ts

Lines changed: 11 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import * as chai from "chai";
88
import { suite, test, timeout } from "mocha-typescript";
99
import { testContainer } from "./test-container";
1010
import { TypeORM } from "./typeorm/typeorm";
11-
import {
12-
WorkspaceCluster,
13-
WorkspaceClusterDB,
14-
WorkspaceClusterWoTLS,
15-
} from "@gitpod/gitpod-protocol/lib/workspace-cluster";
11+
import { WorkspaceCluster, WorkspaceClusterDB } from "@gitpod/gitpod-protocol/lib/workspace-cluster";
1612
import { DBWorkspaceCluster } from "./typeorm/entity/db-workspace-cluster";
1713
const expect = chai.expect;
1814

@@ -39,27 +35,17 @@ export class WorkspaceClusterDBSpec {
3935
@test public async findByName() {
4036
const wsc1: DBWorkspaceCluster = dbWorkspaceCluster({
4137
name: "eu71",
42-
applicationCluster: "eu02",
38+
applicationCluster: "us02",
4339
region: "europe",
4440
url: "some-url",
4541
state: "available",
4642
score: 100,
4743
maxScore: 100,
4844
govern: true,
4945
});
50-
const wsc1a: DBWorkspaceCluster = dbWorkspaceCluster({
51-
name: "eu71",
52-
applicationCluster: "us02",
53-
region: "north-america",
54-
url: "some-url",
55-
state: "cordoned",
56-
score: 0,
57-
maxScore: 0,
58-
govern: false,
59-
});
6046
const wsc2: DBWorkspaceCluster = dbWorkspaceCluster({
6147
name: "us71",
62-
applicationCluster: "eu02",
48+
applicationCluster: "us02",
6349
region: "europe",
6450
url: "some-url",
6551
state: "cordoned",
@@ -69,26 +55,18 @@ export class WorkspaceClusterDBSpec {
6955
});
7056

7157
await this.db.save(wsc1);
72-
await this.db.save(wsc1a);
7358
await this.db.save(wsc2);
7459

75-
// Can find the eu71 cluster as seen by the eu02 application cluster.
76-
const result = await this.db.findByName("eu71", "eu02");
60+
const result = await this.db.findByName("eu71", "us02");
7761
expect(result).not.to.be.undefined;
7862
expect((result as WorkspaceCluster).name).to.equal("eu71");
79-
expect((result as WorkspaceCluster).applicationCluster).to.equal("eu02");
63+
expect((result as WorkspaceCluster).applicationCluster).to.equal("us02");
8064

8165
// Can find the eu71 cluster as seen by the us02 application cluster.
8266
const result2 = await this.db.findByName("eu71", "us02");
8367
expect(result2).not.to.be.undefined;
8468
expect((result2 as WorkspaceCluster).name).to.equal("eu71");
8569
expect((result2 as WorkspaceCluster).applicationCluster).to.equal("us02");
86-
87-
// Can find the us71 cluster as seen by the eu02 application cluster.
88-
const result3 = await this.db.findByName("us71", "eu02");
89-
expect(result3).not.to.be.undefined;
90-
expect((result3 as WorkspaceCluster).name).to.equal("us71");
91-
expect((result3 as WorkspaceCluster).applicationCluster).to.equal("eu02");
9270
}
9371

9472
@test public async deleteByName() {
@@ -136,16 +114,6 @@ export class WorkspaceClusterDBSpec {
136114

137115
@test public async testFindFilteredByName() {
138116
const wsc1: DBWorkspaceCluster = dbWorkspaceCluster({
139-
name: "eu71",
140-
applicationCluster: "eu02",
141-
region: "europe",
142-
url: "some-url",
143-
state: "available",
144-
score: 100,
145-
maxScore: 100,
146-
govern: true,
147-
});
148-
const wsc1a: DBWorkspaceCluster = dbWorkspaceCluster({
149117
name: "eu71",
150118
applicationCluster: "us02",
151119
region: "north-america",
@@ -167,35 +135,25 @@ export class WorkspaceClusterDBSpec {
167135
});
168136

169137
await this.db.save(wsc1);
170-
await this.db.save(wsc1a);
171138
await this.db.save(wsc2);
172139

173-
const wscs = await this.db.findFiltered({ name: "eu71", applicationCluster: "eu02" });
140+
const wscs = await this.db.findFiltered({ name: "eu71", applicationCluster: "us02" });
174141
expect(wscs.length).to.equal(1);
175142
expect(wscs[0].name).to.equal("eu71");
176-
expect(wscs[0].applicationCluster).to.equal("eu02");
143+
expect(wscs[0].applicationCluster).to.equal("us02");
177144
}
178145

179146
@test public async testFindFilteredByApplicationCluster() {
180147
const wsc1: DBWorkspaceCluster = dbWorkspaceCluster({
181148
name: "eu71",
182-
applicationCluster: "eu02",
149+
applicationCluster: "us02",
183150
region: "europe",
184151
url: "some-url",
185152
state: "available",
186153
score: 100,
187154
maxScore: 100,
188155
govern: true,
189-
});
190-
const wsc1a: DBWorkspaceCluster = dbWorkspaceCluster({
191-
name: "eu71",
192-
applicationCluster: "us02",
193-
region: "north-america",
194-
url: "some-url",
195-
state: "cordoned",
196-
score: 0,
197-
maxScore: 0,
198-
govern: false,
156+
admissionConstraints: [],
199157
});
200158
const wsc2: DBWorkspaceCluster = dbWorkspaceCluster({
201159
name: "us71",
@@ -206,79 +164,27 @@ export class WorkspaceClusterDBSpec {
206164
score: 100,
207165
maxScore: 100,
208166
govern: true,
167+
admissionConstraints: [],
209168
});
210169

211170
await this.db.save(wsc1);
212-
await this.db.save(wsc1a);
213171
await this.db.save(wsc2);
214172

215-
const expectedClusters: WorkspaceClusterWoTLS[] = [
216-
{
217-
name: "eu71",
218-
applicationCluster: "eu02",
219-
region: "europe",
220-
url: "some-url",
221-
state: "available",
222-
score: 100,
223-
maxScore: 100,
224-
govern: true,
225-
admissionConstraints: [],
226-
},
227-
];
228-
const actualClusters = await this.db.findFiltered({ applicationCluster: "eu02" });
229-
expect(actualClusters.length).to.equal(1);
230-
expect(actualClusters).to.deep.include.members(expectedClusters);
231-
232-
const expectedClusters2: WorkspaceClusterWoTLS[] = [
233-
{
234-
name: "eu71",
235-
applicationCluster: "us02",
236-
region: "north-america",
237-
url: "some-url",
238-
state: "cordoned",
239-
score: 0,
240-
maxScore: 0,
241-
govern: false,
242-
admissionConstraints: [],
243-
},
244-
{
245-
name: "us71",
246-
applicationCluster: "us02",
247-
region: "north-america",
248-
url: "some-url",
249-
state: "available",
250-
score: 100,
251-
maxScore: 100,
252-
govern: true,
253-
admissionConstraints: [],
254-
},
255-
];
256173
const wscs2 = await this.db.findFiltered({ applicationCluster: "us02" });
257174
expect(wscs2.length).to.equal(2);
258-
expect(wscs2).to.deep.include.members(expectedClusters2);
259175
}
260176

261177
@test public async testFindFilteredExcludesDeletedClusters() {
262178
const wsc1: DBWorkspaceCluster = dbWorkspaceCluster({
263179
name: "eu71",
264-
applicationCluster: "eu02",
180+
applicationCluster: "us02",
265181
region: "europe",
266182
url: "some-url",
267183
state: "available",
268184
score: 100,
269185
maxScore: 100,
270186
govern: true,
271187
});
272-
const wsc1a: DBWorkspaceCluster = dbWorkspaceCluster({
273-
name: "eu71",
274-
applicationCluster: "us02",
275-
region: "north-america",
276-
url: "some-url",
277-
state: "cordoned",
278-
score: 0,
279-
maxScore: 0,
280-
govern: false,
281-
});
282188
const wsc2: DBWorkspaceCluster = dbWorkspaceCluster({
283189
name: "us71",
284190
applicationCluster: "us02",
@@ -291,15 +197,12 @@ export class WorkspaceClusterDBSpec {
291197
});
292198

293199
await this.db.save(wsc1);
294-
await this.db.save(wsc1a);
295200
await this.db.save(wsc2);
296201

297202
await this.db.deleteByName("eu71", "us02");
298203

299204
let wscs = await this.db.findFiltered({ applicationCluster: "us02" });
300205
expect(wscs.length).to.equal(1);
301-
wscs = await this.db.findFiltered({ applicationCluster: "eu02" });
302-
expect(wscs.length).to.equal(1);
303206
}
304207

305208
@test public async testFindFilteredWithRegion() {

0 commit comments

Comments
 (0)