Skip to content

Feat/421 wipe events table script#450

Open
Priyanshubhartistm wants to merge 4 commits intocameri:mainfrom
Priyanshubhartistm:feat/421-wipe-events-table-script
Open

Feat/421 wipe events table script#450
Priyanshubhartistm wants to merge 4 commits intocameri:mainfrom
Priyanshubhartistm:feat/421-wipe-events-table-script

Conversation

@Priyanshubhartistm
Copy link
Copy Markdown
Contributor

@Priyanshubhartistm Priyanshubhartistm commented Apr 10, 2026

Description

  • Added a new clean-db CLI utility for safe events table cleanup.
  • Added npm script support with npm run clean-db.
  • Added safety guardrails:
    • Explicit confirmation prompt requiring Type DELETE to confirm.
    • --force option to skip prompt.
    • Extra warning when NODE_ENV is production.
  • Added purge options:
    • --all for full wipe.
    • --older-than=days for timestamp-based cleanup.
    • --kinds=1,7,4 for kind-specific cleanup.
    • --dry-run to show affected row count without deleting.
  • Added PostgreSQL-specific behavior:
    • Full wipe uses TRUNCATE TABLE events, event_tags RESTART IDENTITY CASCADE.
    • Selective delete runs VACUUM ANALYZE events afterward.
  • Added unit tests for clean-db option parsing and validation.
  • Updated README with relay maintenance usage examples.

Related Issue

Closes #421
#421

Motivation and Context

Relay operators need a safe and repeatable way to reset or prune heavy event data during testing and maintenance without running manual SQL commands.

This change improves:

  • Safety with explicit confirmation and force control.
  • Flexibility with multiple cleanup modes.
  • Speed with optimized full-wipe behavior.
  • Clear maintenance documentation.

How Has This Been Tested?

  • npm run lint
  • npm run build
  • npm run test:unit
  • Manual end-to-end verification on a disposable local PostgreSQL instance:
    • Seeded sample events with different kinds and ages.
    • Verified dry-run counts before deletes.
    • Ran selective cleanup and confirmed post-delete dry-run showed zero.
    • Ran full wipe and confirmed post-wipe dry-run showed zero.
    • Verified final database counts were events=0 and event_tags=0.

Screenshots (if appropriate):

N/A (CLI and database maintenance change)

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my code changes.
  • All new and existing tests passed.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new administrative CLI (npm run clean-db) to safely wipe or selectively prune relay event data, aimed at helping operators maintain/test relays without manual SQL.

Changes:

  • Introduces src/clean-db.ts CLI with confirmation/--force, --dry-run, and filter options (--all, --older-than, --kinds), plus Postgres optimizations (TRUNCATE / VACUUM ANALYZE).
  • Adds unit tests for option parsing/validation.
  • Documents relay maintenance usage in README and exposes the CLI via a new npm script.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/clean-db.ts Implements the clean-db CLI, option parsing, confirmation flow, and DB operations (TRUNCATE / DELETE + VACUUM).
test/unit/clean-db.spec.ts Adds unit tests for option parsing/validation behavior and error messaging.
README.md Adds “Relay Maintenance” section with clean-db usage examples.
package.json Adds clean-db npm script entry point.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +128 to +140
if (arg.startsWith('--older-than')) {
const [value, nextIndex] = getOptionValue(arg, args, index)
options.olderThanDays = parseOlderThanDays(value)
index = nextIndex
continue
}

if (arg.startsWith('--kinds')) {
const [value, nextIndex] = getOptionValue(arg, args, index)
options.kinds = parseKinds(value)
index = nextIndex
continue
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseCleanDbOptions matches --older-than / --kinds using startsWith, which means options like --older-than-days or --kinds-extra will be treated as valid and parsed instead of raising Unknown option. Tighten the check to only accept the exact flag (--older-than/--kinds) or an inline-value form (--older-than=/--kinds=).

Copilot uses AI. Check for mistakes.

## Relay Maintenance

Use `clean-db` to wipe or prune only the `events` table data.
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README says clean-db wipes/prunes "only the events table data", but the implementation also removes data from the derived event_tags table (TRUNCATE includes it when present, and selective deletes trigger cleanup). Please clarify the docs to mention event_tags is also affected so operators don’t assume tags remain intact.

Suggested change
Use `clean-db` to wipe or prune only the `events` table data.
Use `clean-db` to wipe or prune `events` table data. This also removes corresponding data from the derived `event_tags` table when present.

Copilot uses AI. Check for mistakes.

if (options.all) {
await runAllDelete(dbClient)
console.log('Deleted all events with TRUNCATE.')
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When --all is used and event_tags exists, runAllDelete truncates both events and event_tags, but the user-facing message says "Deleted all events with TRUNCATE." Consider updating the output to reflect both tables (or otherwise make the scope explicit) to avoid misleading operators.

Suggested change
console.log('Deleted all events with TRUNCATE.')
console.log('Deleted all rows from events and event_tags with TRUNCATE.')

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Wipe events table script

2 participants