mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 13:01:05 +01:00
fix(db): count transaction statements in execution order before replaying a transaction
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f9b5ddc93b
commit
9cb04d631d
2 files changed
+40
-17
No files matched your search
@@ -225,17 +225,43 @@ describe.skipIf(!support.supported)("explicit ORM prepared statements on Postgre
|
||||
}
|
||||
});
|
||||
|
||||
async function occupyNextStatementName(db: Db) {
|
||||
const names = (await statements(db, "%")).map(row => String(row.name));
|
||||
const prefix = names.find(name => /\d$/.test(name) && !name.includes(" "))!.replace(/\d+$/, "");
|
||||
const counters = names.filter(name => name.startsWith(prefix)).map(name => Number(name.slice(prefix.length)));
|
||||
await db.$client.unsafe(`prepare "${prefix}${Math.max(...counters) + 1}" as select 1`, [], { prepare: false });
|
||||
}
|
||||
|
||||
it("retries a pooled statement unprepared when its prepared statement cannot be created", async () => {
|
||||
const db = fixture({ prepare: true });
|
||||
await statements(db, "%");
|
||||
const names = (await statements(db, "%")).map(row => String(row.name));
|
||||
const prefix = names[0].replace(/\d+$/, "");
|
||||
const next = `${prefix}${Math.max(...names.map(name => Number(name.slice(prefix.length)))) + 1}`;
|
||||
await db.$client.unsafe(`prepare "${next}" as select 1`, [], { prepare: false });
|
||||
await occupyNextStatementName(db);
|
||||
expect(await db.select({ value: records.value }).from(records).where(eq(records.id, "b"))).toEqual([{ value: 2 }]);
|
||||
await occupyNextStatementName(db);
|
||||
expect(await db.$client.unsafe("select $1::integer as raw_retry_marker", [3]).values()).toEqual([[3]]);
|
||||
expect(await statements(db)).toHaveLength(0);
|
||||
expect(preparedStatementStats(db)).toMatchObject({ retried: 1, demoted: 1 });
|
||||
expect(await statements(db, "%raw_retry_marker%")).toHaveLength(0);
|
||||
expect(preparedStatementStats(db)).toMatchObject({ retried: 2, demoted: 2 });
|
||||
});
|
||||
|
||||
it("does not replay a transaction when a query created first executes after another statement", async () => {
|
||||
const db = fixture({ prepare: true });
|
||||
const read = "select id, value from prepared_stale_fixture where id = $1";
|
||||
expect(await db.$client.unsafe(read, ["stale"])).toEqual([{ id: "stale", value: 5 }]);
|
||||
await changeStaleValueType("bigint");
|
||||
let calls = 0;
|
||||
try {
|
||||
await expect(db.$client.begin(async tx => {
|
||||
calls++;
|
||||
const saved = tx.unsafe(read, ["stale"]);
|
||||
await tx.unsafe("select $1::integer as ordering_marker", [1]);
|
||||
return await saved;
|
||||
})).rejects.toMatchObject({ code: "0A000" });
|
||||
expect(calls).toBe(1);
|
||||
expect(preparedStatementStats(db)).toMatchObject({ retriedTransactions: 0, demoted: 1 });
|
||||
} finally {
|
||||
await changeStaleValueType("integer");
|
||||
}
|
||||
});
|
||||
|
||||
it("prepares at most the configured number of distinct texts and skips long texts", async () => {
|
||||
|
||||
@@ -88,23 +88,20 @@ export function installPreparedQueries(client: unknown, state: PreparedQueryStat
|
||||
return Reflect.apply(unsafe, this, args);
|
||||
}
|
||||
if (index === 2 && args.length < 2) args[1] = [];
|
||||
const position = ++scope.statements;
|
||||
const text = args[0];
|
||||
if (typeof text !== "string" || (options !== undefined && "prepare" in options)) {
|
||||
args[index] = { prepare: true, ...options };
|
||||
return Reflect.apply(unsafe, this, args);
|
||||
}
|
||||
args[index] = { ...options };
|
||||
args[index] = { prepare: true, ...options };
|
||||
if (typeof text !== "string") return Reflect.apply(unsafe, this, args);
|
||||
const query = Reflect.apply(unsafe, this, args) as PendingQuery;
|
||||
if (query.options.simple) {
|
||||
query.options.prepare = true;
|
||||
return query;
|
||||
}
|
||||
let explicit: unknown;
|
||||
let explicit: unknown = (options !== undefined && "prepare" in options) || query.options.simple
|
||||
? query.options.prepare : undefined;
|
||||
let position: number | undefined;
|
||||
Object.defineProperty(query.options, "prepare", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
get: () => explicit ?? shouldPrepare(state, text),
|
||||
get: () => {
|
||||
position ??= ++scope.statements;
|
||||
return explicit ?? shouldPrepare(state, text);
|
||||
},
|
||||
set: (value: unknown) => { explicit = value; },
|
||||
});
|
||||
const reject = query.reject;
|
||||
|
||||
Reference in new issue
Block a user