Skip to content

Commit 8be44b5

Browse files
committed
[server] don't start DB if already running
1 parent ba59f2e commit 8be44b5

22 files changed

+76
-172
lines changed

components/gitpod-db/src/auth-provider-entry.spec.db.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,16 @@ import { AuthProviderEntryDB } from ".";
1111
import { DBAuthProviderEntry } from "./typeorm/entity/db-auth-provider-entry";
1212
import { DeepPartial } from "@gitpod/gitpod-protocol/lib/util/deep-partial";
1313
import * as chai from "chai";
14+
import { resetDB } from "./test/reset-db";
1415
const expect = chai.expect;
1516

1617
@suite(timeout(10000))
1718
export class AuthProviderEntryDBSpec {
18-
private readonly db = testContainer.get<AuthProviderEntryDB>(AuthProviderEntryDB);
19-
20-
// TODO(gpl) Since the upgrade to ts-node 10.4.0 establishing the inital connection takes up to 25s
21-
@timeout(30000)
22-
async before() {
23-
await this.clear();
24-
}
19+
private db: AuthProviderEntryDB = testContainer.get<AuthProviderEntryDB>(AuthProviderEntryDB);
2520

2621
async after() {
27-
await this.clear();
28-
}
29-
30-
async clear() {
3122
const typeorm = testContainer.get<TypeORM>(TypeORM);
32-
const manager = await typeorm.getConnection();
33-
console.log("before delete");
34-
await manager.getRepository(DBAuthProviderEntry).delete({});
35-
console.log("after delete");
23+
await resetDB(typeorm);
3624
}
3725

3826
protected authProvider(ap: DeepPartial<DBAuthProviderEntry> = {}): DBAuthProviderEntry {

components/gitpod-db/src/blocked-repository-db.spec.db.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { suite, test, timeout } from "@testdeck/mocha";
1111
import { testContainer } from "./test-container";
1212
import { TypeORMBlockedRepositoryDBImpl } from "./typeorm/blocked-repository-db-impl";
1313
import { TypeORM } from "./typeorm/typeorm";
14-
import { DBBlockedRepository } from "./typeorm/entity/db-blocked-repository";
14+
import { resetDB } from "./test/reset-db";
1515

1616
@suite
1717
class BlockedRepositoryDBSpec {
@@ -79,8 +79,7 @@ class BlockedRepositoryDBSpec {
7979

8080
async wipeRepo() {
8181
const typeorm = testContainer.get<TypeORM>(TypeORM);
82-
const manager = await typeorm.getConnection();
83-
await manager.getRepository(DBBlockedRepository).delete({});
82+
await resetDB(typeorm);
8483
}
8584
}
8685

components/gitpod-db/src/email-domain-filter-db.spec.db.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { suite, test, timeout } from "@testdeck/mocha";
99
import { testContainer } from "./test-container";
1010
import { TypeORM } from "./typeorm/typeorm";
1111
import { EmailDomainFilterDB } from "./email-domain-filter-db";
12-
import { DBEmailDomainFilterEntry } from "./typeorm/entity/db-email-domain-filter-entry";
12+
import { resetDB } from "./test/reset-db";
1313
const expect = chai.expect;
1414

1515
@suite
@@ -27,9 +27,7 @@ export class EmailDomainFilterDBSpec {
2727
}
2828

2929
protected async clear() {
30-
const connection = await this.typeORM.getConnection();
31-
const manager = connection.manager;
32-
await manager.clear(DBEmailDomainFilterEntry);
30+
await resetDB(this.typeORM);
3331
}
3432

3533
@test public async filterSimple() {

components/gitpod-db/src/project-db.spec.db.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7-
import * as chai from "chai";
8-
const expect = chai.expect;
7+
import { Project } from "@gitpod/gitpod-protocol";
98
import { suite, test } from "@testdeck/mocha";
10-
import { TypeORM } from "./typeorm/typeorm";
11-
import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";
9+
import * as chai from "chai";
1210
import { testContainer } from "./test-container";
11+
import { resetDB } from "./test/reset-db";
1312
import { ProjectDBImpl } from "./typeorm/project-db-impl";
14-
import { DBProject } from "./typeorm/entity/db-project";
15-
import { DBUser } from "./typeorm/entity/db-user";
16-
import { Project } from "@gitpod/gitpod-protocol";
13+
import { TypeORM } from "./typeorm/typeorm";
14+
import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";
15+
const expect = chai.expect;
1716

1817
@suite
1918
class ProjectDBSpec {
@@ -30,9 +29,7 @@ class ProjectDBSpec {
3029

3130
async wipeRepo() {
3231
const typeorm = testContainer.get<TypeORM>(TypeORM);
33-
const manager = await typeorm.getConnection();
34-
await manager.getRepository(DBProject).delete({});
35-
await manager.getRepository(DBUser).delete({});
32+
await resetDB(typeorm);
3633
}
3734

3835
@test()

components/gitpod-db/src/team-db.spec.db.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ import { testContainer } from "./test-container";
1313
import { TeamDBImpl } from "./typeorm/team-db-impl";
1414
import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";
1515
import { TypeORM } from "./typeorm/typeorm";
16-
import { DBTeam } from "./typeorm/entity/db-team";
17-
import { DBTeamMembership } from "./typeorm/entity/db-team-membership";
18-
import { DBUser } from "./typeorm/entity/db-user";
19-
import { DBIdentity } from "./typeorm/entity/db-identity";
2016
import { Connection } from "typeorm";
17+
import { resetDB } from "./test/reset-db";
2118

2219
@suite
2320
class TeamDBSpec {
@@ -34,12 +31,7 @@ class TeamDBSpec {
3431

3532
async wipeRepo() {
3633
const typeorm = testContainer.get<TypeORM>(TypeORM);
37-
const manager = await typeorm.getConnection();
38-
await manager.getRepository(DBTeam).delete({});
39-
await manager.getRepository(DBTeamMembership).delete({});
40-
await manager.getRepository(DBUser).delete({});
41-
await manager.getRepository(DBIdentity).delete({});
42-
await manager.query("delete from d_b_oidc_client_config;");
34+
await resetDB(typeorm);
4335
}
4436

4537
@test(timeout(10000))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 { DBUser } from "../typeorm/entity/db-user";
8+
import { TypeORM } from "../typeorm/typeorm";
9+
import { isBuiltinUser } from "../user-db";
10+
11+
export async function resetDB(typeorm: TypeORM) {
12+
const conn = await typeorm.getConnection();
13+
const users = await conn.getRepository(DBUser).find();
14+
// delete all users except the builtin users
15+
conn.getRepository(DBUser).remove(users.filter((u) => !isBuiltinUser(u.id)));
16+
17+
const deletions = conn.entityMetadatas
18+
.filter((meta) => meta.tableName !== "d_b_user")
19+
.map((meta) => conn.getRepository(meta.name).clear());
20+
21+
await Promise.all([
22+
// delete all other entities
23+
...deletions,
24+
25+
// we don't have a typeorm entity for this table
26+
conn.query("DELETE FROM d_b_oidc_client_config;"),
27+
]);
28+
}

components/gitpod-db/src/typeorm/auth-code-repository-db.spec.db.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { AuthCodeRepositoryDB } from "./auth-code-repository-db";
1010
import { UserDB } from "../user-db";
1111
import { DBOAuthAuthCodeEntry } from "./entity/db-oauth-auth-code";
1212
import { TypeORM } from "./typeorm";
13-
import { DBUser } from "./entity/db-user";
1413
import * as chai from "chai";
1514
import { User } from "@gitpod/gitpod-protocol";
15+
import { resetDB } from "../test/reset-db";
1616
const expect = chai.expect;
1717

1818
@suite(timeout(10000))
@@ -30,9 +30,7 @@ export class AuthCodeRepositoryDBSpec {
3030

3131
async wipeRepo() {
3232
const typeorm = testContainer.get<TypeORM>(TypeORM);
33-
const manager = await typeorm.getConnection();
34-
await manager.getRepository(DBOAuthAuthCodeEntry).delete({});
35-
await manager.getRepository(DBUser).delete({});
33+
await resetDB(typeorm);
3634
}
3735

3836
@test()

components/gitpod-db/src/typeorm/code-sync-resource-db.spec.db.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { suite, test, timeout } from "@testdeck/mocha";
1010
import { testContainer } from "../test-container";
1111
import { CodeSyncResourceDB } from "./code-sync-resource-db";
1212
import { IUserDataManifest, SyncResource } from "./entity/db-code-sync-resource";
13+
import { resetDB } from "../test/reset-db";
14+
import { TypeORM } from "./typeorm";
1315
const expect = chai.expect;
1416

1517
@suite(timeout(10000))
@@ -23,6 +25,7 @@ export class CodeSyncResourceDBSpec {
2325
}
2426

2527
async after(): Promise<void> {
28+
await resetDB(testContainer.get<TypeORM>(TypeORM));
2629
await this.db.deleteSettingsSyncResources(this.userId, () => Promise.resolve());
2730
await this.db.deleteCollection(this.userId, undefined, () => Promise.resolve());
2831
}

components/gitpod-db/src/typeorm/one-time-secret-db-impl.spec.db.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import { suite, test, timeout } from "@testdeck/mocha";
88
import { testContainer } from "../test-container";
99
import { TypeORM } from "./typeorm";
10-
import { DBUser } from "./entity/db-user";
1110
import * as chai from "chai";
1211
import { OneTimeSecretDB } from "../one-time-secret-db";
1312
import { DBOneTimeSecret } from "./entity/db-one-time-secret";
13+
import { resetDB } from "../test/reset-db";
1414
const expect = chai.expect;
1515

1616
@suite(timeout(10000))
@@ -27,9 +27,7 @@ export class OneTimeSecretSpec {
2727

2828
async wipeRepo() {
2929
const typeorm = testContainer.get<TypeORM>(TypeORM);
30-
const manager = await typeorm.getConnection();
31-
await manager.getRepository(DBOneTimeSecret).delete({});
32-
await manager.getRepository(DBUser).delete({});
30+
await resetDB(typeorm);
3331
}
3432

3533
@test()

components/gitpod-db/src/typeorm/team-db-impl.spec.db.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7+
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
78
import { suite, test, timeout } from "@testdeck/mocha";
9+
import * as chai from "chai";
10+
import { TeamDB } from "../team-db";
811
import { testContainer } from "../test-container";
12+
import { resetDB } from "../test/reset-db";
913
import { UserDB } from "../user-db";
1014
import { TypeORM } from "./typeorm";
11-
import { DBUser } from "./entity/db-user";
12-
import * as chai from "chai";
13-
import { TeamDB } from "../team-db";
14-
import { DBTeam } from "./entity/db-team";
15-
import { ErrorCodes, ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error";
1615
const expect = chai.expect;
1716

1817
@suite(timeout(10000))
@@ -30,9 +29,7 @@ export class TeamDBSpec {
3029

3130
async wipeRepo() {
3231
const typeorm = testContainer.get<TypeORM>(TypeORM);
33-
const manager = await typeorm.getConnection();
34-
await manager.getRepository(DBTeam).delete({});
35-
await manager.getRepository(DBUser).delete({});
32+
await resetDB(typeorm);
3633
}
3734

3835
@test()

components/gitpod-db/src/user-db.spec.db.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import { DBIdentity } from "./typeorm/entity/db-identity";
1414
import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";
1515
import { TypeORMWorkspaceDBImpl } from "./typeorm/workspace-db-impl";
1616
import { TypeORM } from "./typeorm/typeorm";
17-
import { DBUser } from "./typeorm/entity/db-user";
18-
import { DBWorkspace } from "./typeorm/entity/db-workspace";
19-
import { DBWorkspaceInstance } from "./typeorm/entity/db-workspace-instance";
20-
import { DBGitpodToken } from "./typeorm/entity/db-gitpod-token";
17+
import { resetDB } from "./test/reset-db";
2118

2219
const _IDENTITY1: Identity = {
2320
authProviderId: "GitHub",
@@ -55,12 +52,7 @@ class UserDBSpec {
5552

5653
async wipeRepos() {
5754
const typeorm = testContainer.get<TypeORM>(TypeORM);
58-
const mnr = await typeorm.getConnection();
59-
await mnr.getRepository(DBUser).delete({});
60-
await mnr.getRepository(DBIdentity).delete({});
61-
await mnr.getRepository(DBWorkspace).delete({});
62-
await mnr.getRepository(DBWorkspaceInstance).delete({});
63-
await mnr.getRepository(DBGitpodToken).delete({});
55+
await resetDB(typeorm);
6456
}
6557

6658
// Copy to avoid pollution

components/gitpod-db/src/user-storage-resources-spec.db.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import { TypeORM } from "./typeorm/typeorm";
1313
import { Repository } from "typeorm";
1414
import { UserStorageResourcesDB } from "./user-storage-resources-db";
1515
import { DBUserStorageResource } from "./typeorm/entity/db-user-storage-resource";
16+
import { resetDB } from "./test/reset-db";
1617

1718
@suite
1819
class UserStorageResourcesDBSpec {
1920
typeORM = testContainer.get<TypeORM>(TypeORM);
2021
resourcesDb = testContainer.get<UserStorageResourcesDB>(UserStorageResourcesDB);
2122

2223
protected async getRepo(): Promise<Repository<DBUserStorageResource>> {
23-
return (await (await this.typeORM.getConnection()).manager).getRepository(DBUserStorageResource);
24+
return (await this.typeORM.getConnection()).manager.getRepository(DBUserStorageResource);
2425
}
2526

2627
async after() {
27-
const repo = await this.getRepo();
28-
await repo.createQueryBuilder("resource").delete().execute();
28+
await resetDB(this.typeORM);
2929
}
3030

3131
@test(timeout(10000))

components/gitpod-db/src/webhook-event-db.spec.db.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { suite, test, timeout } from "@testdeck/mocha";
99
import { testContainer } from "./test-container";
1010
import { TypeORM } from "./typeorm/typeorm";
1111
import { WebhookEventDB } from "./webhook-event-db";
12-
import { DBWebhookEvent } from "./typeorm/entity/db-webhook-event";
12+
import { resetDB } from "./test/reset-db";
1313
const expect = chai.expect;
1414

1515
@suite
@@ -27,9 +27,7 @@ export class WebhookEventDBSpec {
2727
}
2828

2929
protected async clear() {
30-
const connection = await this.typeORM.getConnection();
31-
const manager = connection.manager;
32-
await manager.clear(DBWebhookEvent);
30+
await resetDB(this.typeORM);
3331
}
3432

3533
@test public async testSafeUpdate() {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { testContainer } from "./test-container";
1010
import { TypeORM } from "./typeorm/typeorm";
1111
import { WorkspaceCluster, WorkspaceClusterDB } from "@gitpod/gitpod-protocol/lib/workspace-cluster";
1212
import { DBWorkspaceCluster } from "./typeorm/entity/db-workspace-cluster";
13+
import { resetDB } from "./test/reset-db";
1314
const expect = chai.expect;
1415

1516
@suite
@@ -27,9 +28,7 @@ export class WorkspaceClusterDBSpec {
2728
}
2829

2930
protected async clear() {
30-
const connection = await this.typeORM.getConnection();
31-
const manager = connection.manager;
32-
await manager.clear(DBWorkspaceCluster);
31+
await resetDB(this.typeORM);
3332
}
3433

3534
@test public async findByName() {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import { WorkspaceInstance, Workspace, PrebuiltWorkspace, CommitContext } from "
1313
import { testContainer } from "./test-container";
1414
import { TypeORMWorkspaceDBImpl } from "./typeorm/workspace-db-impl";
1515
import { TypeORM } from "./typeorm/typeorm";
16-
import { DBWorkspace } from "./typeorm/entity/db-workspace";
1716
import { DBPrebuiltWorkspace } from "./typeorm/entity/db-prebuilt-workspace";
18-
import { DBWorkspaceInstance } from "./typeorm/entity/db-workspace-instance";
1917
import { secondsBefore } from "@gitpod/gitpod-protocol/lib/util/timeutil";
18+
import { resetDB } from "./test/reset-db";
2019

2120
@suite
2221
class WorkspaceDBSpec {
@@ -185,10 +184,7 @@ class WorkspaceDBSpec {
185184
}
186185

187186
async wipeRepo() {
188-
const mnr = await this.typeorm.getConnection();
189-
await mnr.getRepository(DBWorkspace).delete({});
190-
await mnr.getRepository(DBWorkspaceInstance).delete({});
191-
await mnr.getRepository(DBPrebuiltWorkspace).delete({});
187+
await resetDB(this.typeorm);
192188
}
193189

194190
@test(timeout(10000))

components/server/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
"test:unit": "TS_NODE_FILES=true mocha --opts mocha.opts './**/*.spec.ts' --exclude './node_modules/**'",
2222
"test:db": "TS_NODE_FILES=true mocha --opts mocha.opts './**/*.spec.db.ts' --exclude './node_modules/**'",
2323
"start-services": "yarn start-testdb && yarn start-redis && yarn start-spicedb",
24-
"stop-services": "yarn stop-testdb && yarn stop-redis && yarn stop-spicedb",
25-
"start-testdb": "leeway run components/gitpod-db:init-testdb",
26-
"stop-testdb": "docker stop test-mysql || true && docker rm test-mysql || true",
24+
"stop-services": "yarn stop-redis && yarn stop-spicedb",
25+
"start-testdb": "if netstat -tuln | grep ':23306 '; then echo 'Mysql is already running.'; else leeway run components/gitpod-db:init-testdb; fi",
2726
"start-spicedb": "leeway run components/spicedb:start-spicedb",
2827
"stop-spicedb": "leeway run components/spicedb:stop-spicedb",
2928
"start-redis": "if netstat -tuln | grep ':6379 '; then echo 'Redis is already running.'; else docker run --rm --name test-redis -p 6379:6379 -d redis; fi",

components/server/src/orgs/organization-service.spec.db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import * as chai from "chai";
1212
import { Container } from "inversify";
1313
import "mocha";
1414
import { expectError } from "../projects/projects-service.spec.db";
15-
import { resetDB } from "../test/reset-db";
1615
import { createTestContainer } from "../test/service-testing-container-module";
1716
import { OrganizationService } from "./organization-service";
17+
import { resetDB } from "@gitpod/gitpod-db/lib/test/reset-db";
1818

1919
const expect = chai.expect;
2020

components/server/src/orgs/usage-service.spec.db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { createTestContainer } from "../test/service-testing-container-module";
2525
import { UserService } from "../user/user-service";
2626
import { OrganizationService } from "./organization-service";
2727
import { UsageService } from "./usage-service";
28-
import { resetDB } from "../test/reset-db";
28+
import { resetDB } from "@gitpod/gitpod-db/lib/test/reset-db";
2929

3030
const expect = chai.expect;
3131

0 commit comments

Comments
 (0)