Skip to content

Commit

Permalink
improve limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Dec 4, 2024
1 parent fd92160 commit eaf1098
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
23 changes: 13 additions & 10 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 All @@ -232,7 +235,7 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === JobFilterType.Deployment)
return exists(
tx
.select({ id: releaseJobTrigger.jobId })
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.innerJoin(release, eq(releaseJobTrigger.releaseId, release.id))
.where(
Expand All @@ -246,7 +249,7 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === JobFilterType.Environment)
return exists(
tx
.select({ id: releaseJobTrigger.jobId })
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.where(
and(
Expand All @@ -259,7 +262,7 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === FilterType.Version)
return exists(
tx
.select({ id: releaseJobTrigger.jobId })
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.innerJoin(release, eq(releaseJobTrigger.releaseId, release.id))
.where(
Expand All @@ -270,7 +273,7 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === JobFilterType.JobTarget)
return exists(
tx
.select({ id: releaseJobTrigger.jobId })
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.innerJoin(resource, eq(releaseJobTrigger.resourceId, resource.id))
.where(
Expand All @@ -285,7 +288,7 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => {
if (cond.type === JobFilterType.Release)
return exists(
tx
.select({ id: releaseJobTrigger.jobId })
.select({ value: sql<number>`1` })
.from(releaseJobTrigger)
.where(
and(
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 eaf1098

Please sign in to comment.