Skip to content

fix: avoid no-op Google Sheets backfill rewrites#8864

Open
tataihono wants to merge 5 commits intomainfrom
cursor/sync-file-modification-trigger-af23
Open

fix: avoid no-op Google Sheets backfill rewrites#8864
tataihono wants to merge 5 commits intomainfrom
cursor/sync-file-modification-trigger-af23

Conversation

@tataihono
Copy link
Contributor

@tataihono tataihono commented Mar 17, 2026

Summary

  • stop rewriting Google Sheets during backfill when the target write range is unchanged
  • compare only the exact range the current backfill pass would write (A1 to computed last row/column)
  • keep existing clear+rewrite behavior when that target range differs
  • add backfill worker tests for unchanged vs changed target range behavior

Why

Button-click-only activity can trigger backfill jobs even when there are no exportable submission events. Previously this still cleared/re-wrote the sheet, causing Google Drive to show a file edit with no meaningful data change.

Testing

  • pnpm nx test api-journeys-modern --runInBand --testPathPattern=backfill.spec.ts (fails in current branch due existing unrelated TS error: apis/api-journeys-modern/test/prismaMock.ts importing non-exported PrismaClient)
  • pnpm exec eslint apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.ts apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.spec.ts (repo currently uses Nx lint config; standalone ESLint v9 flat-config entrypoint not present)

Slack Thread

Open in Web Open in Cursor 

Summary by CodeRabbit

  • Tests

    • Added comprehensive tests for the Google Sheets backfill flow, covering skip/write scenarios, read failures, and missing data cases.
  • Bug Fixes

    • Skip unnecessary sheet clearing/writing when content is unchanged.
    • Fallback to full write and log a warning if reading sheet values fails.
    • Gracefully skip backfill and warn when sync or journey records are missing.
  • Refactor

    • Made sheet write logic conditional to avoid needless operations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2b7b4115-9ed0-401d-9982-571c6ba9e9e4

📥 Commits

Reviewing files that changed from the base of the PR and between 7b67c00 and e7d793d.

📒 Files selected for processing (3)
  • apis/api-journeys-modern/src/lib/google/sheets.ts
  • apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.spec.ts
  • apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.spec.ts
  • apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.ts

Walkthrough

Refactors the Google Sheets backfill to read existing sheet values and only clear/write when content differs; adds robust read-failure fallback and warning logs. Adds a comprehensive test suite covering unchanged content, differing content, empty sheets, read failures, and missing records.

Changes

Cohort / File(s) Summary
Backfill tests
apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.spec.ts
New test suite mocking Google auth/Sheets and Prisma; covers scenarios: unchanged sheet, differing content, larger existing data, empty sheet, read failure fallback, missing sync, missing journey. Asserts clear/write calls and ensureSheet not invoked on skips.
Backfill implementation
apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.ts
Refactored to compute write range, read existing sheet values, determine equality via isSheetContentUnchanged, and conditionally clear/write only when content differs. Adds read-failure fallback to full write and warning logs.
Sheets util
apis/api-journeys-modern/src/lib/google/sheets.ts
Adds exported escapeSheetName(name: string): string to escape A1 sheet titles (wraps in single quotes and doubles internal single quotes).

Sequence Diagram

sequenceDiagram
    participant Backfill as Backfill Service
    participant Prisma as Prisma Database
    participant Sheets as Google Sheets API

    Backfill->>Prisma: fetch googleSheetsSync config
    alt sync not found
        Backfill->>Backfill: log warning, skip
    else sync found
        Backfill->>Prisma: fetch journey & events
        alt journey not found
            Backfill->>Backfill: log warning, skip
        else journey found
            Backfill->>Sheets: read existing sheet values (readValues)
            alt read fails
                Backfill->>Sheets: write all values (writeValues)
                Backfill->>Backfill: log warning about read failure
            else read succeeds
                Backfill->>Backfill: compare existing vs new (isSheetContentUnchanged)
                alt content unchanged
                    Backfill->>Backfill: skip clear/write
                else content changed
                    Backfill->>Sheets: clear sheet (clearSheet)
                    Backfill->>Sheets: write new values (writeValues)
                end
            end
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: avoiding unnecessary rewrites of Google Sheets during backfill when content is unchanged, which aligns with the PR's core objective and implementation.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cursor/sync-file-modification-trigger-af23
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link

nx-cloud bot commented Mar 17, 2026

View your CI Pipeline Execution ↗ for commit e7d793d

Command Status Duration Result
nx affected --target=subgraph-check --base=8d2f... ✅ Succeeded 2s View ↗
nx affected --target=extract-translations --bas... ✅ Succeeded <1s View ↗
nx affected --target=lint --base=8d2f616c2679db... ✅ Succeeded <1s View ↗
nx affected --target=type-check --base=8d2f616c... ✅ Succeeded <1s View ↗
nx run-many --target=codegen --all --parallel=3 ✅ Succeeded 1s View ↗
nx run-many --target=prisma-generate --all --pa... ✅ Succeeded 4s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-18 20:56:46 UTC

@tataihono tataihono marked this pull request as ready for review March 17, 2026 01:15
@tataihono tataihono requested a review from mikeallisonJS March 17, 2026 01:16
@tataihono tataihono self-assigned this Mar 17, 2026
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.ts`:
- Around line 330-331: fullRange is built using a raw sheetName which breaks A1
parsing for titles with spaces/apostrophes; escape the sheet name before
concatenating by replacing each single quote with two single quotes and wrapping
the name in single quotes (per Google Sheets A1 syntax) so use an escaped
variant instead of sheetName when building fullRange (refer to columnIndexToA1,
lastColumnA1, and fullRange); update the code that calls readValues() to use the
escaped sheet name, or add a small helper like escapeSheetName(sheetName) that
performs the replace and quoting and use that in place of sheetName, ensuring
readFailed/contentUnchanged behavior no longer triggers for valid sheet titles.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e0fd3d9f-af76-4ecb-bf2f-9cfb4d304ba6

📥 Commits

Reviewing files that changed from the base of the PR and between cb11c74 and 7b67c00.

📒 Files selected for processing (2)
  • apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.spec.ts
  • apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.ts

@stage-branch-merger
Copy link

I see you added the "on stage" label, I'll get this merged to the stage branch!

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.

3 participants