Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sizzldev/ctrlplane into revert-exis…
Browse files Browse the repository at this point in the history
…t-stuff
  • Loading branch information
adityachoudhari26 committed Dec 4, 2024
2 parents 272dc3f + eaf1098 commit 3f91d5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
13 changes: 8 additions & 5 deletions packages/db/src/schema/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
.from(jobMetadata)
.where(
and(eq(jobMetadata.jobId, job.id), eq(jobMetadata.key, cond.key)),
),
)
.limit(1),
);

if (cond.operator === MetadataOperator.Regex)
Expand All @@ -172,7 +173,8 @@ const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
eq(jobMetadata.key, cond.key),
sql`${jobMetadata.value} ~ ${cond.value}`,
),
),
)
.limit(1),
);

if (cond.operator === MetadataOperator.Like)
Expand All @@ -186,7 +188,8 @@ const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
eq(jobMetadata.key, cond.key),
like(jobMetadata.value, cond.value),
),
),
)
.limit(1),
);

return exists(
Expand All @@ -199,7 +202,8 @@ const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
eq(jobMetadata.key, cond.key),
eq(jobMetadata.value, cond.value),
),
),
)
.limit(1),
);
};

Expand All @@ -223,7 +227,6 @@ const buildVersionCondition = (cond: VersionCondition): SQL => {
return ilike(release.version, `%${cond.value}%`);
return sql`${release.version} ~ ${cond.value}`;
};

const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === FilterType.Metadata)
return buildMetadataCondition(tx, cond);
Expand Down
21 changes: 12 additions & 9 deletions packages/db/src/schema/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,60 +217,63 @@ export const releaseJobTriggerRelations = relations(
}),
}),
);

const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
if (cond.operator === MetadataOperator.Null)
return notExists(
tx
.select()
.select({ value: sql<number>`1` })
.from(releaseMetadata)
.where(
and(
eq(releaseMetadata.releaseId, release.id),
eq(releaseMetadata.key, cond.key),
),
),
)
.limit(1),
);

if (cond.operator === MetadataOperator.Regex)
return exists(
tx
.select()
.select({ value: sql<number>`1` })
.from(releaseMetadata)
.where(
and(
eq(releaseMetadata.releaseId, release.id),
eq(releaseMetadata.key, cond.key),
sql`${releaseMetadata.value} ~ ${cond.value}`,
),
),
)
.limit(1),
);

if (cond.operator === MetadataOperator.Like)
return exists(
tx
.select()
.select({ value: sql<number>`1` })
.from(releaseMetadata)
.where(
and(
eq(releaseMetadata.releaseId, release.id),
eq(releaseMetadata.key, cond.key),
like(releaseMetadata.value, cond.value),
),
),
)
.limit(1),
);

return exists(
tx
.select()
.select({ value: sql<number>`1` })
.from(releaseMetadata)
.where(
and(
eq(releaseMetadata.releaseId, release.id),
eq(releaseMetadata.key, cond.key),
eq(releaseMetadata.value, cond.value),
),
),
)
.limit(1),
);
};

Expand Down
20 changes: 12 additions & 8 deletions packages/db/src/schema/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,56 +173,60 @@ const buildMetadataCondition = (tx: Tx, cond: MetadataCondition): SQL => {
if (cond.operator === MetadataOperator.Null)
return notExists(
tx
.select()
.select({ value: sql<number>`1` })
.from(resourceMetadata)
.where(
and(
eq(resourceMetadata.resourceId, resource.id),
eq(resourceMetadata.key, cond.key),
),
),
)
.limit(1),
);

if (cond.operator === MetadataOperator.Regex)
return exists(
tx
.select()
.select({ value: sql<number>`1` })
.from(resourceMetadata)
.where(
and(
eq(resourceMetadata.resourceId, resource.id),
eq(resourceMetadata.key, cond.key),
sql`${resourceMetadata.value} ~ ${cond.value}`,
),
),
)
.limit(1),
);

if (cond.operator === MetadataOperator.Like)
return exists(
tx
.select()
.select({ value: sql<number>`1` })
.from(resourceMetadata)
.where(
and(
eq(resourceMetadata.resourceId, resource.id),
eq(resourceMetadata.key, cond.key),
like(resourceMetadata.value, cond.value),
),
),
)
.limit(1),
);

if ("value" in cond)
return exists(
tx
.select()
.select({ value: sql<number>`1` })
.from(resourceMetadata)
.where(
and(
eq(resourceMetadata.resourceId, resource.id),
eq(resourceMetadata.key, cond.key),
eq(resourceMetadata.value, cond.value),
),
),
)
.limit(1),
);

throw Error("invalid metadata conditions");
Expand Down

0 comments on commit 3f91d5a

Please sign in to comment.