Refactor model imports and update admin classes to use pghistory for historical tracking; replace HistoricalModel with TrackedModel in relevant models

This commit is contained in:
pacnpal
2025-02-09 11:20:40 -05:00
parent 7ecf43f1a4
commit b7f6c60682
24 changed files with 1729 additions and 137 deletions

View File

@@ -0,0 +1,90 @@
# History Tracking Migration
## Context
The project is transitioning from django-simple-history to django-pghistory for model history tracking.
## Implementation Details
### Base Implementation (history_tracking/models.py)
- Both old and new implementations maintained during transition:
- `HistoricalModel` - Legacy base class using django-simple-history
- `TrackedModel` - New base class using django-pghistory
- Custom `DiffMixin` for comparing historical records
- Maintained `HistoricalSlug` for backward compatibility
### Transition Strategy
1. Maintain Backward Compatibility
- Keep both HistoricalModel and TrackedModel during transition
- Update models one at a time to use TrackedModel
- Ensure no breaking changes during migration
2. Model Updates
- Designer (Completed)
- Migrated to TrackedModel
- Updated get_by_slug to use pghistory queries
- Removed SimpleHistoryAdmin dependency
- Pending Model Updates
- Companies (Company, Manufacturer)
- Parks (Park, ParkArea)
- Rides (Ride, RollerCoasterStats)
- Location models
### Migration Process
1. For Each Model:
- Switch base class from HistoricalModel to TrackedModel
- Update admin.py to remove SimpleHistoryAdmin
- Create and apply migrations
- Test history tracking functionality
- Update any history-related queries
2. Testing Steps
- Create test objects
- Make changes
- Verify history records
- Check diff functionality
- Validate historical slug lookup
3. Admin Integration
- Remove SimpleHistoryAdmin
- Use standard ModelAdmin
- Keep existing list displays and search fields
## Benefits
- Native PostgreSQL trigger-based tracking
- More efficient storage and querying
- Better performance characteristics
- Context tracking capabilities
## Rollback Plan
Since both implementations are maintained:
1. Revert model inheritance to HistoricalModel
2. Restore SimpleHistoryAdmin
3. Keep existing migrations
## Next Steps
1. Create migrations for Designer model
2. Update remaining models in this order:
a. Companies app
b. Parks app
c. Rides app
d. Location app
3. Test historical functionality
4. Once all models are migrated:
- Remove HistoricalModel class
- Remove django-simple-history dependency
- Update documentation
## Technical Notes
- Uses pghistory's default tracking configuration
- Maintains compatibility with existing code patterns
- Custom diff functionality preserved
- Historical slug tracking unchanged
- Both tracking systems can coexist during migration
## Completion Criteria
1. All models migrated to TrackedModel
2. All functionality tested and working
3. No dependencies on django-simple-history
4. Documentation updated to reflect new implementation
5. All migrations applied successfully

View File

@@ -0,0 +1,98 @@
# PGHistory Migration Progress
## All Migrations Complete! 🎉
### Latest Migration
- `location/migrations/0002_locationevent_remove_historicallocation_content_type_and_more.py`
- Created LocationEvent model
- Removed simple-history fields
- Set up pghistory triggers
- Cleaned up historical models
### Previously Completed Migrations
1. Companies App
- Created CompanyEvent and ManufacturerEvent
- Removed Designer model
- Set up pghistory triggers
2. Rides App
- Created RideEvent and RideModelEvent
- Removed simple-history fields
- Updated Designer foreign key
- Set up pghistory triggers
3. Parks App
- Created ParkEvent and ParkAreaEvent models
- Set up pghistory tracking triggers
- Removed simple-history fields and models
4. Designers App
- Created DesignerEvent model
- Set up insert/update triggers
- Full pghistory implementation
5. Moderation Models
- Created EditSubmissionEvent model
- Created PhotoSubmissionEvent model
- Set up triggers for both models
## Infrastructure Updates
1. History Tracking App
- Removed simple-history initialization from apps.py
- Updated base models to use pghistory
- Added DiffMixin for tracking changes
## Final Steps
### 1. Remove django-simple-history
```bash
# Update requirements.txt
- Remove django-simple-history==3.8.0
```
### 2. Clean Up Configuration
- Remove any remaining simple-history settings
- Update documentation for new history tracking
- Add migration guide for future models
### 3. Testing
1. Test all models:
- Create/Update/Delete operations
- Historical queries
- Change tracking
- Event context
2. Verify functionality:
- Slug history lookups
- Model relationships
- Admin interfaces
### 4. Documentation Updates
1. Update model documentation
2. Add pghistory usage examples
3. Document migration patterns
4. Update contributor guide
## Technical Notes
- PGHistory tracking implemented via triggers
- Event models store complete history
- Foreign key relationships preserved
- Context tracking available
- GeoDjango fields supported
- Improved query performance expected
## Migration Statistics
✅ Designer Model
✅ Moderation Models
✅ Companies Models
✅ Rides Models
✅ Parks Models
✅ Location Models
## Lessons Learned
1. Keep backward compatibility during transition
2. Migrate models in dependency order
3. Test thoroughly after each migration
4. Update related code incrementally
5. Maintain documentation throughout