Skip to content

Commit f1c142b

Browse files
sagor999roboquat
authored andcommitted
[gitpod-db] add new DB entity for VolumeSnapshot
1 parent 037a480 commit f1c142b

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

components/gitpod-db/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Contains all the database related functionality, implemented using [typeorm](https://typeorm.io/).
44

5+
### Working on gitpod-protocol component
6+
When you are making changes to gitpod-protocol component, make sure to run `yarn build` in gitpod-protocol folder to make sure your changes will be rebuild. Also consider running `yarn watch` so that any changes are rebuilt in realtime.
7+
58
### Adding a new table
69
1. Create a [migration](./src/typeorm/migration/README.md) - use the [baseline](./src/typeorm/migration/1592203031938-Baseline.ts) as an exemplar
710
1. Create a new entity that implements the requisite interface or extend an existing entity as required - see [db-user.ts](./src/typeorm/entity/db-user.ts)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2022 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 { PrimaryColumn, Column, Entity, Index } from "typeorm";
8+
9+
import { VolumeSnapshot } from "@gitpod/gitpod-protocol";
10+
import { TypeORM } from "../typeorm";
11+
import { Transformer } from "../transformer";
12+
13+
@Entity()
14+
@Index("ind_dbsync", ["creationTime"]) // DBSync
15+
export class DBVolumeSnapshot implements VolumeSnapshot {
16+
@PrimaryColumn(TypeORM.UUID_COLUMN_TYPE)
17+
id: string;
18+
19+
@Column({
20+
type: "timestamp",
21+
precision: 6,
22+
default: () => "CURRENT_TIMESTAMP(6)",
23+
transformer: Transformer.MAP_ISO_STRING_TO_TIMESTAMP_DROP,
24+
})
25+
creationTime: string;
26+
27+
@Column(TypeORM.WORKSPACE_ID_COLUMN_TYPE)
28+
@Index("ind_originalWorkspaceId")
29+
originalWorkspaceId: string;
30+
31+
@Column("varchar")
32+
volumeHandle: string;
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) 2022 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 VolumeSnapshotCreation1651188368768 implements MigrationInterface {
10+
public async up(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query(
12+
`CREATE TABLE IF NOT EXISTS d_b_volume_snapshot ( id char(36) NOT NULL, creationTime timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), originalWorkspaceId char(36) NOT NULL, volumeHandle varchar(255) NOT NULL, PRIMARY KEY (id), KEY ind_originalWorkspaceId (originalWorkspaceId), KEY ind_dbsync (creationTime)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
13+
);
14+
}
15+
16+
public async down(queryRunner: QueryRunner): Promise<void> {
17+
}
18+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
WorkspaceInstanceUser,
2626
WhitelistedRepository,
2727
Snapshot,
28+
VolumeSnapshot,
2829
LayoutData,
2930
PrebuiltWorkspace,
3031
RunningWorkspaceInfo,
@@ -41,6 +42,7 @@ import { DBWorkspace } from "./entity/db-workspace";
4142
import { DBWorkspaceInstance } from "./entity/db-workspace-instance";
4243
import { DBLayoutData } from "./entity/db-layout-data";
4344
import { DBSnapshot } from "./entity/db-snapshot";
45+
import { DBVolumeSnapshot } from "./entity/db-volume-snapshot";
4446
import { DBWorkspaceInstanceUser } from "./entity/db-workspace-instance-user";
4547
import { DBRepositoryWhiteList } from "./entity/db-repository-whitelist";
4648
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
@@ -79,6 +81,10 @@ export abstract class AbstractTypeORMWorkspaceDBImpl implements WorkspaceDB {
7981
return await (await this.getManager()).getRepository<DBSnapshot>(DBSnapshot);
8082
}
8183

84+
protected async getVolumeSnapshotRepo(): Promise<Repository<DBVolumeSnapshot>> {
85+
return await (await this.getManager()).getRepository<DBVolumeSnapshot>(DBVolumeSnapshot);
86+
}
87+
8288
protected async getPrebuiltWorkspaceRepo(): Promise<Repository<DBPrebuiltWorkspace>> {
8389
return await (await this.getManager()).getRepository<DBPrebuiltWorkspace>(DBPrebuiltWorkspace);
8490
}
@@ -700,6 +706,34 @@ export abstract class AbstractTypeORMWorkspaceDBImpl implements WorkspaceDB {
700706
return snapshots.find({ where: { originalWorkspaceId: workspaceId } });
701707
}
702708

709+
public async findVolumeSnapshotById(volumeSnapshotId: string): Promise<VolumeSnapshot | undefined> {
710+
const volumeSnapshots = await this.getVolumeSnapshotRepo();
711+
return volumeSnapshots.findOne(volumeSnapshotId);
712+
}
713+
714+
public async storeVolumeSnapshot(volumeSnapshot: VolumeSnapshot): Promise<VolumeSnapshot> {
715+
const volumeSnapshots = await this.getVolumeSnapshotRepo();
716+
const dbVolumeSnapshot = volumeSnapshot as DBVolumeSnapshot;
717+
return await volumeSnapshots.save(dbVolumeSnapshot);
718+
}
719+
720+
public async deleteVolumeSnapshot(volumeSnapshotId: string): Promise<void> {
721+
const volumeSnapshots = await this.getVolumeSnapshotRepo();
722+
await volumeSnapshots.delete(volumeSnapshotId);
723+
}
724+
725+
public async updateVolumeSnapshot(
726+
volumeSnapshot: DeepPartial<VolumeSnapshot> & Pick<VolumeSnapshot, "id">,
727+
): Promise<void> {
728+
const volumeSnapshots = await this.getVolumeSnapshotRepo();
729+
await volumeSnapshots.update(volumeSnapshot.id, volumeSnapshot);
730+
}
731+
732+
public async findVolumeSnapshotsByWorkspaceId(workspaceId: string): Promise<VolumeSnapshot[]> {
733+
const volumeSnapshots = await this.getVolumeSnapshotRepo();
734+
return volumeSnapshots.find({ where: { originalWorkspaceId: workspaceId } });
735+
}
736+
703737
public async storePrebuiltWorkspace(pws: PrebuiltWorkspace): Promise<PrebuiltWorkspace> {
704738
const repo = await this.getPrebuiltWorkspaceRepo();
705739
if (pws.error && pws.error.length > 255) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
WorkspaceInstanceUser,
1414
WhitelistedRepository,
1515
Snapshot,
16+
VolumeSnapshot,
1617
LayoutData,
1718
PrebuiltWorkspace,
1819
PrebuiltWorkspaceUpdatable,
@@ -159,6 +160,12 @@ export interface WorkspaceDB {
159160
deleteSnapshot(snapshotId: string): Promise<void>;
160161
updateSnapshot(snapshot: DeepPartial<Snapshot> & Pick<Snapshot, "id">): Promise<void>;
161162

163+
findVolumeSnapshotById(volumeSnapshotId: string): Promise<VolumeSnapshot | undefined>;
164+
findVolumeSnapshotsByWorkspaceId(workspaceId: string): Promise<VolumeSnapshot[]>;
165+
storeVolumeSnapshot(snapshot: VolumeSnapshot): Promise<VolumeSnapshot>;
166+
deleteVolumeSnapshot(volumeSnapshotId: string): Promise<void>;
167+
updateVolumeSnapshot(snapshot: DeepPartial<VolumeSnapshot> & Pick<VolumeSnapshot, "id">): Promise<void>;
168+
162169
storePrebuiltWorkspace(pws: PrebuiltWorkspace): Promise<PrebuiltWorkspace>;
163170
findPrebuiltWorkspaceByCommit(cloneURL: string, commit: string): Promise<PrebuiltWorkspace | undefined>;
164171
findPrebuildsWithWorkpace(cloneURL: string): Promise<PrebuildWithWorkspace[]>;

components/gitpod-protocol/src/protocol.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ export interface Snapshot {
488488
message?: string;
489489
}
490490

491+
export interface VolumeSnapshot {
492+
id: string;
493+
creationTime: string;
494+
originalWorkspaceId: string;
495+
volumeHandle: string;
496+
}
497+
491498
export type SnapshotState = "pending" | "available" | "error";
492499

493500
export interface LayoutData {

0 commit comments

Comments
 (0)