Skip to content

Commit f17a6f3

Browse files
committed
fix(pgsql-test): fix clearContext() to properly clear all session variables
- Add contextSettings field to track all set context variables - Modify setContext() to merge into tracked state before building SQL - Fix clearContext() to NULL all tracked variables before setting default role - Ensures clearContext() actually clears variables like jwt.claims.user_id Co-Authored-By: Dan Lynch <pyramation@gmail.com>
1 parent e9ae46d commit f17a6f3

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

packages/pgsql-test/src/test-client.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class PgTestClient {
1313
public config: PgConfig;
1414
public client: Client;
1515
private ctxStmts: string = '';
16+
private contextSettings: Record<string, string | null> = {};
1617
private _ended: boolean = false;
1718
private connectPromise: Promise<void> | null = null;
1819

@@ -77,7 +78,9 @@ export class PgTestClient {
7778
}
7879

7980
setContext(ctx: Record<string, string | null>): void {
80-
this.ctxStmts = Object.entries(ctx)
81+
Object.assign(this.contextSettings, ctx);
82+
83+
this.ctxStmts = Object.entries(this.contextSettings)
8184
.map(([key, val]) =>
8285
val === null
8386
? `SELECT set_config('${key}', NULL, true);`
@@ -120,7 +123,23 @@ export class PgTestClient {
120123
*/
121124
clearContext(): void {
122125
const defaultRole = getRoleName('anonymous', (this.config as any).roles);
123-
this.setContext({ role: defaultRole });
126+
127+
const nulledSettings: Record<string, string | null> = {};
128+
Object.keys(this.contextSettings).forEach(key => {
129+
nulledSettings[key] = null;
130+
});
131+
132+
nulledSettings.role = defaultRole;
133+
134+
this.ctxStmts = Object.entries(nulledSettings)
135+
.map(([key, val]) =>
136+
val === null
137+
? `SELECT set_config('${key}', NULL, true);`
138+
: `SELECT set_config('${key}', '${val}', true);`
139+
)
140+
.join('\n');
141+
142+
this.contextSettings = { role: defaultRole };
124143
}
125144

126145
async any<T = any>(query: string, values?: any[]): Promise<T[]> {

0 commit comments

Comments
 (0)