From 8de801025fd7ce70f7305bf32f4243e074042033 Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Thu, 13 Feb 2025 15:54:43 +0100 Subject: [PATCH] task: make setting service handle conflict on insert (#9160) Currently, in enterprise we're struggling with setting service and transactionality; all our verfications says that the setting key is not present, so setting-store happily tries to insert a new row with a new PK. However, somehow at the same time, the key already exists. This commit adds conflict handling to the insertNewRow. --- src/lib/db/setting-store.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/lib/db/setting-store.ts b/src/lib/db/setting-store.ts index d73c088cd5de..f2f58569998a 100644 --- a/src/lib/db/setting-store.ts +++ b/src/lib/db/setting-store.ts @@ -32,10 +32,6 @@ export default class SettingStore implements ISettingStore { }); } - async insertNewRow(name: string, content: any) { - return this.db(TABLE).insert({ name, content }); - } - async exists(name: string): Promise { const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`, @@ -58,13 +54,12 @@ export default class SettingStore implements ISettingStore { return undefined; } + // Is actually an upsert async insert(name: string, content: any): Promise { - const exists = await this.exists(name); - if (exists) { - await this.updateRow(name, content); - } else { - await this.insertNewRow(name, content); - } + await this.db(TABLE) + .insert({ name, content }) + .onConflict('name') + .merge(); } async delete(name: string): Promise {