398 lines
12 KiB
Markdown
398 lines
12 KiB
Markdown
# PHASE-07: Vehicles Feature Migration to Fastify
|
|
|
|
**Status**: 🔄 IN PROGRESS (Started 2025-08-24)
|
|
**Duration**: 4-5 days
|
|
**Prerequisites**: Docker modernization complete (Phase 6) ✅
|
|
**Next Phase**: PHASE-08-Backend-Complete
|
|
**Risk Level**: 🔴 HIGH (Core feature migration)
|
|
|
|
## 🎯 Phase Objectives
|
|
- Migrate complete vehicles feature capsule from Express to Fastify
|
|
- Maintain 100% API compatibility and functionality
|
|
- Achieve 2-3x performance improvement for vehicle operations
|
|
- Preserve Modified Feature Capsule architecture
|
|
- Comprehensive testing and validation
|
|
|
|
## 🚨 CRITICAL SAFETY MEASURES
|
|
|
|
### Before Starting ANY Step
|
|
1. **Full System Backup**
|
|
2. **Working Branch Creation**
|
|
3. **Performance Baseline Documentation**
|
|
4. **Rollback Plan Verification**
|
|
|
|
## 📋 Detailed Implementation Steps
|
|
|
|
### Step 1: Critical Prerequisites & Safety Setup
|
|
- [ ] **Verify Phase 6 Complete**
|
|
```bash
|
|
# Check Docker modernization working
|
|
docker images | grep mvp # Should show smaller, optimized images
|
|
make dev # Should work with new Docker setup
|
|
```
|
|
- [ ] **Complete System Backup**
|
|
```bash
|
|
git add -A
|
|
git commit -m "Pre-vehicles-fastify: All systems working"
|
|
git tag vehicles-express-working
|
|
git branch vehicles-fastify-backup
|
|
```
|
|
- [ ] **Document Current Vehicles Performance**
|
|
```bash
|
|
make dev && sleep 30
|
|
make shell-backend
|
|
|
|
# Test all vehicle endpoints
|
|
autocannon -c 10 -d 30 http://localhost:3001/api/vehicles
|
|
autocannon -c 10 -d 30 http://localhost:3001/api/vehicles/health
|
|
|
|
# Document baseline performance
|
|
echo "EXPRESS BASELINE:" >> vehicles-performance.log
|
|
echo "Vehicles List: [results]" >> vehicles-performance.log
|
|
echo "Memory usage: $(docker stats mvp-backend --no-stream)" >> vehicles-performance.log
|
|
exit
|
|
```
|
|
- [ ] **Verify Complete Vehicles Functionality**
|
|
```bash
|
|
# Test frontend vehicle operations
|
|
# - Login works
|
|
# - Vehicle list loads
|
|
# - Add vehicle works (with VIN decoding)
|
|
# - Edit vehicle works
|
|
# - Delete vehicle works
|
|
# - Mobile interface works
|
|
# Document all working functionality
|
|
```
|
|
|
|
### Step 2: Fastify Vehicles Setup (Parallel Implementation)
|
|
- [ ] **Create Fastify Vehicles Structure**
|
|
```bash
|
|
# Create parallel structure (don't modify Express yet)
|
|
make shell-backend
|
|
mkdir -p src/fastify-features/vehicles
|
|
mkdir -p src/fastify-features/vehicles/api
|
|
mkdir -p src/fastify-features/vehicles/domain
|
|
mkdir -p src/fastify-features/vehicles/data
|
|
mkdir -p src/fastify-features/vehicles/external
|
|
mkdir -p src/fastify-features/vehicles/tests
|
|
```
|
|
|
|
- [ ] **Install Fastify Validation Dependencies**
|
|
```bash
|
|
# Add Fastify-specific validation
|
|
npm install @fastify/type-provider-typebox
|
|
npm install @sinclair/typebox
|
|
npm install fastify-plugin
|
|
npm install @fastify/autoload
|
|
exit
|
|
```
|
|
|
|
### Step 3: Migrate Vehicle Data Layer
|
|
- [ ] **Convert Vehicle Repository to Fastify**
|
|
```typescript
|
|
// src/fastify-features/vehicles/data/vehicles.repository.ts
|
|
// Copy from src/features/vehicles/data/vehicles.repository.ts
|
|
// Update for Fastify context/decorators if needed
|
|
// Maintain identical interface and functionality
|
|
```
|
|
- [ ] **Test Data Layer**
|
|
```bash
|
|
# Create unit tests specifically for Fastify data layer
|
|
# Ensure database operations work identically
|
|
# Test all CRUD operations
|
|
# Test VIN cache operations
|
|
```
|
|
|
|
### Step 4: Migrate Vehicle Domain Logic
|
|
- [ ] **Convert Vehicle Service**
|
|
```typescript
|
|
// src/fastify-features/vehicles/domain/vehicles.service.ts
|
|
// Copy from src/features/vehicles/domain/vehicles.service.ts
|
|
// Update any Express-specific dependencies
|
|
// Maintain all business logic identically
|
|
```
|
|
- [ ] **Convert Vehicle Types**
|
|
```typescript
|
|
// src/fastify-features/vehicles/types/vehicles.types.ts
|
|
// Convert to TypeBox schemas for Fastify validation
|
|
// Maintain type compatibility with frontend
|
|
```
|
|
|
|
### Step 5: Migrate External Integrations
|
|
- [ ] **Convert vPIC Client**
|
|
```typescript
|
|
// src/fastify-features/vehicles/external/vpic/
|
|
// Copy existing vPIC integration
|
|
// Ensure VIN decoding works identically
|
|
// Maintain caching behavior
|
|
```
|
|
- [ ] **Test VIN Decoding**
|
|
```bash
|
|
# Test vPIC integration thoroughly
|
|
# Test with real VIN numbers
|
|
# Test cache behavior
|
|
# Test fallback handling
|
|
```
|
|
|
|
### Step 6: Create Fastify API Layer
|
|
- [ ] **Fastify Validation Schemas**
|
|
```typescript
|
|
// src/fastify-features/vehicles/api/vehicles.schemas.ts
|
|
// Convert Joi schemas to TypeBox schemas
|
|
// Maintain identical validation rules
|
|
// Ensure error messages are identical
|
|
```
|
|
- [ ] **Fastify Route Handlers**
|
|
```typescript
|
|
// src/fastify-features/vehicles/api/vehicles.controller.ts
|
|
// Convert Express controllers to Fastify handlers
|
|
// Maintain identical request/response formats
|
|
// Use Fastify's reply methods
|
|
```
|
|
- [ ] **Fastify Routes Registration**
|
|
```typescript
|
|
// src/fastify-features/vehicles/api/vehicles.routes.ts
|
|
// Define all vehicle routes for Fastify
|
|
// Maintain exact same URL patterns
|
|
// Same middleware/authentication
|
|
```
|
|
|
|
### Step 7: Integration and Testing Setup
|
|
- [ ] **Fastify Vehicles Plugin**
|
|
```typescript
|
|
// src/fastify-features/vehicles/index.ts
|
|
// Create Fastify plugin that registers all vehicles functionality
|
|
// Export registration function
|
|
// Maintain capsule isolation
|
|
```
|
|
- [ ] **Update Feature Flag System**
|
|
```bash
|
|
# Add environment variable
|
|
VEHICLES_BACKEND=express # or 'fastify'
|
|
|
|
# Update main app to conditionally load vehicles
|
|
# Either Express routes OR Fastify routes, not both
|
|
```
|
|
|
|
### Step 8: Comprehensive Testing Phase
|
|
- [ ] **Unit Tests Migration**
|
|
```bash
|
|
# Copy all existing vehicles tests
|
|
# Update for Fastify test patterns
|
|
# Ensure 100% test coverage maintained
|
|
# All tests should pass
|
|
```
|
|
- [ ] **Integration Testing**
|
|
```bash
|
|
# Test both backends in parallel:
|
|
|
|
# Express vehicles
|
|
VEHICLES_BACKEND=express make dev
|
|
# Run full test suite
|
|
# Document all functionality working
|
|
|
|
# Fastify vehicles
|
|
VEHICLES_BACKEND=fastify make dev
|
|
# Run identical test suite
|
|
# Verify identical functionality
|
|
```
|
|
- [ ] **API Compatibility Testing**
|
|
```bash
|
|
# Test exact API compatibility
|
|
# Same request formats
|
|
# Same response formats
|
|
# Same error handling
|
|
# Same status codes
|
|
```
|
|
|
|
### Step 9: Performance Benchmarking
|
|
- [ ] **Fastify Performance Testing**
|
|
```bash
|
|
VEHICLES_BACKEND=fastify make dev && sleep 30
|
|
make shell-backend
|
|
|
|
# Test all vehicle endpoints
|
|
autocannon -c 10 -d 60 http://localhost:3001/api/vehicles
|
|
autocannon -c 50 -d 60 http://localhost:3001/api/vehicles
|
|
autocannon -c 100 -d 60 http://localhost:3001/api/vehicles
|
|
|
|
# Document performance improvements
|
|
echo "FASTIFY RESULTS:" >> vehicles-performance.log
|
|
echo "Vehicles List: [results]" >> vehicles-performance.log
|
|
echo "Memory usage: $(docker stats mvp-backend --no-stream)" >> vehicles-performance.log
|
|
exit
|
|
```
|
|
- [ ] **Performance Comparison Analysis**
|
|
```bash
|
|
# Compare Express vs Fastify results
|
|
# Should show 2-3x improvement in:
|
|
# - Requests per second
|
|
# - Response latency
|
|
# - Memory efficiency
|
|
# Document all improvements
|
|
```
|
|
|
|
### Step 10: Production Readiness
|
|
- [ ] **Frontend Integration Testing**
|
|
```bash
|
|
# Test frontend works with Fastify backend
|
|
VEHICLES_BACKEND=fastify make dev
|
|
|
|
# Test all frontend vehicle functionality:
|
|
# - Vehicle list loading
|
|
# - Add vehicle with VIN decoding
|
|
# - Edit vehicle
|
|
# - Delete vehicle
|
|
# - Mobile interface
|
|
# - Error handling
|
|
```
|
|
- [ ] **Error Handling Verification**
|
|
```bash
|
|
# Test error scenarios:
|
|
# - Invalid VIN
|
|
# - Network failures
|
|
# - Database errors
|
|
# - Authentication errors
|
|
# Ensure identical error responses
|
|
```
|
|
- [ ] **Migration Strategy Documentation**
|
|
```markdown
|
|
# Document the switch process:
|
|
# 1. Set VEHICLES_BACKEND=fastify
|
|
# 2. Restart services
|
|
# 3. Verify functionality
|
|
# 4. Monitor performance
|
|
# 5. Rollback procedure if needed
|
|
```
|
|
|
|
## ✅ Phase Completion Criteria
|
|
|
|
**CRITICAL - All checkboxes must be completed**:
|
|
- [ ] Fastify vehicles implementation 100% functionally identical to Express
|
|
- [ ] All existing vehicle tests pass with Fastify backend
|
|
- [ ] Frontend works identically with Fastify backend
|
|
- [ ] VIN decoding works correctly (vPIC integration)
|
|
- [ ] Performance improvement of 2-3x demonstrated
|
|
- [ ] Feature flag system allows switching between Express/Fastify
|
|
- [ ] Database operations work identically
|
|
- [ ] Caching behavior preserved
|
|
- [ ] Error handling identical
|
|
- [ ] Mobile interface works correctly
|
|
- [ ] Authentication and authorization work
|
|
- [ ] All edge cases tested and working
|
|
|
|
## 🧪 Critical Testing Protocol
|
|
|
|
### Pre-Migration Verification
|
|
```bash
|
|
# MUST PASS - Express vehicles working perfectly
|
|
VEHICLES_BACKEND=express make dev
|
|
# Test every single vehicle operation
|
|
# Document that everything works
|
|
```
|
|
|
|
### Post-Migration Verification
|
|
```bash
|
|
# MUST PASS - Fastify vehicles working identically
|
|
VEHICLES_BACKEND=fastify make dev
|
|
# Test identical operations
|
|
# Verify identical behavior
|
|
```
|
|
|
|
### Performance Verification
|
|
```bash
|
|
# MUST SHOW 2x+ improvement
|
|
# Run identical performance tests
|
|
# Document significant improvements
|
|
# Memory usage should be better or equal
|
|
```
|
|
|
|
### Rollback Readiness Test
|
|
```bash
|
|
# MUST WORK - Switch back to Express
|
|
VEHICLES_BACKEND=express make dev
|
|
# Everything should still work perfectly
|
|
# This is critical for production safety
|
|
```
|
|
|
|
## 🚨 Emergency Procedures
|
|
|
|
### If Migration Fails
|
|
1. **IMMEDIATE**: `VEHICLES_BACKEND=express`
|
|
2. **Restart**: `make rebuild && make dev`
|
|
3. **Verify**: All vehicle functionality works
|
|
4. **Document**: What went wrong in this file
|
|
5. **Plan**: Address issues before retry
|
|
|
|
### If Performance Goals Not Met
|
|
1. **Profile**: Use Fastify performance tools
|
|
2. **Compare**: Detailed comparison with Express
|
|
3. **Optimize**: Focus on bottlenecks
|
|
4. **Retest**: Verify improvements
|
|
5. **Consider**: May need different approach
|
|
|
|
### If Tests Fail
|
|
1. **Stop**: Do not proceed to next phase
|
|
2. **Rollback**: To Express backend
|
|
3. **Debug**: Fix failing tests
|
|
4. **Retest**: Ensure all pass
|
|
5. **Proceed**: Only when 100% pass rate
|
|
|
|
## 🚀 Success Metrics
|
|
|
|
### Performance Targets (MUST ACHIEVE)
|
|
- **Requests/Second**: 2-3x improvement
|
|
- **Response Latency**: 50-70% reduction
|
|
- **Memory Usage**: Equal or better
|
|
- **CPU Efficiency**: Better utilization
|
|
|
|
### Quality Targets (MUST ACHIEVE)
|
|
- **Test Pass Rate**: 100%
|
|
- **API Compatibility**: 100%
|
|
- **Feature Parity**: 100%
|
|
- **Error Handling**: Identical behavior
|
|
|
|
## 🔗 Handoff Information
|
|
|
|
### Handoff Prompt for Future Claude
|
|
```
|
|
Continue MotoVaultPro Phase 7 (Vehicles Fastify). Check PHASE-07-Vehicles-Fastify.md for steps. CRITICAL: This is high-risk core feature migration. Docker from Phase 6 should be complete. Migrate vehicles feature from Express to Fastify maintaining 100% compatibility. Test extensively before proceeding.
|
|
```
|
|
|
|
### Prerequisites Verification
|
|
```bash
|
|
# Verify Phase 6 complete
|
|
docker images | grep mvp # Should show optimized images
|
|
make dev # Should work with modern Docker setup
|
|
|
|
# Verify vehicles currently working
|
|
curl -H "Authorization: Bearer $TOKEN" http://localhost:3001/api/vehicles
|
|
# Should return vehicle data
|
|
```
|
|
|
|
## 📝 Migration Strategy Summary
|
|
|
|
### Phase 7 Approach
|
|
1. **Parallel Implementation** - Build Fastify alongside Express
|
|
2. **Feature Flag Control** - Switch between backends safely
|
|
3. **Comprehensive Testing** - Every feature tested thoroughly
|
|
4. **Performance Validation** - Measure and verify improvements
|
|
5. **Safety First** - Rollback ready at all times
|
|
|
|
### Modified Feature Capsule Preservation
|
|
- Maintain exact same capsule structure
|
|
- Preserve AI-friendly architecture
|
|
- Keep complete isolation between features
|
|
- Maintain comprehensive documentation
|
|
|
|
### Risk Mitigation
|
|
- Parallel implementation reduces risk
|
|
- Feature flags allow instant rollback
|
|
- Comprehensive testing catches issues early
|
|
- Performance monitoring ensures goals met
|
|
|
|
---
|
|
|
|
**Phase 7 Status**: Pending Phase 6 completion
|
|
**CRITICAL PHASE**: Core feature migration - highest risk, highest reward
|
|
**Expected Gain**: 2-3x vehicle API performance improvement |