From c05795bd9fb4a5fe1a71a3505e685feec7e04258 Mon Sep 17 00:00:00 2001 From: Gero Posmyk-Leinemann Date: Mon, 8 Feb 2021 10:59:48 +0000 Subject: [PATCH] [db] Use INSERT INTO ... ON DUPLICATE KEY UPDATE for user storage --- .../typeorm/user-storage-resources-db-impl.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/components/gitpod-db/src/typeorm/user-storage-resources-db-impl.ts b/components/gitpod-db/src/typeorm/user-storage-resources-db-impl.ts index b68a47bc071058..2d61f31b12196a 100644 --- a/components/gitpod-db/src/typeorm/user-storage-resources-db-impl.ts +++ b/components/gitpod-db/src/typeorm/user-storage-resources-db-impl.ts @@ -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 { @@ -31,19 +30,16 @@ export class TypeORMUserStorageResourcesDBImpl implements UserStorageResourcesDB } async update(userId: string, uri: string, content: string): Promise { + // 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 {