Get the FREE Ultimate OpenClaw Setup Guide →

algorithm-migration-with-rollback

Scanned
npx machina-cli add skill shimo4228/claude-code-learned-skills/algorithm-migration-with-rollback --openclaw
Files (1)
SKILL.md
2.8 KB

Algorithm Migration with Rollback Safety

Extracted: 2026-02-07 Context: Replacing core algorithms (SM2 → FSRS, encryption, hashing, etc.) while preserving rollback capability

Problem

When replacing a core algorithm that affects user data:

  • Legacy code removal risks losing restoration capability
  • Data format changes may break existing users
  • Rollback becomes difficult after merging
  • Testing all edge cases is critical

Solution

4-Phase Migration Strategy

Phase 1: Core Implementation (TDD)
├── Add @deprecated markers to old code
├── Implement new algorithm with tests first
└── Target 80%+ coverage

Phase 2: Data Model Migration
├── Extend existing models (don't replace)
├── Implement MigrationStrategy enum
└── Add version detection to parsers

Phase 3: Service Layer Integration
├── Create git tag BEFORE changes
├── Update services to use new algorithm
└── Integration tests

Phase 4: Cleanup & Verification
├── Create final git tag (restoration point)
├── Remove deprecated code
└── Full test suite (500+ tests)

Key Patterns

1. Deprecation Before Deletion:

@available(*, deprecated, renamed: "NewAlgorithm", message: """
    OldAlgorithm is deprecated and will be removed in v2.0.
    Migrate to NewAlgorithm for improved accuracy.
    """)
public enum OldAlgorithm { ... }

2. Transparent Data Migration:

public enum MigrationStrategy {
    static func migrate(oldRecord: OldFormat) -> NewFormat {
        NewFormat(
            // Preserve critical fields
            id: oldRecord.id,
            lastModified: oldRecord.lastModified,
            // Transform algorithm-specific fields
            newField: convertOldToNew(oldRecord.oldField)
        )
    }
}

3. Version Detection:

public static func detectVersion(_ data: String) -> DataVersion {
    if data.contains("new_field_header") { return .v2 }
    return .v1
}

4. Git Tag Restoration Points:

# Before cleanup
git tag v1.x-pre-cleanup

# Emergency restoration
git show v1.x-pre-cleanup:path/to/OldAlgorithm.swift > OldAlgorithm.swift

Example

FSRS Migration (SM2 → FSRS):

  • 566 tests total
  • 4 phases over 1 day (with TDD automation)
  • Zero data loss for existing users
  • Transparent migration on first load

When to Use

  • Replacing encryption/hashing algorithms
  • Upgrading ML models with different input/output formats
  • Migrating database schemas with computed fields
  • Any core algorithm affecting persisted user data

Source

git clone https://github.com/shimo4228/claude-code-learned-skills/blob/main/skills/algorithm-migration-with-rollback/SKILL.mdView on GitHub

Overview

Provides a 4-phase strategy to replace core algorithms (encryption, hashing, ML models) without losing data or rollback capability. It emphasizes deprecation, incremental data model migration, service-layer integration, and a final cleanup with restoration points and full test coverage.

How This Skill Works

Follow Phase 1 through Phase 4 with a tests-first mindset. Extend existing models instead of replacing them, introduce a MigrationStrategy for data transformation, and implement version detection to handle legacy formats. Create git restoration points before cleanup and verify with a full test suite before removing deprecated code.

When to Use It

  • Replacing encryption, hashing, or other core algorithms that affect persisted user data
  • Upgrading ML models that change input/output formats
  • Migrating database schemas that introduce computed or transformed fields
  • Rolling out a new algorithm while preserving rollback to the old version
  • Any core algorithm migration where data integrity and recoverability are critical

Quick Start

  1. Step 1: Add @deprecated markers to old code and implement the new algorithm with tests-first (Phase 1).
  2. Step 2: Extend data models, implement MigrationStrategy, and add version detection; update service layer (Phase 2-3).
  3. Step 3: Create restoration git tags, run integration tests, remove deprecated code, and finalize with a full test suite (Phase 4).

Best Practices

  • Deprecate before deletion: mark old code as deprecated and route to NewAlgorithm
  • Perform transparent data migration: extend models and transform fields without breaking old data
  • Use a MigrationStrategy to encapsulate data transformation logic
  • Implement explicit data version detection to handle v1/v2 formats
  • Create git restoration points (tags) before changes and after cleanup; run full test suite

Example Use Cases

  • FSRS migration from SM2: 4 phases, 566 tests, zero data loss
  • Encryption algorithm upgrade with backward-compatibility path
  • Hashing algorithm upgrade maintaining legacy data access
  • ML model migration with updated input/output schemas
  • Database schema migration introducing computed fields with safe rollbacks

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers