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: Optimize job policy query #153

Merged
merged 2 commits into from
Oct 21, 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
30 changes: 28 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, notInArray } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import * as schema from "@ctrlplane/db/schema";
import {
Expand All @@ -9,11 +9,37 @@ import {
import { JobStatus } from "@ctrlplane/validators/jobs";

export const run = async () => {
const isPassingApprovalGate = notInArray(
schema.environmentPolicyApproval.status,
["pending", "rejected"],
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle null values in isPassingApprovalGate condition

Since environmentPolicyApproval is left-joined, schema.environmentPolicyApproval.status may be null. Using notInArray might exclude records where status is null. If the intention is to include records with null status (i.e., no approval required), consider adjusting the condition to handle null values.

You could modify isPassingApprovalGate to include null statuses:

-import { and, eq, notInArray } from "@ctrlplane/db";
+import { and, eq, notInArray, or, isNull } from "@ctrlplane/db";

 const isPassingApprovalGate = or(
   notInArray(
     schema.environmentPolicyApproval.status,
     ["pending", "rejected"],
   ),
+  isNull(schema.environmentPolicyApproval.status),
 );

This adjustment ensures that records without an environmentPolicyApproval are included in the results.

Committable suggestion was skipped due to low confidence.

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
Loading