Skip to content

Commit

Permalink
fix: Revert to not using exist (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Dec 4, 2024
1 parent eaf1098 commit 3a89101
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 65 deletions.
1 change: 1 addition & 0 deletions packages/api/src/router/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const releaseRouter = createTRPCRouter({
.select()
.from(releaseJobTrigger)
.innerJoin(job, eq(releaseJobTrigger.jobId, job.id))
.innerJoin(release, eq(releaseJobTrigger.releaseId, release.id))
.innerJoin(
resource,
and(
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/router/runbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const runbookRouter = createTRPCRouter({
.where(
and(
eq(SCHEMA.runbookJobTrigger.runbookId, input.runbookId),
SCHEMA.jobMatchesCondition(ctx.db, input.filter),
SCHEMA.runbookJobMatchesCondition(ctx.db, input.filter),
),
)
.orderBy(desc(SCHEMA.job.createdAt))
Expand Down Expand Up @@ -214,7 +214,7 @@ export const runbookRouter = createTRPCRouter({
.where(
and(
eq(SCHEMA.runbookJobTrigger.runbookId, input.runbookId),
SCHEMA.jobMatchesCondition(ctx.db, input.filter),
SCHEMA.runbookJobMatchesCondition(ctx.db, input.filter),
),
)
.then(takeFirst)
Expand Down
97 changes: 34 additions & 63 deletions packages/db/src/schema/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,71 +233,33 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === FilterType.CreatedAt) return buildCreatedAtCondition(cond);
if (cond.type === JobFilterType.Status) return eq(job.status, cond.value);
if (cond.type === JobFilterType.Deployment)
return exists(
tx
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.innerJoin(release, eq(releaseJobTrigger.releaseId, release.id))
.where(
and(
eq(release.deploymentId, cond.value),
eq(releaseJobTrigger.jobId, job.id),
),
)
.limit(1),
);
return eq(release.deploymentId, cond.value);
if (cond.type === JobFilterType.Environment)
return exists(
tx
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.where(
and(
eq(releaseJobTrigger.environmentId, cond.value),
eq(releaseJobTrigger.jobId, job.id),
),
)
.limit(1),
);
if (cond.type === FilterType.Version)
return exists(
tx
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.innerJoin(release, eq(releaseJobTrigger.releaseId, release.id))
.where(
and(eq(releaseJobTrigger.jobId, job.id), buildVersionCondition(cond)),
)
.limit(1),
);
return eq(releaseJobTrigger.environmentId, cond.value);
if (cond.type === FilterType.Version) return buildVersionCondition(cond);
if (cond.type === JobFilterType.JobTarget)
return exists(
tx
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.innerJoin(resource, eq(releaseJobTrigger.resourceId, resource.id))
.where(
and(
eq(releaseJobTrigger.jobId, job.id),
eq(releaseJobTrigger.resourceId, cond.value),
isNull(resource.deletedAt),
),
)
.limit(1),
);
if (cond.type === JobFilterType.Release)
return exists(
tx
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.where(
and(
eq(releaseJobTrigger.jobId, job.id),
eq(releaseJobTrigger.releaseId, cond.value),
),
)
.limit(1),
);
return and(eq(resource.id, cond.value), isNull(resource.deletedAt))!;
if (cond.type === JobFilterType.Release) return eq(release.id, cond.value);

const subCon = cond.conditions.map((c) => buildCondition(tx, c));
const con =
cond.operator === ComparisonOperator.And ? and(...subCon)! : or(...subCon)!;
return cond.not ? not(con) : con;
};

const buildRunbookCondition = (tx: Tx, cond: JobCondition): SQL | undefined => {
if (
cond.type !== FilterType.Metadata &&
cond.type !== FilterType.CreatedAt &&
cond.type !== JobFilterType.Status &&
cond.type !== FilterType.Comparison
)
return undefined;

if (cond.type === FilterType.Metadata)
return buildMetadataCondition(tx, cond);
if (cond.type === FilterType.CreatedAt) return buildCreatedAtCondition(cond);
if (cond.type === JobFilterType.Status) return eq(job.status, cond.value);

const subCon = cond.conditions.map((c) => buildCondition(tx, c));
const con =
Expand All @@ -313,3 +275,12 @@ export function jobMatchesCondition(
? undefined
: buildCondition(tx, condition);
}

export function runbookJobMatchesCondition(
tx: Tx,
condition?: JobCondition,
): SQL<unknown> | undefined {
return condition == null || Object.keys(condition).length === 0
? undefined
: buildRunbookCondition(tx, condition);
}

0 comments on commit 3a89101

Please sign in to comment.