Skip to content

Commit cefb89f

Browse files
committed
Add a test for cached CodecContext invalidation; fix code
1 parent 1a24217 commit cefb89f

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

packages/driver/src/baseConn.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ export class BaseRawConnection {
627627
}
628628

629629
case chars.$s: {
630+
// The state descriptor has change, a modification might have
631+
// been applied to the schema, let's reset codec contexts.
632+
Options.signalSchemaChange();
633+
630634
this._parseDescribeStateMessage();
631635
break;
632636
}
@@ -780,8 +784,8 @@ export class BaseRawConnection {
780784
to learn
781785
*/
782786
if (
783-
(outCodec !== NULL_CODEC && outCodec.tid != newOutCodec.tid) ||
784-
(inCodec !== NULL_CODEC && inCodec.tid != newInCodec.tid)
787+
(outCodec !== NULL_CODEC && outCodec.tid !== newOutCodec.tid) ||
788+
(inCodec !== NULL_CODEC && inCodec.tid !== newInCodec.tid)
785789
) {
786790
Options.signalSchemaChange();
787791

@@ -806,7 +810,7 @@ export class BaseRawConnection {
806810
outCodec = newOutCodec;
807811
warnings = _warnings;
808812
} catch (e: any) {
809-
// An error happened, so we don't know if we bumped the internal
813+
// An error happened, so we don't know if we did bump the internal
810814
// schema tracker or not, so let's do it again to be on the safe
811815
// side.
812816
Options.signalSchemaChange();

packages/driver/test/client.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,60 @@ if (getEdgeDBVersion().major >= 5) {
25202520
}
25212521
});
25222522
});
2523+
2524+
test("codec context invalidation", async () => {
2525+
const query = `SELECT <CodecInv_01>'123'`;
2526+
2527+
const client = getClient().withCodecs({
2528+
"std::str": {
2529+
toDatabase() {
2530+
throw "not implemented";
2531+
},
2532+
fromDatabase(val) {
2533+
return { str: val };
2534+
},
2535+
},
2536+
"std::int32": {
2537+
toDatabase() {
2538+
throw "not implemented";
2539+
},
2540+
fromDatabase(val) {
2541+
return { int: val };
2542+
},
2543+
},
2544+
});
2545+
2546+
try {
2547+
await client.transaction(async (tx) => {
2548+
await tx.execute(`
2549+
CREATE SCALAR TYPE CodecInv_01 EXTENDING std::str;
2550+
`);
2551+
2552+
let ret = await tx.querySingle(query);
2553+
expect(ret).toStrictEqual({ str: "123" });
2554+
2555+
await tx.execute(`
2556+
DROP SCALAR TYPE CodecInv_01;
2557+
CREATE SCALAR TYPE CodecInv_01 EXTENDING std::int32;
2558+
`);
2559+
2560+
// If CodecContext wasn't invalidated and the previous one got
2561+
// used to after running the above DDL, we'll have ret == {str: '123'},
2562+
// because we'd still think that the appropriate codec for
2563+
// 'CodecInv_01' would be one for 'std::str'.
2564+
ret = await tx.querySingle(query);
2565+
expect(ret).toStrictEqual({ int: 123 });
2566+
2567+
throw new CancelTransaction();
2568+
});
2569+
} catch (e) {
2570+
if (!(e instanceof CancelTransaction)) {
2571+
throw e;
2572+
}
2573+
} finally {
2574+
await client.close();
2575+
}
2576+
});
25232577
}
25242578

25252579
if (getEdgeDBVersion().major >= 6) {

0 commit comments

Comments
 (0)