Skip to content

Commit 51b9243

Browse files
remove connection job table
1 parent 191435d commit 51b9243

4 files changed

Lines changed: 13 additions & 146 deletions

File tree

packages/backend/src/connectionSyncWorkload.test.ts

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { beforeEach, describe, expect, test, vi } from "vitest";
66
const mocks = vi.hoisted(() => ({
77
connectionFindUniqueOrThrow: vi.fn(),
88
connectionUpdate: vi.fn(),
9-
connectionSyncJobUpsert: vi.fn(),
10-
connectionSyncJobUpdate: vi.fn(),
119
compileGithubConfig: vi.fn(),
1210
loadConfig: vi.fn(),
1311
syncSearchContexts: vi.fn(),
@@ -99,36 +97,18 @@ import {
9997
import { reportRepositoryDiscoveryIssue } from "./repositoryDiscoveryIssueContext.js";
10098
import { REPO_PERMISSION_SYNC_WHERE } from "./ee/permissionSyncEligibility.js";
10199

102-
const transactionClient = {
103-
connection: {
104-
update: mocks.connectionUpdate,
105-
},
106-
connectionSyncJob: {
107-
upsert: mocks.connectionSyncJobUpsert,
108-
},
109-
};
110-
const transaction = vi.fn(
111-
(callback: (tx: typeof transactionClient) => Promise<unknown>) =>
112-
callback(transactionClient),
113-
);
114-
115100
const db = {
116101
connection: {
117102
findUniqueOrThrow: mocks.connectionFindUniqueOrThrow,
118103
update: mocks.connectionUpdate,
119104
},
120-
connectionSyncJob: {
121-
upsert: mocks.connectionSyncJobUpsert,
122-
update: mocks.connectionSyncJobUpdate,
123-
},
124105
repo: {
125106
findMany: mocks.repoFindMany,
126107
upsert: mocks.repoUpsert,
127108
},
128109
repoToConnection: {
129110
deleteMany: mocks.repoToConnectionDeleteMany,
130111
},
131-
$transaction: transaction,
132112
} as unknown as PrismaClient;
133113

134114
const jobManager = {
@@ -172,10 +152,10 @@ describe("connectionWorkload", () => {
172152
mocks.syncSearchContexts.mockResolvedValue(undefined);
173153
});
174154

175-
test("declares database-backed lifecycle hooks", () => {
155+
test("only records the latest job when the workload starts", () => {
176156
expect(connectionWorkload.onStarted).toBeTypeOf("function");
177-
expect(connectionWorkload.onCompleted).toBeTypeOf("function");
178-
expect(connectionWorkload.onTerminalFailure).toBeTypeOf("function");
157+
expect(connectionWorkload.onCompleted).toBeUndefined();
158+
expect(connectionWorkload.onTerminalFailure).toBeUndefined();
179159
});
180160

181161
test("uses a distinct execution lock for each connection", () => {
@@ -204,26 +184,9 @@ describe("connectionWorkload", () => {
204184
expect(mocks.connectionFindUniqueOrThrow).not.toHaveBeenCalled();
205185
});
206186

207-
test("marks the connection sync job as in progress when started", async () => {
187+
test("records the latest connection sync job ID when started", async () => {
208188
await connectionWorkload.onStarted?.(lifecycleContext);
209189

210-
expect(mocks.connectionSyncJobUpsert).toHaveBeenCalledWith({
211-
where: {
212-
id: "job-1",
213-
},
214-
update: {
215-
status: "IN_PROGRESS",
216-
completedAt: null,
217-
errorMessage: null,
218-
warningMessages: [],
219-
},
220-
create: {
221-
id: "job-1",
222-
connectionId: 42,
223-
status: "IN_PROGRESS",
224-
warningMessages: [],
225-
},
226-
});
227190
expect(mocks.connectionUpdate).toHaveBeenCalledWith({
228191
where: {
229192
id: 42,
@@ -232,42 +195,6 @@ describe("connectionWorkload", () => {
232195
latestSyncJobId: "job-1",
233196
},
234197
});
235-
expect(transaction).toHaveBeenCalledOnce();
236-
});
237-
238-
test("marks the connection sync job as completed", async () => {
239-
await connectionWorkload.onCompleted?.(lifecycleContext, {
240-
outcome: "SUCCESS",
241-
});
242-
243-
expect(mocks.connectionSyncJobUpdate).toHaveBeenCalledWith({
244-
where: {
245-
id: "job-1",
246-
},
247-
data: {
248-
status: "COMPLETED",
249-
completedAt: expect.any(Date),
250-
errorMessage: null,
251-
},
252-
});
253-
});
254-
255-
test("marks the connection sync job as failed after terminal failure", async () => {
256-
await connectionWorkload.onTerminalFailure?.(
257-
lifecycleContext,
258-
new Error("Connection credentials expired"),
259-
);
260-
261-
expect(mocks.connectionSyncJobUpdate).toHaveBeenCalledWith({
262-
where: {
263-
id: "job-1",
264-
},
265-
data: {
266-
status: "FAILED",
267-
completedAt: expect.any(Date),
268-
errorMessage: "Connection credentials expired",
269-
},
270-
});
271198
});
272199

273200
test("orchestrates discovery, persistence, and repo work reconciliation", async () => {

packages/backend/src/connectionSyncWorkload.ts

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Sentry from "@sentry/node";
2-
import { ConnectionSyncJobStatus, PrismaClient } from "@sourcebot/db";
2+
import { PrismaClient } from "@sourcebot/db";
33
import { ConnectionConfig } from "@sourcebot/schemas/v3/index.type";
44
import {
55
CONNECTION_QUEUE,
@@ -152,55 +152,12 @@ export const createConnectionSyncWorkload = ({
152152
: { outcome: "PARTIAL_SUCCESS", reasons: issues };
153153
},
154154
onStarted: async ({ data: { connectionId }, jobId }) => {
155-
await db.$transaction(async (tx) => {
156-
await tx.connectionSyncJob.upsert({
157-
where: {
158-
id: jobId,
159-
},
160-
update: {
161-
status: ConnectionSyncJobStatus.IN_PROGRESS,
162-
completedAt: null,
163-
errorMessage: null,
164-
warningMessages: [],
165-
},
166-
create: {
167-
id: jobId,
168-
connectionId,
169-
status: ConnectionSyncJobStatus.IN_PROGRESS,
170-
warningMessages: [],
171-
},
172-
});
173-
await tx.connection.update({
174-
where: {
175-
id: connectionId,
176-
},
177-
data: {
178-
latestSyncJobId: jobId,
179-
},
180-
});
181-
});
182-
},
183-
onCompleted: async ({ jobId }) => {
184-
await db.connectionSyncJob.update({
185-
where: {
186-
id: jobId,
187-
},
188-
data: {
189-
status: ConnectionSyncJobStatus.COMPLETED,
190-
completedAt: new Date(),
191-
errorMessage: null,
192-
},
193-
});
194-
},
195-
onTerminalFailure: async ({ jobId }, error) => {
196-
await db.connectionSyncJob.update({
155+
await db.connection.update({
197156
where: {
198-
id: jobId,
157+
id: connectionId,
199158
},
200159
data: {
201-
status: ConnectionSyncJobStatus.FAILED,
202-
completedAt: new Date(),
203-
errorMessage: error.message,
160+
latestSyncJobId: jobId,
204161
},
205162
});
206163
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- DropTable
2+
DROP TABLE "ConnectionSyncJob";
3+
4+
-- DropEnum
5+
DROP TYPE "ConnectionSyncJobStatus";

packages/db/prisma/schema.prisma

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ model Connection {
134134
// The type of connection (e.g., github, gitlab, etc.)
135135
connectionType ConnectionType
136136
137-
syncJobs ConnectionSyncJob[]
138137
/// When the connection was last synced successfully.
139138
syncedAt DateTime?
140139
latestSyncJobId String? /// The most recently started connection sync job.
@@ -162,27 +161,6 @@ model Connection {
162161
@@unique([name, orgId])
163162
}
164163

165-
enum ConnectionSyncJobStatus {
166-
PENDING
167-
IN_PROGRESS
168-
COMPLETED
169-
FAILED
170-
}
171-
172-
model ConnectionSyncJob {
173-
id String @id @default(cuid())
174-
status ConnectionSyncJobStatus @default(PENDING)
175-
createdAt DateTime @default(now())
176-
updatedAt DateTime @updatedAt
177-
completedAt DateTime?
178-
179-
warningMessages String[]
180-
errorMessage String?
181-
182-
connection Connection @relation(fields: [connectionId], references: [id], onDelete: Cascade)
183-
connectionId Int
184-
}
185-
186164
model RepoToConnection {
187165
addedAt DateTime @default(now())
188166

0 commit comments

Comments
 (0)