-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[installer-admin]: add the database bindings
- Loading branch information
Simon Emms
committed
Jan 17, 2022
1 parent
817d043
commit e942e8f
Showing
8 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
components/gitpod-db/src/typeorm/entity/db-installation-admin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
components/gitpod-db/src/typeorm/installation-admin-db-impl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
components/gitpod-db/src/typeorm/migration/1642422506330-InstallationAdminTable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters