Skip to content

fix: Make third-party migration writes an all-or-nothing batch - #1701

Merged
hatayama merged 2 commits into
v3-migration-hardeningfrom
fix/migration-write-batch-transaction
Jul 12, 2026
Merged

fix: Make third-party migration writes an all-or-nothing batch#1701
hatayama merged 2 commits into
v3-migration-hardeningfrom
fix/migration-write-batch-transaction

Conversation

@hatayama

@hatayama hatayama commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

  • Replace per-file Write with WriteBatch / WriteBatchAsync so migration apply is one prepare → commit transaction
  • Keep .bak sidecars until the full commit succeeds; on mid-batch failure, restore already committed targets and leave no half-migrated project
  • Move the async yield cadence into prepare; commit stays rename-only with no yields
  • Add focused writer tests for happy path, prepare failure, commit rollback of existing and newly created files, and async yield-sized batches

User Impact

Applying a V2→V3 third-party tool migration no longer leaves the project half-rewritten when a file write fails mid-batch; already written files are restored from backups.

Test plan

  • uloop compile — 0 errors / 0 warnings
  • uloop run-tests --filter-type regex --filter-value ThirdPartyToolMigrationFileWriterTests — 5/5 passed
  • Live demo via uloop execute-dynamic-code + reflection: inject commit failure with a directory target → IOException, file1 restored to original, file3 untouched, sidecarCount=0

Live verification log

tempDirectory=.../UnityCliLoopMigrationWriterLiveDemo/...
exception=IOException: The file '.../FileTwo.cs' already exists.
file1After=original-one
file3After=original-three
file1Restored=True
file3Untouched=True
sidecarCount=0

A mid-batch File.Replace failure previously left projects half-migrated
with no rollback. Prepare all temp sidecars first, keep .bak files until
the full commit succeeds, and restore already committed targets on failure.

Co-authored-by: Cursor <cursoragent@cursor.com>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4751232e-7625-455a-b1a6-0c29a8ce5a53

📥 Commits

Reviewing files that changed from the base of the PR and between 8eab379 and 0ae4edf.

📒 Files selected for processing (1)
  • Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/ThirdPartyToolMigrationFileWriter.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/ThirdPartyToolMigrationFileWriter.cs

📝 Walkthrough

Walkthrough

Migration file updates now use atomic batch writes with temporary files, backups, rollback, and cleanup. Synchronous and asynchronous services delegate to the batch writer, with tests covering success, failures, rollback, sidecar cleanup, and large batches.

Changes

Batch migration writes

Layer / File(s) Summary
Batch writer and rollback mechanics
Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/...
Adds synchronous and asynchronous two-phase batch writing, backup-based rollback, and best-effort sidecar cleanup.
Migration service batch integration
Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/ThirdPartyToolMigrationFileService.cs
Replaces per-file mutation loops with synchronous and asynchronous batch writer calls while preserving pre-write cancellation handling.
Atomicity and async validation
Assets/Tests/Editor/ThirdPartyToolMigrationFileWriterTests.cs
Tests successful writes, preparation and commit failures, rollback behavior, sidecar cleanup, and asynchronous large-batch commits.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MigrationFileService
  participant ThirdPartyToolMigrationFileWriter
  participant FileSystem
  MigrationFileService->>ThirdPartyToolMigrationFileWriter: Submit migration changes
  ThirdPartyToolMigrationFileWriter->>FileSystem: Write temporary sidecars
  ThirdPartyToolMigrationFileWriter->>FileSystem: Commit moves or replacements
  FileSystem-->>ThirdPartyToolMigrationFileWriter: Return commit result
  ThirdPartyToolMigrationFileWriter->>FileSystem: Restore backups or delete new targets on failure
  ThirdPartyToolMigrationFileWriter-->>MigrationFileService: Complete batch or propagate exception
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: making third-party migration writes batch-based and all-or-nothing.
Description check ✅ Passed The description accurately summarizes the batch transaction, rollback behavior, async changes, and added tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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 fix/migration-write-batch-transaction

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/ThirdPartyToolMigrationFileWriter.cs`:
- Around line 90-95: Update PrepareAll and PrepareAllAsync to register each
generated temp sidecar path in preparedWrites before invoking Prepare or
WriteAllText. Ensure cleanup can reach the path when writing fails, while
preserving the existing PreparedWrite flow for successful writes.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8ddb61f3-c3c3-4625-888d-5d46ed50334e

📥 Commits

Reviewing files that changed from the base of the PR and between 8785326 and 8eab379.

⛔ Files ignored due to path filters (1)
  • Assets/Tests/Editor/ThirdPartyToolMigrationFileWriterTests.cs.meta is excluded by none and included by none
📒 Files selected for processing (3)
  • Assets/Tests/Editor/ThirdPartyToolMigrationFileWriterTests.cs
  • Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/ThirdPartyToolMigrationFileService.cs
  • Packages/src/Editor/Infrastructure/ThirdPartyToolMigration/ThirdPartyToolMigrationFileWriter.cs

WriteAllText can leave a partial .tmp if it fails mid-write. Registering
the sidecar path first lets prepare-failure cleanup still delete it.

Co-authored-by: Cursor <cursoragent@cursor.com>
@hatayama
hatayama merged commit 2eefcfa into v3-migration-hardening Jul 12, 2026
2 checks passed
@hatayama
hatayama deleted the fix/migration-write-batch-transaction branch July 12, 2026 01:18
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