Skip to content

Commit

Permalink
task: make setting service handle conflict on insert (#9160)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chriswk authored Feb 13, 2025
1 parent 1036fb6 commit 8de8010
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/lib/db/setting-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
Expand All @@ -58,13 +54,12 @@ export default class SettingStore implements ISettingStore {
return undefined;
}

// Is actually an upsert
async insert(name: string, content: any): Promise<void> {
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<void> {
Expand Down

0 comments on commit 8de8010

Please # to comment.