Skip to content

Commit c912a0b

Browse files
db migration
1 parent 3f72a53 commit c912a0b

File tree

7 files changed

+75
-55
lines changed

7 files changed

+75
-55
lines changed

packages/backend/src/repoIndexManager.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Sentry from '@sentry/node';
2-
import { PrismaClient, Repo, RepoJobStatus, RepoJobType } from "@sourcebot/db";
2+
import { PrismaClient, Repo, RepoIndexingJobStatus, RepoIndexingJobType } from "@sourcebot/db";
33
import { createLogger, Logger } from "@sourcebot/logger";
44
import { existsSync } from 'fs';
55
import { readdir, rm } from 'fs/promises';
@@ -97,7 +97,7 @@ export class RepoIndexManager {
9797
some: {
9898
AND: [
9999
{
100-
type: RepoJobType.INDEX,
100+
type: RepoIndexingJobType.INDEX,
101101
},
102102
{
103103
OR: [
@@ -108,8 +108,8 @@ export class RepoIndexManager {
108108
{
109109
status: {
110110
in: [
111-
RepoJobStatus.PENDING,
112-
RepoJobStatus.IN_PROGRESS,
111+
RepoIndexingJobStatus.PENDING,
112+
RepoIndexingJobStatus.IN_PROGRESS,
113113
]
114114
},
115115
},
@@ -123,7 +123,7 @@ export class RepoIndexManager {
123123
// Don't schedule if there are recent failed jobs (within the threshold date).
124124
{
125125
AND: [
126-
{ status: RepoJobStatus.FAILED },
126+
{ status: RepoIndexingJobStatus.FAILED },
127127
{ completedAt: { gt: thresholdDate } },
128128
]
129129
}
@@ -139,7 +139,7 @@ export class RepoIndexManager {
139139
});
140140

141141
if (reposToIndex.length > 0) {
142-
await this.createJobs(reposToIndex, RepoJobType.INDEX);
142+
await this.createJobs(reposToIndex, RepoIndexingJobType.INDEX);
143143
}
144144
}
145145

@@ -161,13 +161,13 @@ export class RepoIndexManager {
161161
some: {
162162
AND: [
163163
{
164-
type: RepoJobType.CLEANUP,
164+
type: RepoIndexingJobType.CLEANUP,
165165
},
166166
{
167167
status: {
168168
in: [
169-
RepoJobStatus.PENDING,
170-
RepoJobStatus.IN_PROGRESS,
169+
RepoIndexingJobStatus.PENDING,
170+
RepoIndexingJobStatus.IN_PROGRESS,
171171
]
172172
},
173173
},
@@ -184,15 +184,15 @@ export class RepoIndexManager {
184184
});
185185

186186
if (reposToCleanup.length > 0) {
187-
await this.createJobs(reposToCleanup, RepoJobType.CLEANUP);
187+
await this.createJobs(reposToCleanup, RepoIndexingJobType.CLEANUP);
188188
}
189189
}
190190

191-
private async createJobs(repos: Repo[], type: RepoJobType) {
191+
private async createJobs(repos: Repo[], type: RepoIndexingJobType) {
192192
// @note: we don't perform this in a transaction because
193193
// we want to avoid the situation where a job is created and run
194194
// prior to the transaction being committed.
195-
const jobs = await this.db.repoJob.createManyAndReturn({
195+
const jobs = await this.db.repoIndexingJob.createManyAndReturn({
196196
data: repos.map(repo => ({
197197
type,
198198
repoId: repo.id,
@@ -222,12 +222,12 @@ export class RepoIndexManager {
222222
logger.info(`Running ${job.data.type} job ${id} for repo ${job.data.repoName} (id: ${job.data.repoId}) (attempt ${job.attempts + 1} / ${job.maxAttempts})`);
223223

224224

225-
const { repo, type: jobType } = await this.db.repoJob.update({
225+
const { repo, type: jobType } = await this.db.repoIndexingJob.update({
226226
where: {
227227
id,
228228
},
229229
data: {
230-
status: RepoJobStatus.IN_PROGRESS,
230+
status: RepoIndexingJobStatus.IN_PROGRESS,
231231
},
232232
select: {
233233
type: true,
@@ -253,9 +253,9 @@ export class RepoIndexManager {
253253
process.on('SIGINT', signalHandler);
254254

255255
try {
256-
if (jobType === RepoJobType.INDEX) {
256+
if (jobType === RepoIndexingJobType.INDEX) {
257257
await this.indexRepository(repo, logger, abortController.signal);
258-
} else if (jobType === RepoJobType.CLEANUP) {
258+
} else if (jobType === RepoIndexingJobType.CLEANUP) {
259259
await this.cleanupRepository(repo, logger);
260260
}
261261
} finally {
@@ -370,15 +370,15 @@ export class RepoIndexManager {
370370
private onJobCompleted = async (job: Job<JobPayload>) =>
371371
groupmqLifecycleExceptionWrapper('onJobCompleted', logger, async () => {
372372
const logger = createJobLogger(job.data.jobId);
373-
const jobData = await this.db.repoJob.update({
373+
const jobData = await this.db.repoIndexingJob.update({
374374
where: { id: job.data.jobId },
375375
data: {
376-
status: RepoJobStatus.COMPLETED,
376+
status: RepoIndexingJobStatus.COMPLETED,
377377
completedAt: new Date(),
378378
}
379379
});
380380

381-
if (jobData.type === RepoJobType.INDEX) {
381+
if (jobData.type === RepoIndexingJobType.INDEX) {
382382
const repo = await this.db.repo.update({
383383
where: { id: jobData.repoId },
384384
data: {
@@ -388,7 +388,7 @@ export class RepoIndexManager {
388388

389389
logger.info(`Completed index job ${job.data.jobId} for repo ${repo.name} (id: ${repo.id})`);
390390
}
391-
else if (jobData.type === RepoJobType.CLEANUP) {
391+
else if (jobData.type === RepoIndexingJobType.CLEANUP) {
392392
const repo = await this.db.repo.delete({
393393
where: { id: jobData.repoId },
394394
});
@@ -405,10 +405,10 @@ export class RepoIndexManager {
405405
const wasLastAttempt = attempt >= job.opts.attempts;
406406

407407
if (wasLastAttempt) {
408-
const { repo } = await this.db.repoJob.update({
408+
const { repo } = await this.db.repoIndexingJob.update({
409409
where: { id: job.data.jobId },
410410
data: {
411-
status: RepoJobStatus.FAILED,
411+
status: RepoIndexingJobStatus.FAILED,
412412
completedAt: new Date(),
413413
errorMessage: job.failedReason,
414414
},
@@ -428,10 +428,10 @@ export class RepoIndexManager {
428428
private onJobStalled = async (jobId: string) =>
429429
groupmqLifecycleExceptionWrapper('onJobStalled', logger, async () => {
430430
const logger = createJobLogger(jobId);
431-
const { repo } = await this.db.repoJob.update({
431+
const { repo } = await this.db.repoIndexingJob.update({
432432
where: { id: jobId },
433433
data: {
434-
status: RepoJobStatus.FAILED,
434+
status: RepoIndexingJobStatus.FAILED,
435435
completedAt: new Date(),
436436
errorMessage: 'Job stalled',
437437
},

packages/backend/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ const createGitCloneUrlWithToken = (cloneUrl: string, credentials: { username?:
246246

247247
/**
248248
* Wraps groupmq worker lifecycle callbacks with exception handling. This prevents
249-
* uncaught exceptions (e.g., like a RepoJob not existing in the DB) from crashing
249+
* uncaught exceptions (e.g., like a RepoIndexingJob not existing in the DB) from crashing
250250
* the app.
251251
* @see: https://openpanel-dev.github.io/groupmq/api-worker/#events
252252
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `repoIndexingStatus` on the `Repo` table. All the data in the column will be lost.
5+
6+
*/
7+
-- CreateEnum
8+
CREATE TYPE "RepoIndexingJobStatus" AS ENUM ('PENDING', 'IN_PROGRESS', 'COMPLETED', 'FAILED');
9+
10+
-- CreateEnum
11+
CREATE TYPE "RepoIndexingJobType" AS ENUM ('INDEX', 'CLEANUP');
12+
13+
-- AlterTable
14+
ALTER TABLE "Repo" DROP COLUMN "repoIndexingStatus";
15+
16+
-- DropEnum
17+
DROP TYPE "RepoIndexingStatus";
18+
19+
-- CreateTable
20+
CREATE TABLE "RepoIndexingJob" (
21+
"id" TEXT NOT NULL,
22+
"type" "RepoIndexingJobType" NOT NULL,
23+
"status" "RepoIndexingJobStatus" NOT NULL DEFAULT 'PENDING',
24+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
25+
"updatedAt" TIMESTAMP(3) NOT NULL,
26+
"completedAt" TIMESTAMP(3),
27+
"errorMessage" TEXT,
28+
"repoId" INTEGER NOT NULL,
29+
30+
CONSTRAINT "RepoIndexingJob_pkey" PRIMARY KEY ("id")
31+
);
32+
33+
-- AddForeignKey
34+
ALTER TABLE "RepoIndexingJob" ADD CONSTRAINT "RepoIndexingJob_repoId_fkey" FOREIGN KEY ("repoId") REFERENCES "Repo"("id") ON DELETE CASCADE ON UPDATE CASCADE;

packages/db/prisma/schema.prisma

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ datasource db {
1010
url = env("DATABASE_URL")
1111
}
1212

13-
enum RepoIndexingStatus {
14-
NEW
15-
IN_INDEX_QUEUE
16-
INDEXING
17-
INDEXED
18-
FAILED
19-
IN_GC_QUEUE
20-
GARBAGE_COLLECTING
21-
GARBAGE_COLLECTION_FAILED
22-
}
23-
2413
enum ConnectionSyncStatus {
2514
SYNC_NEEDED
2615
IN_SYNC_QUEUE
@@ -55,14 +44,11 @@ model Repo {
5544
connections RepoToConnection[]
5645
imageUrl String?
5746
58-
/// @deprecated status tracking is now done via the `jobs` table.
59-
repoIndexingStatus RepoIndexingStatus @default(NEW)
60-
6147
permittedUsers UserToRepoPermission[]
6248
permissionSyncJobs RepoPermissionSyncJob[]
6349
permissionSyncedAt DateTime? /// When the permissions were last synced successfully.
6450
65-
jobs RepoJob[]
51+
jobs RepoIndexingJob[]
6652
indexedAt DateTime? /// When the repo was last indexed successfully.
6753
6854
external_id String /// The id of the repo in the external service
@@ -78,22 +64,22 @@ model Repo {
7864
@@index([orgId])
7965
}
8066

81-
enum RepoJobStatus {
67+
enum RepoIndexingJobStatus {
8268
PENDING
8369
IN_PROGRESS
8470
COMPLETED
8571
FAILED
8672
}
8773

88-
enum RepoJobType {
74+
enum RepoIndexingJobType {
8975
INDEX
9076
CLEANUP
9177
}
9278

93-
model RepoJob {
79+
model RepoIndexingJob {
9480
id String @id @default(cuid())
95-
type RepoJobType
96-
status RepoJobStatus @default(PENDING)
81+
type RepoIndexingJobType
82+
status RepoIndexingJobStatus @default(PENDING)
9783
createdAt DateTime @default(now())
9884
updatedAt DateTime @updatedAt
9985
completedAt DateTime?

packages/web/src/actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { prisma } from "@/prisma";
1010
import { render } from "@react-email/components";
1111
import * as Sentry from '@sentry/nextjs';
1212
import { encrypt, generateApiKey, getTokenFromConfig, hashSecret } from "@sourcebot/crypto";
13-
import { ApiKey, Org, OrgRole, Prisma, RepoJobStatus, RepoJobType, StripeSubscriptionStatus } from "@sourcebot/db";
13+
import { ApiKey, Org, OrgRole, Prisma, RepoIndexingJobStatus, RepoIndexingJobType, StripeSubscriptionStatus } from "@sourcebot/db";
1414
import { createLogger } from "@sourcebot/logger";
1515
import { GiteaConnectionConfig } from "@sourcebot/schemas/v3/gitea.type";
1616
import { GithubConnectionConfig } from "@sourcebot/schemas/v3/github.type";
@@ -586,11 +586,11 @@ export const getReposStats = async () => sew(() =>
586586
orgId: org.id,
587587
jobs: {
588588
some: {
589-
type: RepoJobType.INDEX,
589+
type: RepoIndexingJobType.INDEX,
590590
status: {
591591
in: [
592-
RepoJobStatus.PENDING,
593-
RepoJobStatus.IN_PROGRESS,
592+
RepoIndexingJobStatus.PENDING,
593+
RepoIndexingJobStatus.IN_PROGRESS,
594594
]
595595
}
596596
},

packages/web/src/app/[domain]/components/navigationMenu/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { env } from "@/env.mjs";
1010
import { ServiceErrorException } from "@/lib/serviceError";
1111
import { isServiceError } from "@/lib/utils";
1212
import { DiscordLogoIcon, GitHubLogoIcon } from "@radix-ui/react-icons";
13-
import { RepoJobStatus, RepoJobType } from "@sourcebot/db";
13+
import { RepoIndexingJobStatus, RepoIndexingJobType } from "@sourcebot/db";
1414
import Link from "next/link";
1515
import { redirect } from "next/navigation";
1616
import { OrgSelector } from "../orgSelector";
@@ -43,11 +43,11 @@ export const NavigationMenu = async ({
4343
where: {
4444
jobs: {
4545
some: {
46-
type: RepoJobType.INDEX,
46+
type: RepoIndexingJobType.INDEX,
4747
status: {
4848
in: [
49-
RepoJobStatus.PENDING,
50-
RepoJobStatus.IN_PROGRESS,
49+
RepoIndexingJobStatus.PENDING,
50+
RepoIndexingJobStatus.IN_PROGRESS,
5151
]
5252
}
5353
},

packages/web/src/app/[domain]/repos/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { env } from "@/env.mjs";
2-
import { RepoJob } from "@sourcebot/db";
2+
import { RepoIndexingJob } from "@sourcebot/db";
33
import { Header } from "../components/header";
44
import { RepoStatus } from "./columns";
55
import { RepositoryTable } from "./repositoryTable";
@@ -8,7 +8,7 @@ import { withOptionalAuthV2 } from "@/withAuthV2";
88
import { isServiceError } from "@/lib/utils";
99
import { ServiceErrorException } from "@/lib/serviceError";
1010

11-
function getRepoStatus(repo: { indexedAt: Date | null, jobs: RepoJob[] }): RepoStatus {
11+
function getRepoStatus(repo: { indexedAt: Date | null, jobs: RepoIndexingJob[] }): RepoStatus {
1212
const latestJob = repo.jobs[0];
1313

1414
if (latestJob?.status === 'PENDING' || latestJob?.status === 'IN_PROGRESS') {

0 commit comments

Comments
 (0)