feat: Complete versioning system transformation

This commit is contained in:
gpt-engineer-app[bot]
2025-10-15 18:12:52 +00:00
parent ea78aff4a7
commit 8cd38234fa
4 changed files with 352 additions and 1 deletions

View File

@@ -44,3 +44,88 @@ await supabase.rpc('set_session_variable', {
value: auth.uid(), // Moderator who approved
});
```
## Automated Cleanup
### pg_cron Schedule
The versioning system automatically cleans up old versions monthly via `pg_cron`:
- **Schedule:** 2 AM UTC on the 1st of each month
- **Retention:** 50 most recent versions per entity (configurable)
- **Tables affected:** All `*_versions` tables (`park_versions`, `ride_versions`, `company_versions`, `ride_model_versions`)
### Manual Cleanup
To run cleanup manually for a specific entity type:
```sql
-- Clean up old park versions, keeping 50 most recent per park
SELECT cleanup_old_versions('park', 50);
-- Clean up rides, keeping 100 versions
SELECT cleanup_old_versions('ride', 100);
```
Or use the TypeScript utility:
```typescript
import { cleanupVersions } from '@/lib/versioningUtils';
// Clean up old park versions
const deletedCount = await cleanupVersions('park', 50);
console.log(`Deleted ${deletedCount} old versions`);
```
### Monitoring Cleanup Jobs
Check pg_cron job status:
```sql
-- View all cleanup jobs
SELECT * FROM cron.job WHERE jobname LIKE 'cleanup%';
-- View recent job execution history
SELECT * FROM cron.job_run_details
WHERE jobname LIKE 'cleanup%'
ORDER BY start_time DESC
LIMIT 10;
```
### Adjusting Retention Policy
To keep more or fewer versions, update the cron schedule:
```sql
-- Unschedule existing job
SELECT cron.unschedule('cleanup-park-versions');
-- Reschedule with new retention count
SELECT cron.schedule(
'cleanup-park-versions',
'0 2 1 * *', -- 2 AM UTC on 1st of month
$$SELECT cleanup_old_versions('park', 100);$$ -- Keep 100 versions
);
```
### Version Storage Monitoring
Monitor version table sizes:
```sql
-- Count versions per entity type
SELECT 'parks' as entity_type, COUNT(*) as total_versions
FROM park_versions
UNION ALL
SELECT 'rides', COUNT(*) FROM ride_versions
UNION ALL
SELECT 'companies', COUNT(*) FROM company_versions
UNION ALL
SELECT 'ride_models', COUNT(*) FROM ride_model_versions;
-- Get oldest version date per entity type
SELECT 'parks' as entity_type, MIN(created_at) as oldest_version
FROM park_versions
UNION ALL
SELECT 'rides', MIN(created_at) FROM ride_versions;
```

View File

@@ -33,8 +33,64 @@ The new system uses dedicated relational tables:
- ✅ All new versions written to relational tables
- ✅ Triggers active on all entity tables
- ✅ Automated cleanup scheduled via pg_cron
- ⚠️ Old `entity_versions` table retained for backward compatibility
- ⚠️ `src/lib/versioningHelpers.ts` deprecated but not removed
- ⚠️ `src/lib/versioningHelpers.ts` deprecated but not removed (scheduled for removal: 2025-12-01)
## Migration Timeline
### ✅ Phase 1: New System Deployed (Completed)
- Relational version tables created (`park_versions`, `ride_versions`, etc.)
- Triggers enabled on all entity tables
- RLS policies active and tested
- Frontend integrated with new hooks
- Complete documentation suite created
### 🟡 Phase 2: Parallel Operation (Current - Days 1-30)
- Both old and new systems exist side-by-side
- New triggers create versions in relational tables
- Old JSONB table receives no new data
- Monitoring for issues and edge cases
- `versioningHelpers.ts` marked as deprecated
**Action Items:**
- [ ] Monitor version creation in new tables
- [ ] Verify no new inserts to old `entity_versions` table
- [ ] Search codebase for deprecated function usage
- [ ] Collect feedback from team
### 🔵 Phase 3: Archive Legacy Data (Day 30)
- Archive old `entity_versions` to `entity_versions_archive`
- Verify data integrity and counts match
- Keep archive for 60 more days as safety net
- Document archive location and access procedures
**SQL Migration:**
```sql
-- See supabase/migrations/*_archive_legacy_versions.sql
CREATE TABLE entity_versions_archive (LIKE entity_versions INCLUDING ALL);
INSERT INTO entity_versions_archive SELECT * FROM entity_versions;
```
### 🟢 Phase 4: Drop Legacy Tables (Day 90)
- Drop old `entity_versions` table
- Drop old RPC functions (`create_entity_version`, `compare_versions`, etc.)
- Remove `src/lib/versioningHelpers.ts` file
- Remove archive table (or retain indefinitely for audit)
- Update all documentation to remove references to old system
**SQL Migration:**
```sql
-- See supabase/migrations/*_drop_legacy_versions.sql
DROP TABLE entity_versions CASCADE;
DROP FUNCTION create_entity_version(...);
```
### 🚀 Phase 5: Optimization (Ongoing)
- Automated cleanup via pg_cron (monthly)
- Performance monitoring and index tuning
- Documentation updates based on usage patterns
- Version retention policy adjustments as needed
## Backfill Script (Optional)