Skip to content

Commit

Permalink
[db] Use INSERT INTO ... ON DUPLICATE KEY UPDATE for user storage
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl committed Feb 15, 2021
1 parent 9b4e22f commit c05795b
Showing 1 changed file with 9 additions and 13 deletions.
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 c05795b

Please sign in to comment.