Skip to content

Commit e362f07

Browse files
committed
refactor(webapp): enhance runsRepositoryFactory to accept options for replication and ClickHouse configuration
1 parent 2465678 commit e362f07

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

apps/webapp/app/services/runsRepositoryFactory.server.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import { RunsRepository } from "./runsRepository/runsRepository.server";
66
export function createRunsRepository(options: {
77
clickhouse: ClickHouse;
88
prisma: PrismaClient;
9+
isReplicationEnabled?: boolean;
10+
isClickHouseConfigured?: boolean;
911
}): RunsRepository {
10-
const isReplicationEnabled = env.RUN_REPLICATION_ENABLED === "1";
11-
const isClickHouseConfigured = !!env.RUN_REPLICATION_CLICKHOUSE_URL;
12+
const isReplicationEnabled = options.isReplicationEnabled ?? env.RUN_REPLICATION_ENABLED === "1";
13+
const isClickHouseConfigured = options.isClickHouseConfigured ?? !!env.RUN_REPLICATION_CLICKHOUSE_URL;
1214

1315
const defaultRepository = isReplicationEnabled && isClickHouseConfigured
1416
? "clickhouse"
1517
: "postgres";
1618

1719
return new RunsRepository({
18-
...options,
20+
clickhouse: options.clickhouse,
21+
prisma: options.prisma,
1922
defaultRepository,
2023
});
2124
}

apps/webapp/test/runsRepositoryFactory.test.ts

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
1+
import { describe, it, expect, vi } from "vitest";
2+
23
vi.mock("~/env.server", () => ({
3-
env: {},
4+
env: {
5+
RUN_REPLICATION_ENABLED: "0",
6+
RUN_REPLICATION_CLICKHOUSE_URL: undefined,
7+
DATABASE_CONNECTION_LIMIT: 10,
8+
DATABASE_POOL_TIMEOUT: 60,
9+
DATABASE_CONNECTION_TIMEOUT: 20,
10+
},
411
}));
12+
513
vi.mock("~/db.server", () => ({
614
prisma: {},
715
$replica: {},
@@ -12,69 +20,51 @@ import type { ClickHouse } from "@internal/clickhouse";
1220
import type { PrismaClient } from "@trigger.dev/database";
1321

1422
describe("createRunsRepository", () => {
15-
let mockClickhouse: ClickHouse;
16-
let mockPrisma: PrismaClient;
17-
let originalEnv: Record<string, unknown>;
18-
19-
beforeEach(async () => {
20-
const envModule = await import("~/env.server");
21-
originalEnv = { ...envModule.env };
22-
23-
mockClickhouse = {} as ClickHouse;
24-
mockPrisma = {} as PrismaClient;
25-
});
26-
27-
afterEach(async () => {
28-
const envModule = await import("~/env.server");
29-
Object.assign(envModule.env, originalEnv);
30-
});
31-
32-
it("should default to postgres when RUN_REPLICATION_ENABLED is not set", async () => {
33-
const envModule = await import("~/env.server");
34-
envModule.env.RUN_REPLICATION_ENABLED = "0";
35-
envModule.env.RUN_REPLICATION_CLICKHOUSE_URL = "http://localhost:8123";
23+
const mockClickhouse = {} as ClickHouse;
24+
const mockPrisma = {} as PrismaClient;
3625

26+
it("should default to postgres when replication is disabled", () => {
3727
const repository = createRunsRepository({
3828
clickhouse: mockClickhouse,
3929
prisma: mockPrisma,
30+
isReplicationEnabled: false,
31+
isClickHouseConfigured: true,
4032
});
4133

4234
expect(repository).toBeDefined();
4335
expect(repository.listRuns).toBeDefined();
4436
});
4537

46-
it("should default to postgres when RUN_REPLICATION_CLICKHOUSE_URL is not set", async () => {
47-
const envModule = await import("~/env.server");
48-
envModule.env.RUN_REPLICATION_ENABLED = "1";
49-
envModule.env.RUN_REPLICATION_CLICKHOUSE_URL = undefined;
50-
38+
it("should default to postgres when ClickHouse is not configured", () => {
5139
const repository = createRunsRepository({
5240
clickhouse: mockClickhouse,
5341
prisma: mockPrisma,
42+
isReplicationEnabled: true,
43+
isClickHouseConfigured: false,
5444
});
5545

5646
expect(repository).toBeDefined();
5747
expect(repository.listRuns).toBeDefined();
5848
});
5949

60-
it("should default to clickhouse when both conditions are met", async () => {
61-
const envModule = await import("~/env.server");
62-
envModule.env.RUN_REPLICATION_ENABLED = "1";
63-
envModule.env.RUN_REPLICATION_CLICKHOUSE_URL = "http://localhost:8123";
64-
50+
it("should default to clickhouse when both conditions are met", () => {
6551
const repository = createRunsRepository({
6652
clickhouse: mockClickhouse,
6753
prisma: mockPrisma,
54+
isReplicationEnabled: true,
55+
isClickHouseConfigured: true,
6856
});
6957

7058
expect(repository).toBeDefined();
7159
expect(repository.listRuns).toBeDefined();
7260
});
7361

74-
it("should create a valid RunsRepository instance", () => {
62+
it("should create a valid RunsRepository instance with all required methods", () => {
7563
const repository = createRunsRepository({
7664
clickhouse: mockClickhouse,
7765
prisma: mockPrisma,
66+
isReplicationEnabled: false,
67+
isClickHouseConfigured: false,
7868
});
7969

8070
expect(repository.listRuns).toBeDefined();

0 commit comments

Comments
 (0)