keep createSession raw writes on primary when replicas are enabled
This commit is contained in:
1 parent
7a176e10bc
commit
4c4071b270
3 files changed
+148
-10
No files matched your search
@@ -0,0 +1,82 @@
|
||||
import { beforeAll, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
process.env.DATABASE_URL ??= 'postgresql://user:pass@localhost:5432/umami?schema=public';
|
||||
delete process.env.DATABASE_REPLICA_URL;
|
||||
|
||||
vi.mock('@prisma/adapter-pg', () => ({
|
||||
PrismaPg: class PrismaPg {},
|
||||
}));
|
||||
|
||||
vi.mock('@prisma/extension-read-replicas', () => ({
|
||||
readReplicas: () => () => ({}),
|
||||
}));
|
||||
|
||||
vi.mock('@/generated/prisma/client', () => ({
|
||||
PrismaClient: class PrismaClient {
|
||||
$executeRawUnsafe = vi.fn();
|
||||
$queryRawUnsafe = vi.fn();
|
||||
$transaction = vi.fn();
|
||||
$on = vi.fn();
|
||||
$extends() {
|
||||
return this;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
let getRawQueryClient!: typeof import('./prisma').getRawQueryClient;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ getRawQueryClient } = await import('./prisma'));
|
||||
});
|
||||
|
||||
interface RawQueryClient {
|
||||
$executeRawUnsafe: (query: string, ...params: any[]) => unknown;
|
||||
$queryRawUnsafe: (query: string, ...params: any[]) => unknown;
|
||||
$primary?: () => unknown;
|
||||
$replica?: () => unknown;
|
||||
}
|
||||
|
||||
function createClient(): RawQueryClient {
|
||||
return {
|
||||
$executeRawUnsafe: vi.fn(),
|
||||
$queryRawUnsafe: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
describe('getRawQueryClient', () => {
|
||||
test('uses a replica client for read queries when replicas are enabled', () => {
|
||||
const replica = createClient();
|
||||
const client = {
|
||||
...createClient(),
|
||||
$replica: vi.fn(() => replica),
|
||||
};
|
||||
|
||||
expect(getRawQueryClient(client, { useReplica: true })).toBe(replica);
|
||||
});
|
||||
|
||||
test('keeps read queries on the primary client when replicas are disabled', () => {
|
||||
const client = {
|
||||
...createClient(),
|
||||
$replica: vi.fn(() => createClient()),
|
||||
};
|
||||
|
||||
expect(getRawQueryClient(client, { useReplica: false })).toBe(client);
|
||||
});
|
||||
|
||||
test('uses the primary client for raw writes when available', () => {
|
||||
const primary = createClient();
|
||||
const client = {
|
||||
...createClient(),
|
||||
$primary: vi.fn(() => primary),
|
||||
$replica: vi.fn(() => createClient()),
|
||||
};
|
||||
|
||||
expect(getRawQueryClient(client, { useReplica: true, write: true })).toBe(primary);
|
||||
});
|
||||
|
||||
test('falls back to the current client for raw writes without a primary helper', () => {
|
||||
const client = createClient();
|
||||
|
||||
expect(getRawQueryClient(client, { write: true })).toBe(client);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user