Skip to content

Commit

Permalink
[db] Improve performance of DBUserStorageResource.update(...) (gitpod…
Browse files Browse the repository at this point in the history
…-io#3151)

* [db] UserStorageResource: make userId char(36) instead of varchar(255)

* [db] Use INSERT INTO ... ON DUPLICATE KEY UPDATE for user storage
  • Loading branch information
geropl authored Feb 15, 2021
1 parent 54416b8 commit 886f014
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class UserStorageUserIdChar1612781029090 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("ALTER TABLE d_b_user_storage_resource MODIFY userId char(36);");
}

public async down(queryRunner: QueryRunner): Promise<any> {
}

}
22 changes: 9 additions & 13 deletions components/gitpod-db/src/typeorm/user-storage-resources-db-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { injectable, inject } from "inversify";
import { TypeORM } from "./typeorm";
import { UserStorageResourcesDB } from "../user-storage-resources-db";
import { DBUserStorageResource } from "./entity/db-user-storage-resource";
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';

@injectable()
export class TypeORMUserStorageResourcesDBImpl implements UserStorageResourcesDB {
Expand All @@ -31,19 +30,16 @@ export class TypeORMUserStorageResourcesDBImpl implements UserStorageResourcesDB
}

async update(userId: string, uri: string, content: string): Promise<void> {
// docs: https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
const repo = await this.getUserStorageResourceRepo();
let resource = await this.getResource(userId, uri);
if (resource) {
log.info({ userId }, 'updating resource', { uri });
await repo.update(resource, { content });
} else {
log.info({ userId }, 'saving resource', { uri });
resource = new DBUserStorageResource();
resource.userId = userId;
resource.uri = uri;
resource.content = content;
await repo.save(resource);
}
await repo.query(`
INSERT INTO d_b_user_storage_resource
(userId, uri, content)
VALUES
(?, ?, ?)
ON DUPLICATE KEY UPDATE
content = VALUES(content);
`, [ userId, uri, content ]);
}

async deleteAllForUser(userId: string): Promise<void> {
Expand Down

0 comments on commit 886f014

Please sign in to comment.