Skip to content

Commit 8de8010

Browse files
authored
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.
1 parent 1036fb6 commit 8de8010

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/lib/db/setting-store.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ export default class SettingStore implements ISettingStore {
3232
});
3333
}
3434

35-
async insertNewRow(name: string, content: any) {
36-
return this.db(TABLE).insert({ name, content });
37-
}
38-
3935
async exists(name: string): Promise<boolean> {
4036
const result = await this.db.raw(
4137
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
@@ -58,13 +54,12 @@ export default class SettingStore implements ISettingStore {
5854
return undefined;
5955
}
6056

57+
// Is actually an upsert
6158
async insert(name: string, content: any): Promise<void> {
62-
const exists = await this.exists(name);
63-
if (exists) {
64-
await this.updateRow(name, content);
65-
} else {
66-
await this.insertNewRow(name, content);
67-
}
59+
await this.db(TABLE)
60+
.insert({ name, content })
61+
.onConflict('name')
62+
.merge();
6863
}
6964

7065
async delete(name: string): Promise<void> {

0 commit comments

Comments
 (0)