Skip to content

Commit

Permalink
fix: Optimize job policy query (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Oct 21, 2024
1 parent e510b04 commit 5358ede
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions apps/jobs/src/policy-checker/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eq } from "@ctrlplane/db";
import { and, eq, isNull, or } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import * as schema from "@ctrlplane/db/schema";
import {
Expand All @@ -9,11 +9,38 @@ import {
import { JobStatus } from "@ctrlplane/validators/jobs";

export const run = async () => {
const isPassingApprovalGate = or(
isNull(schema.environment.policyId),
eq(schema.environmentPolicy.approvalRequirement, "automatic"),
eq(schema.environmentPolicyApproval.status, "approved"),
);

const releaseJobTriggers = await db
.select()
.from(schema.releaseJobTrigger)
.innerJoin(schema.job, eq(schema.releaseJobTrigger.jobId, schema.job.id))
.where(eq(schema.job.status, JobStatus.Pending))
.innerJoin(
schema.environment,
eq(schema.releaseJobTrigger.environmentId, schema.environment.id),
)
.leftJoin(
schema.environmentPolicy,
eq(schema.environment.policyId, schema.environmentPolicy.id),
)
.leftJoin(
schema.environmentPolicyApproval,
and(
eq(
schema.environmentPolicyApproval.policyId,
schema.environmentPolicy.id,
),
eq(
schema.environmentPolicyApproval.releaseId,
schema.releaseJobTrigger.releaseId,
),
),
)
.where(and(eq(schema.job.status, JobStatus.Pending), isPassingApprovalGate))
.then((rows) => rows.map((row) => row.release_job_trigger));

if (releaseJobTriggers.length === 0) return;
Expand Down

0 comments on commit 5358ede

Please sign in to comment.