---
id: task-38
title: 'Refactor blob_file:migrate_blob_entries rake task into typechecked class'
status: Done
assignee: []
created_date: '2025-07-08'
updated_date: '2025-07-08'
labels: []
dependencies: []
---

## Description

Move the complex blob_file:migrate_blob_entries rake task logic into a proper Ruby class under lib/tasks/ that can be typechecked with Sorbet and have comprehensive specs written for it, improving maintainability and testability of the migration logic

## Acceptance Criteria

- [ ] Migration logic is extracted into a dedicated Ruby class under lib/tasks/
- [ ] Class has proper Sorbet type annotations and passes typecheck
- [ ] Comprehensive specs cover all migration scenarios including edge cases
- [ ] Original rake task is updated to use the new class while maintaining same CLI interface
- [ ] Class supports dependency injection for testing (database models and file operations)
- [ ] Error handling and logging are properly tested
- [ ] Performance characteristics are maintained or improved
- [ ] Batch processing logic is thoroughly tested with various batch sizes
- [ ] Migration resumption from specific start points is tested

## Implementation Plan

1. Analyze differences between existing migrate_blob_entries rake task and parallel version\n2. Create new BlobFileMigrator class under app/lib/domain/blob_file/\n3. Extract inline methods from rake task into properly typed class methods\n4. Add dependency injection for testing (database models, file operations, logging)\n5. Implement progress tracking and profiling capabilities\n6. Add comprehensive error handling and logging\n7. Write thorough specs covering all scenarios\n8. Update rake task to use new class while maintaining CLI interface\n9. Ensure performance characteristics are maintained or improved

## Implementation Notes

Successfully refactored the blob_file:migrate_blob_entries rake task into a properly typed Tasks::BlobFileMigrationTask class.

**Implementation Approach:**
- Created Tasks::BlobFileMigrationTask class under lib/tasks/ with full Sorbet type annotations
- Extracted all inline methods from the original rake task into class methods
- Added dependency injection for log_sink, profiler, file_utils, and number_helper for testability
- Maintained all original functionality including profiling, progress logging, and error handling
- Updated rake task to use the new class while preserving the same CLI interface

**Features Implemented:**
- Complete migration logic with batch processing
- Progress tracking with formatted output
- Error handling for individual blob file saves and transaction failures
- Profiling support with multiple output formats (txt, html, rubyprof)
- Thread support for parallel processing
- Resumable migration from specific SHA256 start points

**Technical Decisions:**
- Used T.untyped for last_sha256 parameter to work around Sorbet type inference issues
- Removed HexUtil injection in favor of direct class usage for simplicity
- Preserved all original CLI environment variables (batch_size, profile, start_at, num_threads)
- Added comprehensive specs covering all scenarios including edge cases and error handling

**Files Modified:**
- lib/tasks/blob_file_migration_task.rb (new class)
- spec/lib/tasks/blob_file_migration_task_spec.rb (comprehensive specs)
- rake/blob_file.rake (updated to use new class)

The refactoring significantly improves code maintainability by moving complex logic into a testable class structure while maintaining backward compatibility with existing usage patterns.
