Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Revert to not using exist #252

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Revert to not using exist
  • Loading branch information
adityachoudhari26 committed Dec 4, 2024
commit 272dc3f414358083da6c5615a041c8f30be8029f
1 change: 1 addition & 0 deletions packages/api/src/router/release.ts
Original file line number Diff line number Diff line change
@@ -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(
4 changes: 2 additions & 2 deletions packages/api/src/router/runbook.ts
Original file line number Diff line number Diff line change
@@ -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))
@@ -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)
97 changes: 34 additions & 63 deletions packages/db/src/schema/job.ts
Original file line number Diff line number Diff line change
@@ -230,71 +230,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({ id: releaseJobTrigger.jobId })
.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({ id: releaseJobTrigger.jobId })
.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({ id: releaseJobTrigger.jobId })
.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({ id: releaseJobTrigger.jobId })
.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({ id: releaseJobTrigger.jobId })
.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 =
@@ -310,3 +272,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);
}