Skip to content

Conversation

@cubic-dev-local
Copy link

@cubic-dev-local cubic-dev-local bot commented Sep 12, 2025

## What does this PR do?
  • Fixes #XXXX (GitHub issue number)
  • Fixes CAL-XXXX (Linear issue number - should be visible at the bottom of the GitHub issue description)

Visual Demo (For contributors especially)

A visual demonstration is strongly recommended, for both the original and new change (video / image - any one).

Video Demo (if applicable):

  • Show screen recordings of the issue or feature.
  • Demonstrate how to reproduce the issue, the behavior before and after the change.

Image Demo (if applicable):

  • Add side-by-side screenshots of the original and updated change.
  • Highlight any significant change(s).

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set?
  • What are the minimal test data to have?
  • What is expected (happy path) to have (input and output)?
  • Any other important info that could help to test that PR

Checklist

  • I haven't read the contributing guide
  • My code doesn't follow the style guidelines of this project
  • I haven't commented my code, particularly in hard-to-understand areas
  • I haven't checked if my changes generate no new warnings

Prompt used to fix this problem

After upgrading Prisma to v6, things are broken. Fix all the $queryRaw usages from

    const bookingsFromSelected = await this.prisma.$queryRaw<
      Array<{
        eventTypeId: number;
        count: number;
      }>
    >`
      SELECT
        "eventTypeId",
        COUNT(id)::int as count
      FROM "BookingTimeStatusDenormalized"
      WHERE ${baseConditions} AND "eventTypeId" IS NOT NULL
      GROUP BY "eventTypeId"
      ORDER BY count DESC
      LIMIT 10
    `;

to

    const query = Prisma.sql`
      SELECT
        "eventTypeId",
        COUNT(id)::int as count
      FROM "BookingTimeStatusDenormalized"
      WHERE ${baseConditions} AND "eventTypeId" IS NOT NULL
      GROUP BY "eventTypeId"
      ORDER BY count DESC
      LIMIT 10
    `;
    

    const bookingsFromSelected = await this.prisma.$queryRaw<
      Array<{
        eventTypeId: number;
        count: number;
      }>
    >(query);

We're basically extracting the whole query as Prisma.sql, and pass the query to queryRaw.

But this needs to be fixed only when the $queryRaw..... includes a parameter that is an instance of Prisma.sql. So no need to fix something like this, because the parameterized values (someString) is primitive values, and not an instance of Prisma.sql....

    const bookingsFromSelected = await this.prisma.$queryRaw<
      Array<{
        eventTypeId: number;
        count: number;
      }>
    >`
      SELECT
        "eventTypeId",
        COUNT(id)::int as count
      FROM "BookingTimeStatusDenormalized"
      WHERE "eventTypeId" IS NOT NULL OR "status" = ${someString}
      GROUP BY "eventTypeId"
      ORDER BY count DESC
      LIMIT 10
    `;
---

Based on: calcom/cal.com#23775

Summary by cubic

Updates raw SQL calls to the Prisma v6-safe pattern by extracting queries into Prisma.sql and passing them to $queryRaw, fixing runtime errors when interpolating Sql fragments. Touches Insights booking and routing services; no behavior changes expected.

  • Refactors
    • Replaced $queryRaw template usage with const query = Prisma.sql...; await prisma.$queryRaw(query) where Prisma.Sql fragments (e.g., baseConditions, case statements) are interpolated.
    • Split several queries into named variables for readability (count queries, paginated queries, CTEs).
    • Minor safety cleanup when writing period keys to userStatsMap.
    • Updated imports to use Prisma from @calcom/prisma/client.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 2 files

@github-actions
Copy link

This PR is being marked as stale due to inactivity.

@github-actions github-actions bot added the Stale label Sep 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants