Skip to content

db efficiency improvements #1657

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

Merged
merged 1 commit into from
Feb 3, 2025
Merged

db efficiency improvements #1657

merged 1 commit into from
Feb 3, 2025

Conversation

ericallam
Copy link
Member

@ericallam ericallam commented Feb 3, 2025

  • remove calls to findUnique, especially when including/selecting relations
  • add some missing indexes
  • add spans to $transaction calls to help track down long running txs

Summary by CodeRabbit

  • Refactor
    • Unified data retrieval across various flows to return the first matching record, ensuring more robust behavior when multiple entries exist.
    • Enhanced transaction tracing with descriptive labels for improved diagnostics.
  • Chores
    • Introduced multiple database indexes to optimize performance and responsiveness.
    • Streamlined import structures to simplify maintenance.
  • Bug Fixes
    • Improved error handling for alert delivery and background operations, resulting in more resilient processing.

- remove calls to findUnique, especially when including/selecting relations
- add some missing indexes
- add spans to $transaction calls to help track down long running txs
Copy link
Contributor

coderabbitai bot commented Feb 3, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/webapp/app/db.server.ts

Oops! Something went wrong! :(

ESLint: 8.45.0

ESLint couldn't find the config "custom" to extend from. Please check that the name of the config is correct.

The config "custom" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

apps/webapp/app/presenters/v3/ApiBatchResultsPresenter.server.ts

Oops! Something went wrong! :(

ESLint: 8.45.0

ESLint couldn't find the config "custom" to extend from. Please check that the name of the config is correct.

The config "custom" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

apps/webapp/app/presenters/v3/EnvironmentVariablesPresenter.server.ts

Oops! Something went wrong! :(

ESLint: 8.45.0

ESLint couldn't find the config "custom" to extend from. Please check that the name of the config is correct.

The config "custom" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

  • 42 others

Walkthrough

The changes update various database interactions throughout the codebase. In the core database module, the $transaction function now supports an overload accepting a string and a tracing span for enhanced observability. Across multiple presenters, routes, and service files, calls to methods like findUnique (or findUniqueOrThrow) have been replaced with their findFirst (or findFirstOrThrow) variants. Several service methods have been updated with descriptive transaction labels and span enhancements. Additionally, migration scripts and the Prisma schema have been modified to add new indexes on multiple tables for improved query performance.

Changes

File(s) Change Summary
apps/webapp/app/db.server.ts Enhanced $transaction function: added new overloads accepting a string and a span parameter to enable tracing.
apps/webapp/app/presenters/v3/*
(ApiBatchResults, ApiRunResult, Deployment, EnvironmentVariables, NewAlertChannel, RunList, RunStream, Span, TasksStream)
Replaced findUnique/findUniqueOrThrow with findFirst/findFirstOrThrow in database queries for flexible record retrieval.
apps/webapp/app/routes/api.v1.*
(deployments, projects)
Updated queries from findUnique to findFirst for retrieving deployment and project records.
apps/webapp/app/services/autoIncrementCounter.server.ts Removed the unused $transaction import and consolidated import statements.
apps/webapp/app/v3/services/*
(alerts, batch, cancel, complete, create, requeue, enqueue, reschedule, restore, trigger, upsert, etc.)
Modified query methods from findUnique to findFirst, added descriptive transaction labels (e.g., "create attempt", "cancel attempt", "reschedule run"), and integrated tracing spans where applicable.
internal-packages/database/prisma/migrations/* Added new SQL index creation commands on various columns (e.g., in BatchTaskRun, CheckpointRestoreEvent, TaskRunDependency, BackgroundWorker, BackgroundWorkerTask) for performance improvements.
internal-packages/database/prisma/schema.prisma Updated model definitions by adding indexes (e.g., on BackgroundWorker, BackgroundWorkerTask, TaskRunDependency, BatchTaskRun, CheckpointRestoreEvent) to optimize query performance.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant DBServer
    participant Prisma
    Client->>DBServer: Call $transaction(fnOrName, fnOrOptions, options)
    alt String overload provided
        DBServer->>DBServer: Start a tracing span using provided string
    else Function overload provided
        DBServer->>DBServer: Execute transaction function directly
    end
    DBServer->>Prisma: Execute DB queries (using findFirst variants)
    Prisma-->>DBServer: Return query result
    DBServer-->>Client: Return transaction result with span context
Loading

Possibly related PRs

Suggested reviewers

  • matt-aitken

Poem

I’m a little rabbit hopping with glee,
Tracing transactions as clear as can be.
Queries now fetch the very first sight,
With indexes added, performance is bright.
Bugs and errors, now chased away,
In the code’s garden, we joyfully play.
🐰🌟 Hop on for cleaner code today!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Feb 3, 2025

@trigger.dev/build

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/build@1657

@trigger.dev/react-hooks

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/react-hooks@1657

trigger.dev

npm i https://pkg.pr.new/triggerdotdev/trigger.dev@1657

@trigger.dev/rsc

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/rsc@1657

@trigger.dev/sdk

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/sdk@1657

@trigger.dev/core

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/core@1657

commit: bdf5ae8

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (6)
apps/webapp/app/v3/services/cancelTaskAttemptDependencies.server.ts (1)

9-27: Consider keeping findUnique for primary key lookup.

Since we're querying by id (likely a primary key), using findUnique would be more appropriate and potentially more efficient than findFirst.

-    const taskAttempt = await this._prisma.taskRunAttempt.findFirst({
+    const taskAttempt = await this._prisma.taskRunAttempt.findUnique({

Additionally, consider optimizing the deep includes by selecting only necessary fields.

apps/webapp/app/v3/services/alerts/performDeploymentAlerts.server.ts (1)

10-15: Consider keeping findUnique for primary key lookup.

Since we're querying by id (likely a primary key), using findUnique would be more appropriate and potentially more efficient than findFirst.

-    const deployment = await this._prisma.workerDeployment.findFirst({
+    const deployment = await this._prisma.workerDeployment.findUnique({
apps/webapp/app/v3/services/expireEnqueuedRun.server.ts (1)

22-34: Consider keeping findUnique for primary key lookup.

Since we're querying by id (likely a primary key), using findUnique would be more appropriate and potentially more efficient than findFirst.

-    const run = await this._prisma.taskRun.findFirst({
+    const run = await this._prisma.taskRun.findUnique({

Additionally, consider optimizing the deep includes by selecting only necessary fields.

apps/webapp/app/presenters/v3/TasksStreamPresenter.server.ts (1)

36-51: Subject: Potential Issue: Missing Uniqueness Enforcement for Project Slug within Organization

The investigation did not reveal any unique constraint (neither a Prisma @@unique([slug, organizationId]) nor a database unique index) governing the combination of projectSlug and organizationSlug. This implies that using findFirst might return unpredictable (or non-deterministic) results if duplicate records exist.

  • No unique constraint is found in the Prisma schema.
  • No unique indexes are present in the migration files.

Please review and enforce a uniqueness constraint if appropriate, or adjust the logic and related database validations accordingly.

🔗 Analysis chain

Verify uniqueness constraints for project slug within organization.

The change from findUnique to findFirst assumes that the combination of projectSlug and organizationSlug is unique. Please verify this assumption to prevent potential issues with returning unexpected projects.


I'm not seeing conclusive evidence from the first command. Let's run additional scripts to verify that the database (or Prisma schema) enforces a unique constraint for the combination of project slug and organization.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if there are any duplicate project slugs within the same organization
ast-grep --pattern 'CREATE UNIQUE INDEX $_ ON "Project" ("slug", "organizationId")'

Length of output: 85


Script:

#!/bin/bash
echo "Searching for unique constraints in the Prisma schema (e.g., @@unique([slug, organizationId])):"
rg '@@unique\(\[.*slug.*organizationId.*\]\)' -n .

echo ""
echo "Searching for unique index definitions in migration files:"
rg 'CREATE UNIQUE INDEX' -n prisma/migrations || echo "No unique indexes found in migrations"

Length of output: 582

apps/webapp/app/v3/services/createCheckpointRestoreEvent.server.ts (1)

31-35: Use findUnique for unique identifier queries.
The Prisma schema confirms that both the id and friendlyId fields are defined as unique (with @id and @unique, respectively). Since the queried field (id) is unique, it is more appropriate and semantically correct to use findUnique rather than findFirst.

  • File: apps/webapp/app/v3/services/createCheckpointRestoreEvent.server.ts (Lines 31-35 and 47-59)
  • Recommendation: Replace findFirst queries with findUnique when querying by these unique identifiers.
🔗 Analysis chain

Verify if findFirst is the optimal choice for querying by unique identifiers.

While the change from findUnique to findFirst aligns with the PR's efficiency improvements, both queries use what appear to be unique identifiers (id and friendlyId). If these fields have unique constraints, findUnique might be more appropriate.

Let's verify the schema constraints:

Also applies to: 47-59

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if id and friendlyId are unique fields in the schema
# Test: Search for unique constraints on these fields in the Prisma schema
# Expected: Find unique constraints for both fields

rg -A 5 'model (Checkpoint|TaskRun)' | rg -A 10 '(id|friendlyId).*@(id|unique)'

Length of output: 3336

apps/webapp/app/v3/marqs/devQueueConsumer.server.ts (1)

84-89: Review Verification Result: Missing Unique Constraints Detected

Our investigation could not locate any unique constraint definitions enforcing the combination of friendlyId and runtimeEnvironmentId for the BackgroundWorker model or enforcing uniqueness of friendlyId in the TaskRunAttempt model in the Prisma schema located at ./references/v3-catalog/prisma/schema/schema.prisma. This absence implies that using findFirst for record retrieval may return non-unique results if duplicates exist.

  • BackgroundWorker: No evidence was found that the combination of friendlyId and runtimeEnvironmentId is enforced as unique.
  • TaskRunAttempt: No evidence was found that friendlyId is enforced as unique.

Please review and, if appropriate, add the necessary unique constraints to ensure data integrity.

🔗 Analysis chain

Verify uniqueness constraints for background worker and task run attempt retrieval.

The changes from findUnique to findFirst could potentially return unexpected records if multiple entries match the criteria. Ensure that:

  1. The combination of friendlyId and runtimeEnvironmentId maintains uniqueness for background workers
  2. The friendlyId maintains uniqueness for task run attempts

Run the following script to check for potential duplicates:

Also applies to: 173-175


Please run this script to verify that:

  • The BackgroundWorker model enforces uniqueness on the combination of friendlyId and runtimeEnvironmentId.
  • The TaskRunAttempt model enforces uniqueness on friendlyId.

If no unique constraints are present, or if the definitions differ from your expectations, then further action may be required.


Please run the above script to gather further details on whether:

  • The BackgroundWorker model enforces uniqueness on the combination of friendlyId and runtimeEnvironmentId.
  • The TaskRunAttempt model enforces uniqueness on friendlyId.

Based on the output, we can determine if the current usage of findFirst in the code is safe or if the schema is missing the expected constraints.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for unique constraints on background workers and task run attempts

# Test: Search for unique constraint or index definitions
ast-grep --pattern $'model BackgroundWorker {
  $$$
  @@unique([$_])
  $$$
}'

ast-grep --pattern $'model TaskRunAttempt {
  $$$
  @@unique([$_])
  $$$
}'

Length of output: 155


Script:

#!/bin/bash
# Description: Locate the prisma schema file and check unique constraints for the BackgroundWorker
#            and TaskRunAttempt models to verify that uniqueness constraints exist as expected.

# Find the prisma schema file (typically named schema.prisma)
schema_file=$(fd "schema.prisma" -n)
echo "Schema file: $schema_file"

echo ""
echo "=== Checking BackgroundWorker model for unique constraints (combination of friendlyId and runtimeEnvironmentId) ==="
rg -A 50 'model BackgroundWorker' "$schema_file" | grep -E '@(unique|@@unique)'

echo ""
echo "=== Checking TaskRunAttempt model for unique constraints (friendlyId) ==="
rg -A 50 'model TaskRunAttempt' "$schema_file" | grep -E '@(unique|@@unique)'

Length of output: 945


Script:

#!/bin/bash
# Description: Locate the prisma schema file using the "find" command and print out the model definitions for
# BackgroundWorker and TaskRunAttempt to manually verify that unique constraints exist where expected.
#
# This script avoids using fd flags that cause errors and directly uses find to locate the schema file.

# Locate the prisma schema file
schema_file=$(find . -type f -name "schema.prisma" | head -n1)
if [ -z "$schema_file" ]; then
  echo "schema.prisma file not found."
  exit 1
fi
echo "Schema file: $schema_file"
echo ""

# Print the BackgroundWorker model definition (until the next model starts)
echo "=== BackgroundWorker Model Definition ==="
awk '/model BackgroundWorker/{flag=1} flag && /^model / && !/model BackgroundWorker/ {flag=0} flag {print}' "$schema_file"
echo ""

# Print the TaskRunAttempt model definition (until the next model starts)
echo "=== TaskRunAttempt Model Definition ==="
awk '/model TaskRunAttempt/{flag=1} flag && /^model / && !/model TaskRunAttempt/ {flag=0} flag {print}' "$schema_file"

Length of output: 843


Script:

#!/bin/bash
# Description: Print the full definitions of the BackgroundWorker and TaskRunAttempt models
# and search for any unique constraint indicators (both inline @unique and block @@unique)
# in the Prisma schema file to verify that the expected uniqueness constraints are in place.

# Locate the prisma schema file
schema_file=$(find . -type f -name "schema.prisma" | head -n1)
if [ -z "$schema_file" ]; then 
  echo "schema.prisma file not found."
  exit 1
fi
echo "Schema file: $schema_file"
echo ""

# Print full model definition for BackgroundWorker (from its declaration until the next model declaration)
echo "=== Full BackgroundWorker Model Definition ==="
sed -n '/model BackgroundWorker/,/model /p' "$schema_file"
echo ""

# Print full model definition for TaskRunAttempt (from its declaration until the next model declaration)
echo "=== Full TaskRunAttempt Model Definition ==="
sed -n '/model TaskRunAttempt/,/model /p' "$schema_file"
echo ""

# Search for unique constraint indicators in the entire schema file
echo "=== Searching for unique constraint indicators ('@unique' and '@@unique') ==="
rg '@(unique|@@unique)' "$schema_file"

Length of output: 1003

🧹 Nitpick comments (11)
internal-packages/database/prisma/migrations/20250203133220_add_missing_checkpoint_restore_event_checkpoint_id_index/migration.sql (1)

1-1: Comment Clarity and Context
The comment -- CreateIndex is simple and informs the reader that an index is being created. If further detail is available about the performance improvements expected from this index, consider adding a bit more context in the comment.

apps/webapp/app/v3/services/createDeployedBackgroundWorker.server.ts (1)

40-98: Consider using a transaction for atomic operations.

The background worker creation and deployment update are separate database operations. Consider wrapping them in a transaction to ensure atomicity and reduce the number of database round trips.

Here's how you could refactor this:

-      const backgroundWorker = await this._prisma.backgroundWorker.create({
-        data: {
-          friendlyId: generateFriendlyId("worker"),
-          version: deployment.version,
-          runtimeEnvironmentId: environment.id,
-          projectId: environment.projectId,
-          metadata: body.metadata,
-          contentHash: body.metadata.contentHash,
-          cliVersion: body.metadata.cliPackageVersion,
-          sdkVersion: body.metadata.packageVersion,
-          supportsLazyAttempts: body.supportsLazyAttempts,
-        },
-      });
+      const backgroundWorker = await this._prisma.$transaction(async (tx) => {
+        const worker = await tx.backgroundWorker.create({
+          data: {
+            friendlyId: generateFriendlyId("worker"),
+            version: deployment.version,
+            runtimeEnvironmentId: environment.id,
+            projectId: environment.projectId,
+            metadata: body.metadata,
+            contentHash: body.metadata.contentHash,
+            cliVersion: body.metadata.cliPackageVersion,
+            sdkVersion: body.metadata.packageVersion,
+            supportsLazyAttempts: body.supportsLazyAttempts,
+          },
+        });
+
+        await tx.workerDeployment.update({
+          where: {
+            id: deployment.id,
+          },
+          data: {
+            status: "DEPLOYED",
+            workerId: worker.id,
+            deployedAt: new Date(),
+          },
+        });
+
+        return worker;
+      }, {
+        label: "create_background_worker_and_update_deployment",
+      });
apps/webapp/app/db.server.ts (1)

37-95: Enhance error handling in nested transactions.
If a nested transaction or additional logic throws during the active span, ensure these errors are caught and traced properly. You might also log them within the span itself for more robust diagnostics.

apps/webapp/app/v3/services/rescheduleTaskRun.server.ts (1)

20-20: LGTM! Consider adding more span attributes.

The addition of the transaction name "reschedule run" improves traceability. Consider adding more span attributes like run_id and delay for better debugging context.

-    return await $transaction(this._prisma, "reschedule run", async (tx) => {
+    return await $transaction(this._prisma, "reschedule run", async (tx, span) => {
+      span?.setAttribute("run_id", taskRun.id);
+      span?.setAttribute("delay", delay.toISOString());
apps/webapp/app/presenters/v3/ApiBatchResultsPresenter.server.ts (1)

12-32: LGTM! Consider adding an index for the query.

The switch from findUnique to findFirst aligns with the PR objectives. Consider adding a composite index on (friendlyId, runtimeEnvironmentId) to optimize this query.

Add the following index to your Prisma schema:

model BatchTaskRun {
  @@index([friendlyId, runtimeEnvironmentId])
}
apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.ts (1)

29-38: LGTM! Consider adding an index for the query.

The switch from findUnique to findFirst aligns with the PR objectives. Consider adding a composite index on (externalRef) with an include on the environments relation to optimize this query.

Add the following index to your Prisma schema:

model Project {
  @@index([externalRef])
}
apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (1)

8-16: LGTM! Consider adding an index for the query.

The switch from findUnique to findFirst aligns with the PR objectives. Consider adding an index on id to optimize this query.

Add the following index to your Prisma schema:

model TaskScheduleInstance {
  @@index([id])
}
apps/webapp/app/v3/services/upsertTaskSchedule.server.ts (1)

39-50: Consider using unique constraints for schedule lookups.

The switch from findUnique to findFirst suggests there might be multiple records matching either friendlyId or the combination of projectId and deduplicationKey. This could lead to inconsistencies if multiple schedules exist with the same identifiers.

Consider adding unique constraints in the database schema:

  1. A unique constraint on friendlyId if it's meant to be globally unique.
  2. A composite unique constraint on (projectId, deduplicationKey) if they should be unique within a project.
apps/webapp/app/v3/services/createCheckpoint.server.ts (1)

33-50: Consider using unique constraints for attempt lookups.

The switch from findUnique to findFirst suggests there might be multiple attempts with the same friendlyId. This could lead to inconsistencies in checkpoint creation if multiple attempts exist with the same identifier.

Consider adding a unique constraint on friendlyId in the database schema if it's meant to be globally unique across attempts.

apps/webapp/app/presenters/v3/SpanPresenter.server.ts (1)

32-36: Consider using unique constraints for project lookups.

The switch from findUnique to findFirst suggests there might be multiple projects with the same slug. This could lead to inconsistencies if multiple projects exist with the same slug.

Consider adding a unique constraint on slug in the database schema if it's meant to be globally unique across projects.

apps/webapp/app/presenters/v3/RunListPresenter.server.ts (1)

134-145: Consider using unique constraints for bulk action lookups.

The switch from findUnique to findFirst suggests there might be multiple bulk actions with the same friendlyId. This could lead to inconsistencies if multiple bulk actions exist with the same identifier.

Consider adding a unique constraint on friendlyId in the database schema if it's meant to be globally unique across bulk actions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b67ea9a and bdf5ae8.

📒 Files selected for processing (53)
  • apps/webapp/app/db.server.ts (2 hunks)
  • apps/webapp/app/presenters/v3/ApiBatchResultsPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/ApiRunResultPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/DeploymentPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/EnvironmentVariablesPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/NewAlertChannelPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/RunListPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/RunStreamPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/SpanPresenter.server.ts (1 hunks)
  • apps/webapp/app/presenters/v3/TasksStreamPresenter.server.ts (1 hunks)
  • apps/webapp/app/routes/api.v1.deployments.$deploymentId.ts (1 hunks)
  • apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.ts (1 hunks)
  • apps/webapp/app/routes/api.v1.projects.$projectRef.ts (1 hunks)
  • apps/webapp/app/services/autoIncrementCounter.server.ts (1 hunks)
  • apps/webapp/app/v3/environmentVariables/environmentVariablesRepository.server.ts (13 hunks)
  • apps/webapp/app/v3/marqs/devQueueConsumer.server.ts (2 hunks)
  • apps/webapp/app/v3/requeueTaskRun.server.ts (1 hunks)
  • apps/webapp/app/v3/services/alerts/createAlertChannel.server.ts (1 hunks)
  • apps/webapp/app/v3/services/alerts/deliverAlert.server.ts (1 hunks)
  • apps/webapp/app/v3/services/alerts/performDeploymentAlerts.server.ts (2 hunks)
  • apps/webapp/app/v3/services/alerts/performTaskAttemptAlerts.server.ts (2 hunks)
  • apps/webapp/app/v3/services/alerts/performTaskRunAlerts.server.ts (1 hunks)
  • apps/webapp/app/v3/services/batchTriggerV3.server.ts (3 hunks)
  • apps/webapp/app/v3/services/bulk/performBulkAction.server.ts (2 hunks)
  • apps/webapp/app/v3/services/cancelAttempt.server.ts (3 hunks)
  • apps/webapp/app/v3/services/cancelDevSessionRuns.server.ts (1 hunks)
  • apps/webapp/app/v3/services/cancelTaskAttemptDependencies.server.ts (1 hunks)
  • apps/webapp/app/v3/services/completeAttempt.server.ts (2 hunks)
  • apps/webapp/app/v3/services/createBackgroundWorker.server.ts (1 hunks)
  • apps/webapp/app/v3/services/createCheckpoint.server.ts (1 hunks)
  • apps/webapp/app/v3/services/createCheckpointRestoreEvent.server.ts (2 hunks)
  • apps/webapp/app/v3/services/createDeployedBackgroundWorker.server.ts (1 hunks)
  • apps/webapp/app/v3/services/createDeploymentBackgroundWorker.server.ts (1 hunks)
  • apps/webapp/app/v3/services/createTaskRunAttempt.server.ts (2 hunks)
  • apps/webapp/app/v3/services/deploymentIndexFailed.server.ts (1 hunks)
  • apps/webapp/app/v3/services/enqueueDelayedRun.server.ts (2 hunks)
  • apps/webapp/app/v3/services/expireEnqueuedRun.server.ts (1 hunks)
  • apps/webapp/app/v3/services/indexDeployment.server.ts (1 hunks)
  • apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (2 hunks)
  • apps/webapp/app/v3/services/rescheduleTaskRun.server.ts (1 hunks)
  • apps/webapp/app/v3/services/restoreCheckpoint.server.ts (1 hunks)
  • apps/webapp/app/v3/services/timeoutDeployment.server.ts (1 hunks)
  • apps/webapp/app/v3/services/triggerScheduledTask.server.ts (1 hunks)
  • apps/webapp/app/v3/services/triggerTask.server.ts (6 hunks)
  • apps/webapp/app/v3/services/upsertTaskSchedule.server.ts (2 hunks)
  • internal-packages/database/prisma/migrations/20250203132941_add_missing_batch_task_run_dependent_task_attempt_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/migrations/20250203133220_add_missing_checkpoint_restore_event_checkpoint_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/migrations/20250203133324_add_missing_checkpoint_restore_event_run_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/migrations/20250203133511_add_missing_task_run_dependency_dependent_attempt_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/migrations/20250203133619_add_missing_task_run_dependency_dependent_batch_run_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/migrations/20250203133835_add_missing_background_worker_runtime_environment_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/migrations/20250203134009_add_missing_background_worker_task_runtime_environment_id_project_id_index/migration.sql (1 hunks)
  • internal-packages/database/prisma/schema.prisma (5 hunks)
✅ Files skipped from review due to trivial changes (7)
  • internal-packages/database/prisma/migrations/20250203134009_add_missing_background_worker_task_runtime_environment_id_project_id_index/migration.sql
  • internal-packages/database/prisma/migrations/20250203133835_add_missing_background_worker_runtime_environment_id_index/migration.sql
  • internal-packages/database/prisma/migrations/20250203132941_add_missing_batch_task_run_dependent_task_attempt_id_index/migration.sql
  • internal-packages/database/prisma/migrations/20250203133324_add_missing_checkpoint_restore_event_run_id_index/migration.sql
  • apps/webapp/app/v3/services/alerts/performTaskRunAlerts.server.ts
  • apps/webapp/app/services/autoIncrementCounter.server.ts
  • internal-packages/database/prisma/migrations/20250203133511_add_missing_task_run_dependency_dependent_attempt_id_index/migration.sql
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (45)
internal-packages/database/prisma/migrations/20250203133220_add_missing_checkpoint_restore_event_checkpoint_id_index/migration.sql (1)

2-2: Index Creation with CONCURRENTLY and IF NOT EXISTS
The SQL statement correctly creates the index concurrently and only if it does not already exist, which helps avoid locking the table unnecessarily. However, note that in PostgreSQL the CREATE INDEX CONCURRENTLY command must be run outside of a transaction block. Ensure that your migration runner or tool is configured accordingly to avoid runtime errors.

internal-packages/database/prisma/migrations/20250203133619_add_missing_task_run_dependency_dependent_batch_run_id_index/migration.sql (1)

1-2: Verify Transaction Context for CONCURRENTLY Usage.

The index is created using CREATE INDEX CONCURRENTLY IF NOT EXISTS, which cannot run inside a transaction block in PostgreSQL. Please ensure that your migration runner or system executes this statement outside of any surrounding transaction to avoid runtime errors. Additionally, verify that combining IF NOT EXISTS with CONCURRENTLY is supported on your target PostgreSQL version.

apps/webapp/app/v3/services/createDeployedBackgroundWorker.server.ts (1)

26-30: Verify uniqueness constraint and index on friendlyId.

The change from findUnique to findFirst aligns with the PR's goal of improving database efficiency. However, since friendlyId appears to be a unique identifier, we should ensure it has the appropriate database constraints and indexes for optimal query performance.

Let's verify the schema definition and indexes:

✅ Verification successful

Verified: The Prisma schema confirms that friendlyId is unique and indexed.

The WorkerDeployment model in internal-packages/database/prisma/schema.prisma defines the friendlyId field with the @unique constraint, ensuring both uniqueness and an implicit index. This supports the change from findUnique to findFirst without performance concerns.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if friendlyId has uniqueness constraint and index in the schema

# Search for the WorkerDeployment model definition and any indexes on friendlyId
rg -A 10 "model\s+WorkerDeployment\s*{" 

# Search for any existing index definitions on friendlyId
rg "@@index\(\[.*friendlyId.*\]\)"
rg "@@unique\(\[.*friendlyId.*\]\)"

Length of output: 236


Script:

#!/bin/bash
# Description: Check for the WorkerDeployment model and inspect the configuration for friendlyId in the Prisma schema.

# Locate the prisma schema file (assuming it is located at prisma/schema.prisma)
if [ -f prisma/schema.prisma ]; then
  echo "Found prisma/schema.prisma, searching for WorkerDeployment model..."
  rg -nH -A 10 'model\s+WorkerDeployment\s*\{' prisma/schema.prisma

  echo -e "\nSearching for 'friendlyId' references in prisma/schema.prisma..."
  rg -nH 'friendlyId' prisma/schema.prisma
else
  echo "Could not locate prisma/schema.prisma. Please verify the schema file location."
fi

Length of output: 200


Script:

#!/bin/bash
# Description: Search for all prisma schema files in the repository and inspect them for the WorkerDeployment model
# and any references to friendlyId, including potential uniqueness constraints and indexes.
echo "Searching for 'schema.prisma' files in the repository..."
fd --type file 'schema.prisma'

echo -e "\nInspecting any discovered schema.prisma file(s) for WorkerDeployment model and friendlyId references..."
for file in $(fd --type file 'schema.prisma'); do
    echo -e "\nFound schema file: $file"
    echo "-------------------------------"
    echo "WorkerDeployment model in $file:"
    rg -nH -A 10 'model\s+WorkerDeployment\s*\{' "$file"
    echo "-------------------------------"
    echo "References to friendlyId in $file:"
    rg -nH 'friendlyId' "$file"
    echo "-------------------------------"
done

Length of output: 4532

apps/webapp/app/v3/services/alerts/createAlertChannel.server.ts (1)

53-59: Ensure uniqueness or clarify multi-record handling.
Switching from findUnique to findFirst can potentially allow multiple records with the same deduplicationKey to exist in the database. If you need strict uniqueness, consider enforcing a unique constraint or fallback strategy to handle additional records.

Would you like a script to scan for multiple deduplicationKey entries in the database and confirm no duplication is occurring?

apps/webapp/app/db.server.ts (2)

16-17: Commendable approach to integrate tracing.
Bringing in startActiveSpan and Span helps add valuable observability to transaction flows.


26-31: Consider returning a strict type instead of Promise<R | undefined>.
The function signature’s Promise<R | undefined> can lead to confusion — if fn must return R, then returning undefined might be unexpected. Evaluate if it should be Promise<R> or ensure a valid reason for handling undefined.

apps/webapp/app/presenters/v3/ApiRunResultPresenter.server.ts (1)

12-24: Confirm intended behavior with findFirst.
Replacing findUnique with findFirst allows multiple possible matches, returning effectively the “first” record. If there can be multiple tasks for the same friendlyId and runtimeEnvironmentId, this is fine. Otherwise, consider a uniqueness index or confirm that only one such record could ever exist.

apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (1)

22-38: LGTM! Great addition of span tracing.

The implementation of span tracing with detailed attributes will help track down performance issues in timestamp calculations.

apps/webapp/app/v3/services/timeoutDeployment.server.ts (1)

9-16: LGTM! Performance improvement by using findFirst

Good change! Using findFirst instead of findUnique when including relations is more efficient as it avoids an extra uniqueness check, while still returning the same result when querying by id.

apps/webapp/app/v3/services/enqueueDelayedRun.server.ts (2)

10-22: LGTM! Performance improvement by using findFirst

Good change! Using findFirst instead of findUnique when including relations is more efficient as it avoids an extra uniqueness check, while still returning the same result when querying by id.


40-40: LGTM! Added transaction span for better observability

Good addition of the transaction span label. This will help track long-running transactions and identify potential bottlenecks.

apps/webapp/app/v3/services/deploymentIndexFailed.server.ts (1)

26-34: LGTM! Consistent use of findFirst

Good change! While this query doesn't include relations, using findFirst maintains consistency with other database queries and still provides the same result when querying by unique identifiers (id or friendlyId).

apps/webapp/app/routes/api.v1.deployments.$deploymentId.ts (1)

30-42: LGTM! Significant performance improvement for deeply nested relations

Excellent change! Using findFirst instead of findUnique is particularly beneficial here due to the deeply nested relations (worker.tasks). This avoids expensive uniqueness checks while maintaining the same result when querying by the composite key (friendlyId + environmentId).

apps/webapp/app/routes/api.v1.projects.$projectRef.ts (1)

30-46: Verify index coverage for the complex query.

The switch to findFirst might impact query performance, especially with the complex where conditions and included relations. Let's verify the index coverage.

apps/webapp/app/v3/services/alerts/performDeploymentAlerts.server.ts (1)

48-48: LGTM! Good addition of the transaction label.

The descriptive transaction label "create and send deploy alert" improves tracing capabilities.

apps/webapp/app/v3/services/alerts/performTaskAttemptAlerts.server.ts (2)

16-23: LGTM! The change from findUnique to findFirst is safe here.

Since we're querying by id which is unique, this change aligns with the PR's efficiency improvements without risking data integrity.


49-49: Great addition of the transaction label!

The descriptive label "create and send attempt alert" will help in tracking long-running transactions.

apps/webapp/app/presenters/v3/NewAlertChannelPresenter.server.ts (1)

11-15: LGTM! Safe transition to findFirstOrThrow.

The change maintains the same behavior since we're querying by id which is unique.

apps/webapp/app/v3/services/cancelDevSessionRuns.server.ts (1)

72-77: LGTM! Safe transition to findFirst.

Both queries use unique identifiers (friendlyId and id), making this change safe while improving database efficiency.

apps/webapp/app/presenters/v3/RunStreamPresenter.server.ts (1)

24-31: Consider using findUnique for querying by friendlyId.

If friendlyId is a unique field in the TaskRun model (which it appears to be), using findUnique would be more appropriate and potentially more efficient as it can utilize unique indexes.

Let's verify the schema:

✅ Verification successful

Switch to findUnique for querying by friendlyId
The Prisma schema confirms that friendlyId is declared as a unique field in the TaskRun model (internal-packages/database/prisma/schema.prisma), so using findUnique will be both semantically correct and more efficient.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if friendlyId is a unique field in TaskRun model
# Test: Search for unique constraint on friendlyId in the Prisma schema
# Expected: Find unique constraint for friendlyId

rg -A 5 'model TaskRun' | rg -A 10 'friendlyId.*@unique'

Length of output: 1280

apps/webapp/app/v3/services/cancelAttempt.server.ts (2)

52-52: LGTM! Good addition of the transaction label.

Adding the descriptive label "cancel attempt" improves observability and helps in tracking long-running transactions, which aligns with the PR objectives.


31-38: Consider using findUnique for TaskRunAttempt queries.

Both queries use friendlyId which appears to be a unique identifier. If this field has a unique constraint, using findUnique would be more appropriate.

Let's verify the schema:

Also applies to: 96-108

✅ Verification successful

Action: Replace findFirst with findUnique for TaskRunAttempt queries

The Prisma schema confirms that the friendlyId field in the TaskRunAttempt model is marked as unique. You should update both query instances (lines 31-38 and 96-108) in apps/webapp/app/v3/services/cancelAttempt.server.ts to use findUnique for clarity and efficiency.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if friendlyId is a unique field in TaskRunAttempt model
# Test: Search for unique constraint on friendlyId in the Prisma schema
# Expected: Find unique constraint for friendlyId

rg -A 5 'model TaskRunAttempt' | rg -A 10 'friendlyId.*@unique'

Length of output: 190

apps/webapp/app/v3/services/createDeploymentBackgroundWorker.server.ts (1)

23-27: Consider using findUnique for WorkerDeployment query.

The query uses friendlyId which appears to be a unique identifier. If this field has a unique constraint, using findUnique would be more appropriate.

Let's verify the schema:

✅ Verification successful

Update Query in WorkerDeployment Service

The review comment is valid. The friendlyId field in the WorkerDeployment model is marked as @unique in the Prisma schema (see internal-packages/database/prisma/schema.prisma). This confirms that using findUnique is more appropriate than findFirst.

  • File: apps/webapp/app/v3/services/createDeploymentBackgroundWorker.server.ts (Lines 23-27)
  • Action: Replace findFirst with findUnique for querying by friendlyId.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if friendlyId is a unique field in WorkerDeployment model
# Test: Search for unique constraint on friendlyId in the Prisma schema
# Expected: Find unique constraint for friendlyId

rg -A 5 'model WorkerDeployment' | rg -A 10 'friendlyId.*@unique'

Length of output: 638

apps/webapp/app/v3/services/bulk/performBulkAction.server.ts (2)

96-101: LGTM! Change aligns with DB efficiency improvements.

The switch from findUnique to findFirst when including relations is consistent with the PR's objective to improve database efficiency.

Let's verify the uniqueness constraints are in place:

✅ Verification successful

Unique constraint verified in Prisma schema.

The Prisma schema located at internal-packages/database/prisma/schema.prisma confirms the unique constraint on the id field of the BulkActionGroup model with the declaration id String @id @default(cuid()). This verifies the related changes and aligns with the intended DB efficiency improvements.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if unique constraints exist for bulkActionGroup.id
# Expected: Should find a unique constraint on the id field

ast-grep --pattern 'model BulkActionGroup {
  $$$
  id String @id $$$
  $$$
}'

Length of output: 80


Script:

#!/bin/bash
# Search for the BulkActionGroup model definition and check for the unique id declaration in Prisma schema files
rg "model BulkActionGroup" -A 50 -- "*.prisma" | grep -E "id\s+\S+\s+@id" || echo "Unique constraint not found in BulkActionGroup model"

Length of output: 239


Script:

#!/bin/bash
# Search for the BulkActionGroup model definition and check for the unique id declaration across all files,
# excluding common build folders.
rg "model BulkActionGroup" -A 30 --glob "!.git" --glob "!node_modules" --glob "!dist" | grep -E "id\s+String\s+@id" || echo "Unique constraint not found in BulkActionGroup model"

Length of output: 198


10-17: LGTM! Change aligns with DB efficiency improvements.

The switch from findUnique to findFirst when including relations is consistent with the PR's objective to improve database efficiency.

Let's verify the uniqueness constraints are in place:

✅ Verification successful

Unique Constraint Confirmed on BulkActionItem.id

The Prisma schema in ./internal-packages/database/prisma/schema.prisma shows that the BulkActionItem model’s id field is defined with the @id attribute (and a @default(cuid())). This confirms the uniqueness constraint is in place, validating the switch from findUnique to findFirst with relation inclusions for the intended DB efficiency improvements.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if unique constraints exist for bulkActionItem.id
# Expected: Should find a unique constraint on the id field

ast-grep --pattern 'model BulkActionItem {
  $$$
  id String @id $$$
  $$$
}'

Length of output: 79


Script:

#!/bin/bash
# Re-check the BulkActionItem model for an id field with a unique constraint
# using a more flexible pattern to account for potential variations (e.g., default values).
ast-grep --pattern 'model BulkActionItem {
  $$$
  id $_ @id $$$
  $$$
}'

Length of output: 75


Script:

#!/bin/bash
# Locate the Prisma schema file containing the BulkActionItem model and display its context including the id field and its constraints.
FILE=$(rg -l "model BulkActionItem" -n . | head -n 1)
if [ -f "$FILE" ]; then
  echo "Found schema file: $FILE"
  rg -C 5 "model BulkActionItem" "$FILE"
else
  echo "BulkActionItem model not found in the repository."
fi

Length of output: 645

apps/webapp/app/v3/services/indexDeployment.server.ts (1)

11-18: LGTM! Change aligns with DB efficiency improvements.

The switch from findUnique to findFirst when including relations is consistent with the PR's objective to improve database efficiency. The error handling for null results is properly maintained.

Let's verify the uniqueness constraints are in place:

✅ Verification successful

Verification Successful: Unique Constraint Confirmed

  • The Prisma schema in internal-packages/database/prisma/schema.prisma shows the WorkerDeployment model with the line id String @id @default(cuid()), confirming the unique constraint on the id field.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if unique constraints exist for workerDeployment.id
# Expected: Should find a unique constraint on the id field

ast-grep --pattern 'model WorkerDeployment {
  $$$
  id String @id $$$
  $$$
}'

Length of output: 81


Script:

#!/bin/bash
# New verification: Search for the WorkerDeployment model definition in our Prisma schema files 
# to confirm that the "id" field has the unique constraint (@id).

rg "model\s+WorkerDeployment" --context 20

Length of output: 6335

apps/webapp/app/presenters/v3/EnvironmentVariablesPresenter.server.ts (1)

18-32: LGTM! Change aligns with DB efficiency improvements.

The switch from findUnique to findFirst with complex relations is consistent with the PR's objective to improve database efficiency. The error handling is well-maintained with a clear error message.

Let's verify the uniqueness constraints are in place:

apps/webapp/app/v3/services/restoreCheckpoint.server.ts (1)

16-44: LGTM! Change aligns with DB efficiency improvements.

The switch from findUnique to findFirst with deeply nested relations is consistent with the PR's objective to improve database efficiency. The extensive error handling for various scenarios is properly maintained.

Let's verify the uniqueness constraints are in place:

✅ Verification successful

I'll run a new script using rg to extract the full block for the CheckpointRestoreEvent model and check for the unique constraint on the id field.


Verification confirmed: The unique constraint on CheckpointRestoreEvent.id is present.

  • The Prisma schema (internal-packages/database/prisma/schema.prisma) defines the CheckpointRestoreEvent model with id String @id @default(cuid()), ensuring uniqueness.
  • The switch from findUnique to findFirst aligns with the database efficiency improvements, as the unique constraint on id remains valid.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if unique constraints exist for checkpointRestoreEvent.id
# Expected: Should find a unique constraint on the id field

ast-grep --pattern 'model CheckpointRestoreEvent {
  $$$
  id String @id $$$
  $$$
}'

Length of output: 87


Script:

#!/bin/bash
# Search for the full model block for CheckpointRestoreEvent with context to verify the unique constraint on the id field (id String @id)
rg -Pzo "model\\s+CheckpointRestoreEvent\\s*\\{(.*?)\\}" . | tr '\0' '\n'

Length of output: 71


Script:

#!/bin/bash
# Attempt to locate the Prisma schema file and extract the model block for CheckpointRestoreEvent
FILE=$(fd -HI "schema.prisma" | head -n 1)
if [ -z "$FILE" ]; then
    echo "schema.prisma not found. Searching entire repository for model CheckpointRestoreEvent block..."
    rg -N "model CheckpointRestoreEvent" -A 100 .
else
    echo "Found schema file: $FILE"
    echo "Extracting model definition for CheckpointRestoreEvent (showing up to 100 lines following the declaration)..."
    awk '/model CheckpointRestoreEvent {/,/}/' "$FILE"
fi

Length of output: 1646

apps/webapp/app/v3/requeueTaskRun.server.ts (1)

14-34: Verify uniqueness of task run IDs.

The change from findUnique to findFirst assumes task run IDs are unique. Please ensure there's a unique constraint in the schema to prevent duplicate task runs.

Run the following script to verify the schema constraint:

apps/webapp/app/v3/services/triggerScheduledTask.server.ts (1)

15-29: Verify uniqueness of task schedule instance IDs.

The change from findUnique to findFirst assumes task schedule instance IDs are unique. Please ensure there's a unique constraint in the schema to prevent duplicate instances.

Run the following script to verify the schema constraint:

✅ Verification successful

Let's perform additional checks by re-inspecting the Prisma schema to verify that the task schedule instance ID is enforced as unique. We'll run two scripts: one to display the full definition of the TaskScheduleInstance model (to manually inspect the unique or primary key constraints) and another to specifically search for an id field annotated with @id (which implies uniqueness).


TaskScheduleInstance Uniqueness Verified
The schema clearly defines the id field as the primary key with @id @default(cuid()), ensuring that each task schedule instance is unique. No changes are necessary.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for unique constraint on taskScheduleInstance table.
# Expect: A unique constraint on id.

ast-grep --pattern $'model TaskScheduleInstance {
  $$$
  @@unique([$_])
  $$$
}'

Length of output: 82


Script:

#!/bin/bash
# Script 1: Display the full definition of the TaskScheduleInstance model with context.
# This should help confirm how the unique constraint is implemented.
rg "model TaskScheduleInstance" -C 20 --glob "*.prisma" || true

Length of output: 3054


Script:

#!/bin/bash
# Script 2: Check if the TaskScheduleInstance model has an 'id' field using the @id attribute.
# The presence of @id indicates that the field is a primary key and therefore unique.
ast-grep --pattern $'model TaskScheduleInstance { $$$ id $_ @id $$$ }' || true

Length of output: 71

apps/webapp/app/v3/services/createTaskRunAttempt.server.ts (2)

133-133: LGTM! Improved transaction tracing.

Adding the descriptive label "create attempt" to the transaction improves observability and makes it easier to track long-running transactions.


47-84: Verify uniqueness of task run lookup.

The change from findUnique to findFirst assumes task run IDs and friendly IDs are unique within an environment. Please ensure there's a unique constraint in the schema to prevent duplicates.

Run the following script to verify the schema constraint:

✅ Verification successful

I'm generating an additional shell script to inspect the Prisma schema for the TaskRun model definition—including any unique constraints—to manually verify that a unique constraint exists. Please run the script below and share its output:


Task Run Lookup is Backed by Proper Unique Constraints.
The Prisma schema confirms that:

  • The task run's id field is a primary key, ensuring uniqueness.
  • The friendlyId field is marked with @unique, so it is unique on its own.
  • Although the query filters by runtimeEnvironmentId as well, the uniqueness is ensured by the individual constraints on id and friendlyId.

No composite unique constraint with runtimeEnvironmentId is needed since each field used in the lookup is globally unique.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for unique constraint on taskRun table.
# Expect: A unique constraint on id, friendlyId, and runtimeEnvironmentId.

ast-grep --pattern $'model TaskRun {
  $$$
  @@unique([$_])
  $$$
}'

Length of output: 69


Script:

#!/bin/bash
# This script locates Prisma schema files and extracts the TaskRun model definition,
# including any @@unique constraints, so we can verify uniqueness.
files=$(fd -t f "schema.prisma")
if [ -z "$files" ]; then
  echo "No schema.prisma file found."
  exit 1
fi

for file in $files; do
  echo "=== File: $file ==="
  sed -n '/model TaskRun {/,/}/p' "$file"
  echo ""
done

Length of output: 5833

apps/webapp/app/v3/services/upsertTaskSchedule.server.ts (1)

95-142: LGTM! Enhanced transaction observability.

The addition of the span name "UpsertTaskSchedule.upsertNewSchedule" improves transaction tracing, aligning with the PR's objective to implement spans in $transaction calls.

apps/webapp/app/v3/services/createBackgroundWorker.server.ts (1)

30-30: Verify uniqueness constraints for project retrieval.

The change from findUniqueOrThrow to findFirstOrThrow could potentially return a different project if multiple projects match the criteria. Ensure that the combination of externalRef and environments.id maintains uniqueness.

Run the following script to check for potential duplicate projects:

✅ Verification successful

Uniqueness Constraint Verified for Project Retrieval

The schema confirms that the Project model uses a unique constraint on the externalRef field (see externalRef String @unique in the Prisma schema). With this constraint, even though the code now uses findFirstOrThrow, only one project record can ever match the externalRef criterion. There is no composite uniqueness on [externalRef, environments.id], but since externalRef is unique by itself, it safeguards against duplicate project retrieval.

  • The Project model in internal-packages/database/prisma/schema.prisma shows externalRef marked as unique.
  • No composite index on [externalRef, environments.id] was found, which indicates that relying on the externalRef uniqueness is intentional.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential duplicate projects that could match the same criteria

# Test: Search for unique constraint or index definitions on projects table
ast-grep --pattern $'model Project {
  $$$
  @@unique([$_])
  $$$
}'

# Test: Search for projects with the same externalRef
rg -A 5 $'model Project'

Length of output: 1937

apps/webapp/app/v3/services/completeAttempt.server.ts (1)

609-618: Verify uniqueness constraints for environment and task run attempt retrieval.

The changes from findUnique to findFirst could potentially return unexpected records if multiple entries match the criteria. Ensure that:

  1. The id field maintains uniqueness for runtime environments
  2. The friendlyId maintains uniqueness for task run attempts

Run the following script to check for potential duplicates:

Also applies to: 622-635

✅ Verification successful

Uniqueness constraints verified in Prisma schema.

  • The RuntimeEnvironment model uses id as its primary key, which guarantees uniqueness.
  • The TaskRunAttempt model has a unique constraint on friendlyId.

The evidence confirms that using findFirst in these retrievals is safe given that duplication is prevented by the schema definitions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for unique constraints on runtime environments and task run attempts

# Test: Search for unique constraint or index definitions
ast-grep --pattern $'model RuntimeEnvironment {
  $$$
  @@unique([$_])
  $$$
}'

ast-grep --pattern $'model TaskRunAttempt {
  $$$
  @@unique([$_])
  $$$
}'

Length of output: 157


Script:

#!/bin/bash
# This script attempts to list all Prisma schema files and then show the model definitions
# for RuntimeEnvironment and TaskRunAttempt, so we can verify the uniqueness annotations.

echo "Listing all Prisma schema files:"
fd -e prisma

echo ""
echo "Searching for the model RuntimeEnvironment definition and its fields:"
rg "model RuntimeEnvironment" -N -A 30

echo ""
echo "Searching for the model TaskRunAttempt definition and its fields:"
rg "model TaskRunAttempt" -N -A 30

Length of output: 8159

apps/webapp/app/v3/environmentVariables/environmentVariablesRepository.server.ts (2)

139-139: LGTM! Improved transaction spans with descriptive labels.

The addition of descriptive strings to $transaction calls enhances observability by providing better context for tracing and debugging:

  • "create env var"
  • "edit env var"
  • "delete env var"
  • "delete env var value"

Also applies to: 283-283, 527-527, 620-620


60-84: Verify uniqueness constraints for project and environment variable retrieval.

The changes from findUnique to findFirst could potentially return unexpected records if multiple entries match the criteria. Ensure that:

  1. The id field maintains uniqueness for projects
  2. The combination of projectId and key maintains uniqueness for environment variables

Run the following script to check for potential duplicates:

Also applies to: 269-277, 291-296

✅ Verification successful

Uniqueness constraints verified in the Prisma schema.

  • The Project model uses an id field marked as @id (with a default cuid()), ensuring its uniqueness.
  • The EnvironmentVariable model enforces the unique combination of projectId and key via @@unique([projectId, key]).

Given the schema confirms these constraints, switching from findUnique to findFirst does not introduce risks of duplicate data retrieval.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for unique constraints on projects and environment variables

# Test: Search for unique constraint or index definitions
ast-grep --pattern $'model Project {
  $$$
  @@unique([$_])
  $$$
}'

ast-grep --pattern $'model EnvironmentVariable {
  $$$
  @@unique([$_])
  $$$
}'

Length of output: 151


Script:

#!/bin/bash
# First, locate the Prisma schema file(s)
echo "Searching for files named schema.prisma..."
fd schema.prisma

# Now, print out the definition blocks for the Project and EnvironmentVariable models,
# along with any uniqueness constraints (including @id and @@unique) from the Prisma schema.
echo "=== Model Project definition (with uniqueness constraints) ==="
rg -U "model Project\s*\{([^}]+)\}" -n .

echo "=== Model EnvironmentVariable definition (with uniqueness constraints) ==="
rg -U "model EnvironmentVariable\s*\{([^}]+)\}" -n .

Length of output: 5489

apps/webapp/app/v3/services/triggerTask.server.ts (1)

177-177: LGTM! Efficient database query changes.

The changes from findUnique to findFirst are appropriate here as we're looking up records that could potentially have multiple matches, and we only need the first matching record in each case. The where clauses are specific enough to ensure we get the intended records.

Also applies to: 214-214, 231-231, 275-275, 323-323

apps/webapp/app/v3/services/batchTriggerV3.server.ts (1)

162-162: LGTM! Improved database operations.

The changes enhance the code in two ways:

  1. Using findFirst for dependent attempt retrieval aligns with the more efficient query pattern
  2. Adding the "create batch run" transaction label improves observability for tracking long-running transactions

Also applies to: 459-459

apps/webapp/app/v3/services/alerts/deliverAlert.server.ts (1)

550-570: LGTM! Improved Slack integration lookup.

The changes to use findFirst for both primary and fallback Slack integration lookups are well-implemented:

  1. Primary lookup uses specific criteria (integrationId + organizationId)
  2. Fallback lookup appropriately orders by creation date to get the most recent integration
  3. Both queries handle potential multiple matches efficiently
internal-packages/database/prisma/schema.prisma (5)

1586-1586: Index Addition in BackgroundWorker Model

A new index on runtimeEnvironmentId has been added here. This should help optimize queries filtering on this field across worker records. Please verify that the index order aligns with your most common query patterns.


1649-1650: Composite Index in BackgroundWorkerTask Model

The addition of the composite index on [runtimeEnvironmentId, projectId] will likely speed up queries that filter by both fields. Ensure that the column order reflects the actual filtering patterns in your database queries for optimal performance.


1918-1919: Indexes Added in TaskRunDependency Model

Separate indexes on dependentAttemptId and dependentBatchRunId have been introduced to enhance lookup efficiency for task dependencies. Confirm that these indexes support your frequently executed joins/filters and do not duplicate existing indexes.


2204-2204: Index Addition in BatchTaskRun Model

An index on dependentTaskAttemptId is now present to improve the performance for queries filtering on task attempts associated with batch runs. Verify that this field is a common query predicate and check that the added index yields the expected performance improvements.


2335-2336: Indexes Added in CheckpointRestoreEvent Model

Indexes on both checkpointId and runId have been added. These should facilitate faster lookup of checkpoint events and help in correlating runs with their restore events. It is advisable to review the query plans to ensure these indexes are being utilized effectively.

@ericallam ericallam merged commit b377b57 into main Feb 3, 2025
13 checks passed
@ericallam ericallam deleted the ea-branch-9 branch February 3, 2025 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant