|
| 1 | +# Ownership Snapshot Cleanup Scaffold |
| 2 | + |
| 3 | +This scaffold adds a scheduled cleanup job that targets expired ownership snapshot rows. |
| 4 | + |
| 5 | +The implementation is intentionally conservative: |
| 6 | + |
| 7 | +- it runs only when explicitly enabled |
| 8 | +- it supports dry-run mode by default |
| 9 | +- it skips cleanup if the target table does not exist yet |
| 10 | + |
| 11 | +## Environment variables |
| 12 | + |
| 13 | +Use the following variables to control behavior: |
| 14 | + |
| 15 | +- `OWNERSHIP_SNAPSHOT_CLEANUP_ENABLED` (default: `false`) |
| 16 | +- `OWNERSHIP_SNAPSHOT_CLEANUP_INTERVAL_MINUTES` (default: `60`) |
| 17 | +- `OWNERSHIP_SNAPSHOT_RETENTION_DAYS` (default: `30`) |
| 18 | +- `OWNERSHIP_SNAPSHOT_CLEANUP_DRY_RUN` (default: `true`) |
| 19 | +- `OWNERSHIP_SNAPSHOT_TABLE_NAME` (default: `creator_ownership_snapshots`) |
| 20 | + |
| 21 | +## How cleanup works |
| 22 | + |
| 23 | +On each run, the job computes a cutoff timestamp as: |
| 24 | + |
| 25 | +`now - OWNERSHIP_SNAPSHOT_RETENTION_DAYS` |
| 26 | + |
| 27 | +Then it checks whether the configured table exists. |
| 28 | + |
| 29 | +- In dry-run mode, it only counts rows where `expiresAt < cutoff`. |
| 30 | +- In non-dry-run mode, it deletes rows where `expiresAt < cutoff`. |
| 31 | + |
| 32 | +## Local validation steps |
| 33 | + |
| 34 | +1. Configure `.env`: |
| 35 | + |
| 36 | +```bash |
| 37 | +OWNERSHIP_SNAPSHOT_CLEANUP_ENABLED=true |
| 38 | +OWNERSHIP_SNAPSHOT_CLEANUP_INTERVAL_MINUTES=1 |
| 39 | +OWNERSHIP_SNAPSHOT_RETENTION_DAYS=30 |
| 40 | +OWNERSHIP_SNAPSHOT_CLEANUP_DRY_RUN=true |
| 41 | +OWNERSHIP_SNAPSHOT_TABLE_NAME=creator_ownership_snapshots |
| 42 | +``` |
| 43 | + |
| 44 | +2. Start the server: |
| 45 | + |
| 46 | +```bash |
| 47 | +pnpm dev |
| 48 | +``` |
| 49 | + |
| 50 | +3. Confirm logs show one of the following: |
| 51 | + |
| 52 | +- `Ownership snapshot cleanup dry-run completed` |
| 53 | +- `Ownership snapshot cleanup skipped because target table was not found` |
| 54 | + |
| 55 | +4. Run tests for the scaffold: |
| 56 | + |
| 57 | +```bash |
| 58 | +pnpm exec jest src/jobs/ownership-snapshot-cleanup.job.test.ts |
| 59 | +``` |
| 60 | + |
| 61 | +5. Optional delete validation (only when safe): set `OWNERSHIP_SNAPSHOT_CLEANUP_DRY_RUN=false`, run locally against non-production data, and verify delete counts in logs. |
0 commit comments