Skip to content

fix: add backfill migration for routing response denormalized tables #21474

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 18 commits into from
Aug 5, 2025

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented May 22, 2025

What does this PR do?

This PR adds backfill migration for the RoutingFormResponseDenormalized and RoutingFormResponseField tables.

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A - 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.

@github-actions github-actions bot added the ❗️ migrations contains migration files label May 22, 2025
Copy link

vercel bot commented May 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Jul 28, 2025 1:15pm
cal-eu ⬜️ Ignored (Inspect) Visit Preview Jul 28, 2025 1:15pm

Copy link
Contributor

github-actions bot commented Jun 7, 2025

This PR is being marked as stale due to inactivity.

@github-actions github-actions bot added the Stale label Jun 7, 2025
@eunjae-lee
Copy link
Contributor Author

Note to myself:

To skip these migrations:

insert into
    _prisma_migrations (
        id,
        migration_name,
        checksum,
        started_at,
        finished_at,
        applied_steps_count
    )
values (
        gen_random_uuid(),
        '20250618093846_routing_form_response_denormalize_backfill',
        '<INSERT-CHECKSUM1>',
        now(),
        now(),
        1
    ),
    (
        gen_random_uuid(),
        '20250618093923_routing_form_response_field_backfill',
        '<INSERT-CHECKSUM2>',
        now(),
        now(),
        1
    );

CHECKSUM1

sha256sum packages/prisma/migrations/20250618093846_routing_form_response_denormalize_backfill/migration.sql

CHECKSUM2

sha256sum packages/prisma/migrations/20250618093923_routing_form_response_field_backfill/migration.sql

Copy link
Contributor

github-actions bot commented Jul 8, 2025

This PR is being marked as stale due to inactivity.

@github-actions github-actions bot added the Stale label Jul 8, 2025
emrysal
emrysal previously approved these changes Jul 28, 2025
Copy link
Contributor

@emrysal emrysal left a comment

Choose a reason for hiding this comment

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

Will be released as part of tonights release.

@eunjae-lee eunjae-lee marked this pull request as ready for review July 28, 2025 11:35
@eunjae-lee eunjae-lee requested a review from a team as a code owner July 28, 2025 11:35
Copy link
Contributor

coderabbitai bot commented Jul 28, 2025

Caution

Review failed

The head commit changed during the review from a989026 to e3fdfb2.

Walkthrough

This pull request adds two standalone SQL migration scripts for backfilling denormalized data related to routing form responses. The first script performs a batch upsert on the "RoutingFormResponseDenormalized" table by processing records in chunks of 1000 from the "App_RoutingForms_FormResponse" table, joining with related tables, and inserting or updating records as needed while logging progress and throttling load. The second script processes the same source table in chunks, identifying routing form response fields requiring updates by comparing existing denormalized data with expected values derived from JSON responses and field definitions, then calls a reprocessing function for each such response, handling errors and logging progress similarly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15–20 minutes

Possibly related PRs

  • fix: routing form response denormalized triggers #22518: This PR sets up triggers and functions to maintain the "RoutingFormResponseDenormalized" table in real-time, complementing the batch backfill migration script in the current PR that operates on the same table but through batch processing.
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/backfill-denorm-tables

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @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.

@graphite-app graphite-app bot requested a review from a team July 28, 2025 11:35
@dosubot dosubot bot added the routing-forms area: routing forms, routing, forms label Jul 28, 2025
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: 4

🧹 Nitpick comments (3)
packages/prisma/migrations/20250618093846_routing_form_response_denormalize_backfill/migration.sql (2)

4-8: Consider deriving start_id from the data instead of hard-coding 1.
If the smallest App_RoutingForms_FormResponse.id is greater than 1, the first chunk does an unnecessary DELETE on a range that cannot hold rows, slightly inflating processed_count and wasting time.

-start_id INTEGER := 1;      -- Starting ID
+SELECT COALESCE(MIN(id), 1) INTO start_id FROM "App_RoutingForms_FormResponse";

23-29: processed_count is over-counted when gaps exist in IDs.
processed_count := processed_count + chunk_size; assumes every BETWEEN range contains exactly chunk_size responses, which is false when IDs are sparse or at the tail of the table. Prefer COUNT(*) of the actually inserted rows (or use GET DIAGNOSTICS) to keep the progress log trustworthy.

packages/prisma/migrations/20250618093923_routing_form_response_field_backfill/migration.sql (1)

30-33: WHEN OTHERS swallows useful detail; include SQLSTATE.
Capturing SQLSTATE helps triage repeated failures.

-RAISE WARNING 'Failed to process responseId %: %', response_record.id, SQLERRM;
+RAISE WARNING 'Failed responseId % (SQLSTATE %, message %)', 
+              response_record.id, SQLSTATE, SQLERRM;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a745d8a and ecce399.

📒 Files selected for processing (2)
  • packages/prisma/migrations/20250618093846_routing_form_response_denormalize_backfill/migration.sql (1 hunks)
  • packages/prisma/migrations/20250618093923_routing_form_response_field_backfill/migration.sql (1 hunks)
🔇 Additional comments (1)
packages/prisma/migrations/20250618093846_routing_form_response_denormalize_backfill/migration.sql (1)

58-99: LEFT JOINs can duplicate the target id, risking unique_violation.
If a booking has multiple Tracking rows, the SELECT will return >1 row for the same r.id, breaking the presumed PK on RoutingFormResponseDenormalized.id. Add DISTINCT ON (r.id) or aggregate the tracking columns (e.g., MAX) before insertion.

Copy link

graphite-app bot commented Jul 28, 2025

Graphite Automations

"Add ready-for-e2e label" took an action on this PR • (07/28/25)

1 label was added to this PR based on Keith Williams's automation.

"Add consumer team as reviewer" took an action on this PR • (07/28/25)

1 reviewer was added to this PR based on Keith Williams's automation.

@eunjae-lee eunjae-lee marked this pull request as draft July 28, 2025 11:48
@eunjae-lee eunjae-lee requested a review from emrysal July 28, 2025 11:52
@eunjae-lee eunjae-lee marked this pull request as ready for review July 28, 2025 12:12
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

♻️ Duplicate comments (3)
packages/prisma/migrations/20250618093846_routing_form_response_denormalize_backfill/migration.sql (2)

1-26: Entire backfill still runs in one monolithic transaction

The DO block is a single implicit transaction, so every DELETE/UPSERT lock it takes is held until the very end. On large tables this can block production traffic, create bloat, and make the migration nearly un-cancelable. Please commit per chunk (e.g. move the chunk logic to a helper function invoked with PERFORM …; COMMIT;) or use autonomous transactions (CALL) instead.


194-195: pg_sleep executed while locks are held

Because no commit happens before the sleep, row/share locks stay active for the whole second, defeating the throttling purpose. Move the sleep after a per-chunk commit or drop it.

packages/prisma/migrations/20250618093923_routing_form_response_field_backfill/migration.sql (1)

124-126: pg_sleep holds locks for the entire pause

Without a preceding commit the sleep simply lengthens the lock window. Commit (or split into separate transactions) before calling pg_sleep, or delete the sleep.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ecce399 and e59fc85.

📒 Files selected for processing (2)
  • packages/prisma/migrations/20250618093846_routing_form_response_denormalize_backfill/migration.sql (1 hunks)
  • packages/prisma/migrations/20250618093923_routing_form_response_field_backfill/migration.sql (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache

@eunjae-lee eunjae-lee force-pushed the fix/backfill-denorm-tables branch from e59fc85 to a989026 Compare July 28, 2025 13:09
@eunjae-lee
Copy link
Contributor Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jul 28, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@emrysal emrysal left a comment

Choose a reason for hiding this comment

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

LGTM, prod is prepared. 👍

@emrysal emrysal merged commit 006aecb into main Aug 5, 2025
87 of 92 checks passed
@emrysal emrysal deleted the fix/backfill-denorm-tables branch August 5, 2025 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consumer core area: core, team members only ❗️ migrations contains migration files ready-for-e2e routing-forms area: routing forms, routing, forms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants