Skip to content

Commit e591a3d

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

13 files changed

+63
-73
lines changed

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 { DBGitpodToken } from "../typeorm/entity/db-gitpod-token";
8+
import { DBIdentity } from "../typeorm/entity/db-identity";
9+
import { DBOAuthAuthCodeEntry } from "../typeorm/entity/db-oauth-auth-code";
10+
import { DBOneTimeSecret } from "../typeorm/entity/db-one-time-secret";
11+
import { DBProject } from "../typeorm/entity/db-project";
12+
import { DBTeam } from "../typeorm/entity/db-team";
13+
import { DBTeamMembership } from "../typeorm/entity/db-team-membership";
14+
import { DBUser } from "../typeorm/entity/db-user";
15+
import { DBWorkspace } from "../typeorm/entity/db-workspace";
16+
import { DBWorkspaceInstance } from "../typeorm/entity/db-workspace-instance";
17+
import { TypeORM } from "../typeorm/typeorm";
18+
import { isBuiltinUser } from "../user-db";
19+
20+
export async function resetDB(typeorm: TypeORM) {
21+
const conn = await typeorm.getConnection();
22+
// delete all users except the builtin users
23+
const users = await conn.getRepository(DBUser).find();
24+
await conn.getRepository(DBUser).remove(users.filter((u) => !isBuiltinUser(u.id)));
25+
26+
await conn.getRepository(DBTeam).clear();
27+
await conn.getRepository(DBTeamMembership).clear();
28+
await conn.getRepository(DBProject).clear();
29+
await conn.getRepository(DBIdentity).clear();
30+
await conn.getRepository(DBWorkspace).clear();
31+
await conn.getRepository(DBWorkspaceInstance).clear();
32+
await conn.getRepository(DBGitpodToken).clear();
33+
await conn.getRepository(DBOAuthAuthCodeEntry).clear();
34+
await conn.getRepository(DBOneTimeSecret).clear();
35+
// we don't have a typeorm entity for this table
36+
await conn.query("DELETE FROM d_b_oidc_client_config;");
37+
}

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/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/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

components/server/src/projects/projects-service.spec.db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { createTestContainer } from "../test/service-testing-container-module";
1515
import { ProjectsService } from "./projects-service";
1616
import { ApplicationError, ErrorCode, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1717
import { SpiceDBAuthorizer } from "../authorization/spicedb-authorizer";
18-
import { resetDB } from "../test/reset-db";
18+
import { resetDB } from "@gitpod/gitpod-db/lib/test/reset-db";
1919

2020
const expect = chai.expect;
2121

components/server/src/test/reset-db.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

components/server/src/user/user-service.spec.db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Container } from "inversify";
1010
import { createTestContainer } from "../test/service-testing-container-module";
1111
import { UserService } from "./user-service";
1212
import { TypeORM } from "@gitpod/gitpod-db/lib";
13-
import { resetDB } from "../test/reset-db";
13+
import { resetDB } from "@gitpod/gitpod-db/lib/test/reset-db";
1414

1515
const expect = chai.expect;
1616

0 commit comments

Comments
 (0)