Skip to content

Commit

Permalink
[installer-admin]: add the database bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Emms committed Jan 17, 2022
1 parent 817d043 commit e942e8f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 5 deletions.
5 changes: 5 additions & 0 deletions components/gitpod-db/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import { ProjectDBImpl } from './typeorm/project-db-impl';
import { EntityManager } from 'typeorm';
import { OssAllowListDB } from './oss-allowlist-db';
import { OssAllowListDBImpl } from './typeorm/oss-allowlist-db-impl';
import { TypeORMInstallationAdminImpl } from './typeorm/installation-admin-db-impl';
import { InstallationAdminDB } from './installation-admin-db';

// THE DB container module that contains all DB implementations
export const dbContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
Expand All @@ -72,6 +74,9 @@ export const dbContainerModule = new ContainerModule((bind, unbind, isBound, reb
bind(TermsAcceptanceDB).toService(TermsAcceptanceDBImpl);
bindDbWithTracing(TracedUserDB, bind, UserDB).inSingletonScope();

bind(TypeORMInstallationAdminImpl).toSelf().inSingletonScope();
bind(InstallationAdminDB).toService(TypeORMInstallationAdminImpl);

bind(AuthProviderEntryDB).to(AuthProviderEntryDBImpl).inSingletonScope();

bind(TypeORMWorkspaceDBImpl).toSelf().inSingletonScope();
Expand Down
3 changes: 2 additions & 1 deletion components/gitpod-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export * from "./edu-email-domain-db";
export * from "./email-domain-filter-db";
export * from "./typeorm/entity/db-account-entry";
export * from "./project-db";
export * from "./team-db";
export * from "./team-db";
export * from "./installation-admin-db";
12 changes: 12 additions & 0 deletions components/gitpod-db/src/installation-admin-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { InstallationAdmin } from "@gitpod/gitpod-protocol";

export const InstallationAdminDB = Symbol('InstallationAdminDB');
export interface InstallationAdminDB {
getData(): Promise<InstallationAdmin>
}
18 changes: 18 additions & 0 deletions components/gitpod-db/src/typeorm/entity/db-installation-admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { InstallationAdmin } from "@gitpod/gitpod-protocol";
import { Entity, Column, PrimaryColumn } from "typeorm";
import { TypeORM } from "../typeorm";

@Entity()
export class DBInstallationAdmin implements InstallationAdmin {
@PrimaryColumn(TypeORM.UUID_COLUMN_TYPE)
id: string;

@Column()
sendTelemetry: boolean
}
56 changes: 56 additions & 0 deletions components/gitpod-db/src/typeorm/installation-admin-db-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { inject, injectable, } from 'inversify';
import { InstallationAdmin } from '@gitpod/gitpod-protocol';
import { v4 as uuidv4 } from 'uuid';
import { Repository } from 'typeorm';
import { TypeORM } from './typeorm';
import { InstallationAdminDB } from '../installation-admin-db';
import { DBInstallationAdmin } from './entity/db-installation-admin';

@injectable()
export class TypeORMInstallationAdminImpl implements InstallationAdminDB {
@inject(TypeORM) typeORM: TypeORM;

protected async getEntityManager() {
return (await this.typeORM.getConnection()).manager;
}

protected async createDefaultRecord(): Promise<InstallationAdmin> {
const record: InstallationAdmin = {
id: uuidv4(),
sendTelemetry: true
};

const repo = await this.getInstallationAdminRepo()
return repo.save(record);
}

async getInstallationAdminRepo(): Promise<Repository<DBInstallationAdmin>> {
return (await this.getEntityManager()).getRepository<DBInstallationAdmin>(DBInstallationAdmin);
}

/**
* Get Data
*
* Returns the first record found or creates a
* new record.
*
* @returns Promise<InstallationAdmin>
*/
async getData(): Promise<InstallationAdmin> {
const repo = await this.getInstallationAdminRepo()
const [record] = await repo.find()

if (record) {
return record;
}

/* Record not found - create one */
return this.createDefaultRecord();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";
import { tableExists } from "./helper/helper";

export class InstallationAdminTable1642422506330 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("CREATE TABLE IF NOT EXISTS `d_b_installation_admin` (`id` char(128) NOT NULL, `sendTelemetry` tinyint(4) NOT NULL DEFAULT 0, `_lastModified` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY(`id`)) ENGINE=InnoDB");
}

public async down(queryRunner: QueryRunner): Promise<void> {
if (await tableExists(queryRunner, "d_b_installation_admin")) {
await queryRunner.query("DROP TABLE `d_b_installation_admin`");
}
}

}
5 changes: 5 additions & 0 deletions components/gitpod-protocol/src/teams-projects-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ export interface Team {
deleted?: boolean;
}

export interface InstallationAdmin {
id: string;
sendTelemetry: boolean;
}

export type TeamMemberRole = "owner" | "member";

export interface TeamMemberInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* See License-AGPL.txt in the project root for license information.
*/

import { injectable } from 'inversify';
import { injectable, inject } from 'inversify';
import * as express from 'express';
import { InstallationAdminDB } from '@gitpod/gitpod-db/lib';

@injectable()
export class InstallationAdminController {
@inject(InstallationAdminDB) protected readonly installationAdminDb: InstallationAdminDB;

get apiRouter(): express.Router {
const router = express.Router();

router.get('/data', async (req: express.Request, res: express.Response) => {
res.send({
sendTelemetry: false
});
const data = await this.installationAdminDb.getData();

res.send(data);
});

return router;
Expand Down

0 comments on commit e942e8f

Please sign in to comment.