Skip to content

Commit

Permalink
Add a test for cached CodecContext invalidation; fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Jan 11, 2025
1 parent 1a24217 commit cefb89f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/driver/src/baseConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ export class BaseRawConnection {
}

case chars.$s: {
// The state descriptor has change, a modification might have
// been applied to the schema, let's reset codec contexts.
Options.signalSchemaChange();

this._parseDescribeStateMessage();
break;
}
Expand Down Expand Up @@ -780,8 +784,8 @@ export class BaseRawConnection {
to learn
*/
if (
(outCodec !== NULL_CODEC && outCodec.tid != newOutCodec.tid) ||
(inCodec !== NULL_CODEC && inCodec.tid != newInCodec.tid)
(outCodec !== NULL_CODEC && outCodec.tid !== newOutCodec.tid) ||
(inCodec !== NULL_CODEC && inCodec.tid !== newInCodec.tid)
) {
Options.signalSchemaChange();

Expand All @@ -806,7 +810,7 @@ export class BaseRawConnection {
outCodec = newOutCodec;
warnings = _warnings;
} catch (e: any) {
// An error happened, so we don't know if we bumped the internal
// An error happened, so we don't know if we did bump the internal
// schema tracker or not, so let's do it again to be on the safe
// side.
Options.signalSchemaChange();
Expand Down
54 changes: 54 additions & 0 deletions packages/driver/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,60 @@ if (getEdgeDBVersion().major >= 5) {
}
});
});

test("codec context invalidation", async () => {
const query = `SELECT <CodecInv_01>'123'`;

const client = getClient().withCodecs({
"std::str": {
toDatabase() {
throw "not implemented";
},
fromDatabase(val) {
return { str: val };
},
},
"std::int32": {
toDatabase() {
throw "not implemented";
},
fromDatabase(val) {
return { int: val };
},
},
});

try {
await client.transaction(async (tx) => {
await tx.execute(`
CREATE SCALAR TYPE CodecInv_01 EXTENDING std::str;
`);

let ret = await tx.querySingle(query);
expect(ret).toStrictEqual({ str: "123" });

await tx.execute(`
DROP SCALAR TYPE CodecInv_01;
CREATE SCALAR TYPE CodecInv_01 EXTENDING std::int32;
`);

// If CodecContext wasn't invalidated and the previous one got
// used to after running the above DDL, we'll have ret == {str: '123'},
// because we'd still think that the appropriate codec for
// 'CodecInv_01' would be one for 'std::str'.
ret = await tx.querySingle(query);
expect(ret).toStrictEqual({ int: 123 });

throw new CancelTransaction();
});
} catch (e) {
if (!(e instanceof CancelTransaction)) {
throw e;
}
} finally {
await client.close();
}
});
}

if (getEdgeDBVersion().major >= 6) {
Expand Down

0 comments on commit cefb89f

Please sign in to comment.