Skip to content

refactor: from repl-first to cli-first design#5

Merged
edraobdu merged 11 commits intomainfrom
feat/cli-refactor
Dec 28, 2025
Merged

refactor: from repl-first to cli-first design#5
edraobdu merged 11 commits intomainfrom
feat/cli-refactor

Conversation

@edraobdu
Copy link
Owner

CLI Refactor: From REPL-first to CLI-first Design

This PR refactors pgslice from a REPL-first tool to a CLI-first tool with non-interactive dump capabilities, while maintaining the existing REPL mode for interactive use.

Summary

  • Transform pgslice into a proper CLI tool that can stream SQL dumps to stdout
  • Add comprehensive CLI flags for non-interactive dumps (--table, --pks, --timeframe, --truncate, etc.)
  • Introduce dependency graph visualization with --graph flag
  • Add schema introspection commands (--tables, --describe)
  • Centralize shared operations between CLI and REPL modes
  • Add visual progress indicators (spinner + progress bar) for better UX
  • Maintain backward compatibility with existing REPL mode

Key Changes

1. Non-Interactive CLI Mode

New flags for direct dumps without entering REPL:

  • --table TABLE + --pks PK_VALUES: Dump specific records by primary key
  • --timeframe COLUMN:START:END: Filter main table by date range (alternative to --pks)
  • --truncate TABLE:COL:START:END: Apply timeframe filters to related tables (can be repeated)
  • --output FILE: Write to file (default: stdout for easy piping)
  • --wide: Enable wide mode (follow self-referencing FKs)
  • --keep-pks: Keep original primary key values instead of remapping
  • --graph: Display table relationship graph after dump

Examples:

# Dump to stdout
pgslice --host localhost --database mydb --table users --pks 42 > user.sql

# Dump by timeframe
pgslice --table orders --timeframe "created_at:2024-01-01:2024-12-31" > orders.sql

# Dump with truncate filter for related tables
pgslice --table users --pks 1 --truncate "orders:created_at:2024-01-01:2024-12-31" --output dump.sql

2. Schema Introspection Commands

- --tables: List all tables in the schema
- --describe TABLE: Show table structure and relationships

3. Dependency Graph Visualization

- New --graph flag displays ASCII art graph of table relationships
- Shows record counts per table and FK relationships
- Implemented in new utils/graph_visualizer.py module

4. Progress Indicators

- Visual spinner during traversal operations
- Progress bar enabled in both CLI and REPL modes
- Only shown when stderr is a TTY (not when piped)

5. Code Organization Refactoring

New operations/ module centralizes shared logic:
- operations/dump_ops.py: Shared dump execution logic
- operations/parsing.py: Timeframe/truncate filter parsing
- operations/schema_ops.py: List tables and describe table operations

Benefits:
- Eliminates code duplication between CLI and REPL
- Easier to maintain and test
- Cleaner separation of concerns

6. REPL Mode Updates

- Renamed --timeframe → --truncate for clarity (applies to related tables, not main table)
- Enabled progress bar in REPL mode
- Updated to use centralized operations from operations/ module
- Improved help text and error messages

Testing

- 32 files changed: 4,552 additions, 523 deletions
- New test files: 8 new test modules covering all new functionality
- Coverage: Maintained >93% overall code coverage
- Test modules added:
  - tests/unit/operations/test_dump_ops.py
  - tests/unit/operations/test_parsing.py
  - tests/unit/operations/test_schema_ops.py
  - tests/unit/utils/test_graph_visualizer.py
  - tests/unit/utils/test_spinner.py
  - tests/unit/graph/test_traverser_progress.py
  - Expanded tests/unit/test_cli.py with CLI mode tests

Breaking Changes

None - All changes are backward compatible:
- REPL mode continues to work exactly as before
- Only breaking change is internal API (REPL --timeframe → --truncate)
- Existing configurations and workflows remain functional

Migration Guide

No migration needed! Existing REPL users can continue using pgslice as before:
pgslice --host localhost --database mydb
# Type 'help' for commands

New CLI-first users can now use direct commands:
pgslice --host localhost --database mydb --table users --pks 42 > dump.sql

Documentation Updates

- Updated README.md with CLI-first examples
- Added comprehensive examples for all new flags
- Updated development documentation (CLAUDE.md)
- Improved help text in CLI

Test Plan

- All existing tests pass
- New CLI mode tests added
- Operations module fully tested
- Graph visualizer tested
- Progress indicators tested
- Manual testing of CLI and REPL modes
- Coverage maintained above 93%

🤖 Generated with https://claude.com/claude-code

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

@edraobdu edraobdu merged commit a6daf9c into main Dec 28, 2025
6 checks passed
@edraobdu edraobdu deleted the feat/cli-refactor branch December 28, 2025 16:21
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.

1 participant