fix: try a save point in retry

This commit is contained in:
Barış Soner Uşaklı
2026-03-28 15:09:03 -04:00
parent 991e977813
commit 203f4cc7ff

View File

@@ -45,7 +45,9 @@ DELETE FROM "legacy_object"
helpers.ensureLegacyObjectsType = async function (db, keys, type) { helpers.ensureLegacyObjectsType = async function (db, keys, type) {
keys = [...new Set(keys)]; keys = [...new Set(keys)];
if (!keys.length) {
return;
}
await db.query({ await db.query({
name: 'ensureLegacyObjectTypeBefore', name: 'ensureLegacyObjectTypeBefore',
text: ` text: `
@@ -76,11 +78,18 @@ FROM UNNEST($1::TEXT[]) k
async function tryUpsert(db, queryConfig) { async function tryUpsert(db, queryConfig) {
let res; let res;
const savepoint = `upsert_${Math.random().toString(36).substring(7)}`;
try { try {
await db.query(`SAVEPOINT ${savepoint}`);
res = await db.query(queryConfig); res = await db.query(queryConfig);
await db.query(`RELEASE SAVEPOINT ${savepoint}`);
} catch (err) { } catch (err) {
if (err.code === '23505') { // retry if failed due to error: unique constraint if (err.code === '23505') { // retry if failed due to error: unique constraint
// Roll back to the savepoint to prevent
// error: current transaction is aborted, commands ignored until end of transaction block
await db.query(`ROLLBACK TO SAVEPOINT ${savepoint}`);
res = await db.query(queryConfig); res = await db.query(queryConfig);
await db.query(`RELEASE SAVEPOINT ${savepoint}`);
} else { } else {
throw err; throw err;
} }