|
| 1 | +# GitHub Copilot Instructions for postgresql-archiver |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This is a high-performance CLI tool written in Go for archiving PostgreSQL partitioned table data to S3-compatible object storage. The project emphasizes performance, data integrity, and user experience with a beautiful TUI (Terminal User Interface) and embedded web-based cache viewer. |
| 6 | + |
| 7 | +## Development Environment |
| 8 | + |
| 9 | +### Language and Version |
| 10 | +- **Go Version**: 1.21 or later (see `go.mod` for the exact version) |
| 11 | +- The project is tested against Go 1.21, 1.22, and 1.23 in CI |
| 12 | + |
| 13 | +### Key Dependencies |
| 14 | +- **AWS SDK for Go**: S3 operations and uploads |
| 15 | +- **Bubble Tea/Bubbles/Lipgloss**: TUI framework for interactive progress display |
| 16 | +- **Cobra**: CLI framework |
| 17 | +- **Viper**: Configuration management |
| 18 | +- **zstd (klauspost/compress)**: High-performance compression |
| 19 | +- **lib/pq**: PostgreSQL driver |
| 20 | +- **WebSocket (github.com/gorilla/websocket)**: Real-time cache viewer updates |
| 21 | + |
| 22 | +## Code Structure |
| 23 | + |
| 24 | +### Main Components |
| 25 | +- `main.go`: Entry point with error handling |
| 26 | +- `cmd/`: Contains all command and business logic |
| 27 | + - `archiver.go`: Core archiving logic |
| 28 | + - `cache.go`: Intelligent caching system for metadata |
| 29 | + - `cache_server.go`: WebSocket server for cache viewer |
| 30 | + - `cache_viewer_html.go`: Embedded HTML/JS for web UI |
| 31 | + - `config.go`: Configuration handling |
| 32 | + - `progress.go`: TUI progress display |
| 33 | + - `pid.go`: Process tracking |
| 34 | + - `root.go`: Cobra command setup |
| 35 | + |
| 36 | +### Test Files |
| 37 | +- Follow Go conventions: `*_test.go` |
| 38 | +- Use `github.com/DATA-DOG/go-sqlmock` for database mocking |
| 39 | +- Tests are in the same package as the code they test |
| 40 | + |
| 41 | +## Coding Standards |
| 42 | + |
| 43 | +### Style Guidelines |
| 44 | +- Follow standard Go conventions and idiomatic Go practices |
| 45 | +- Use `go fmt` for formatting (enforced in CI) |
| 46 | +- Run `go vet` to catch common mistakes |
| 47 | +- Use `staticcheck` for additional linting |
| 48 | +- Run `golangci-lint` for comprehensive linting |
| 49 | + |
| 50 | +### Error Handling |
| 51 | +- Always wrap errors with context using `fmt.Errorf` with `%w` verb |
| 52 | +- Return errors rather than panicking unless absolutely necessary |
| 53 | +- Use descriptive error messages that help debugging |
| 54 | + |
| 55 | +### Naming Conventions |
| 56 | +- Use camelCase for private functions/variables |
| 57 | +- Use PascalCase for exported functions/types |
| 58 | +- Use descriptive names that clearly indicate purpose |
| 59 | +- Struct fields that are exported should be PascalCase |
| 60 | + |
| 61 | +### Comments |
| 62 | +- Document all exported functions, types, and methods |
| 63 | +- Use complete sentences in documentation comments |
| 64 | +- Start documentation comments with the name of the thing being documented |
| 65 | +- Keep comments up-to-date with code changes |
| 66 | + |
| 67 | +## Testing Requirements |
| 68 | + |
| 69 | +### Running Tests |
| 70 | +```bash |
| 71 | +go test ./... # Run all tests |
| 72 | +go test -v ./... # Verbose output |
| 73 | +go test -race ./... # With race detector |
| 74 | +go test -coverprofile=coverage.out ./... # With coverage |
| 75 | +``` |
| 76 | + |
| 77 | +### Test Expectations |
| 78 | +- All new features must include tests |
| 79 | +- Maintain or improve code coverage |
| 80 | +- Use table-driven tests where appropriate |
| 81 | +- Mock external dependencies (database, S3) in unit tests |
| 82 | +- Tests must pass with the race detector enabled |
| 83 | + |
| 84 | +## Build and Development Workflow |
| 85 | + |
| 86 | +### Building |
| 87 | +```bash |
| 88 | +go build -v . # Build for current platform |
| 89 | +go build -v ./... # Build all packages |
| 90 | +``` |
| 91 | + |
| 92 | +### Linting |
| 93 | +```bash |
| 94 | +go vet ./... # Standard Go linting |
| 95 | +staticcheck ./... # Advanced static analysis |
| 96 | +golangci-lint run --timeout=5m # Comprehensive linting |
| 97 | +``` |
| 98 | + |
| 99 | +### CI/CD |
| 100 | +- All PRs must pass CI checks before merging |
| 101 | +- CI runs tests on Go 1.21, 1.22, and 1.23 |
| 102 | +- CI includes: tests, linting, building, and multi-platform builds |
| 103 | +- Coverage is uploaded to Codecov |
| 104 | + |
| 105 | +## Architecture Principles |
| 106 | + |
| 107 | +### Performance |
| 108 | +- Use goroutines for parallel processing (configurable workers) |
| 109 | +- Implement efficient caching to avoid redundant operations |
| 110 | +- Use zstd compression with multi-core support |
| 111 | +- Buffer I/O operations appropriately |
| 112 | + |
| 113 | +### Data Integrity |
| 114 | +- Verify file sizes (compressed and uncompressed) |
| 115 | +- Use MD5 checksums for single-part uploads |
| 116 | +- Use multipart ETags for large files (>100MB) |
| 117 | +- Never skip verification steps |
| 118 | + |
| 119 | +### User Experience |
| 120 | +- Provide real-time progress feedback via TUI |
| 121 | +- Show dual progress bars (per-partition and overall) |
| 122 | +- Enable embedded web viewer for monitoring |
| 123 | +- Display clear error messages with context |
| 124 | +- Support graceful interruption |
| 125 | + |
| 126 | +### Caching Strategy |
| 127 | +- Cache row counts for 24 hours (refreshed daily) |
| 128 | +- Cache file metadata permanently (size, MD5, compression ratio) |
| 129 | +- Skip extraction/compression when cached metadata matches S3 |
| 130 | +- Track errors with timestamps |
| 131 | + |
| 132 | +## Configuration |
| 133 | + |
| 134 | +### Config File |
| 135 | +- Primary config: `~/.postgresql-archiver.yaml` |
| 136 | +- Example config: `example-config.yaml` in repository root |
| 137 | +- Uses Viper for flexible configuration (env vars, flags, config files) |
| 138 | + |
| 139 | +### Required Settings |
| 140 | +- Database connection (host, port, user, password, name) |
| 141 | +- S3 settings (endpoint, bucket, access_key, secret_key, region) |
| 142 | +- Table name (base name without date suffix) |
| 143 | + |
| 144 | +### Optional Settings |
| 145 | +- Workers (parallel processing count, default: 4) |
| 146 | +- Date range (start_date, end_date) |
| 147 | +- Debug mode |
| 148 | +- Dry run mode |
| 149 | +- Cache viewer (enable/disable, port) |
| 150 | + |
| 151 | +## Partition Formats Supported |
| 152 | + |
| 153 | +The archiver handles multiple partition naming formats: |
| 154 | +- `table_YYYYMMDD` (e.g., `messages_20240315`) |
| 155 | +- `table_pYYYYMMDD` (e.g., `messages_p20240315`) |
| 156 | +- `table_YYYY_MM` (e.g., `messages_2024_03`) |
| 157 | + |
| 158 | +## Special Considerations |
| 159 | + |
| 160 | +### Embedded Resources |
| 161 | +- `cache-viewer.html` is embedded into the binary via `cache_viewer_html.go` |
| 162 | +- When modifying the web UI, regenerate the Go file |
| 163 | + |
| 164 | +### WebSocket Communication |
| 165 | +- Real-time updates use WebSocket protocol |
| 166 | +- Auto-reconnecting for reliability |
| 167 | +- JSON message format for cache updates |
| 168 | + |
| 169 | +### S3 Uploads |
| 170 | +- Automatic multipart upload for files >100MB |
| 171 | +- Proper ETag handling for multipart uploads |
| 172 | +- Support for S3-compatible storage (Hetzner, MinIO, etc.) |
| 173 | + |
| 174 | +### Process Management |
| 175 | +- PID file: `/tmp/postgresql-archiver.pid` |
| 176 | +- Task info file: `/tmp/postgresql-archiver-task.json` |
| 177 | +- Clean up on exit or interrupt |
| 178 | + |
| 179 | +## Dependencies Management |
| 180 | + |
| 181 | +### Adding Dependencies |
| 182 | +- Use `go get` to add new dependencies |
| 183 | +- Run `go mod tidy` to clean up |
| 184 | +- Verify with `go mod verify` |
| 185 | +- Check for vulnerabilities with `govulncheck` or similar security scanning tools |
| 186 | + |
| 187 | +### Updating Dependencies |
| 188 | +- Review changes carefully, especially for core dependencies |
| 189 | +- Run full test suite after updates |
| 190 | +- Update go.mod and go.sum together |
| 191 | + |
| 192 | +## Common Patterns |
| 193 | + |
| 194 | +### Error Style |
| 195 | +```go |
| 196 | +if err != nil { |
| 197 | + return fmt.Errorf("descriptive message: %w", err) |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +### Logging with Lipgloss |
| 202 | +```go |
| 203 | +errorStyle := lipgloss.NewStyle(). |
| 204 | + Foreground(lipgloss.Color("#FF0000")). |
| 205 | + Bold(true) |
| 206 | +fmt.Fprintln(os.Stderr, errorStyle.Render("❌ Error: "+err.Error())) |
| 207 | +``` |
| 208 | + |
| 209 | +### Struct Initialization |
| 210 | +```go |
| 211 | +archiver := &Archiver{ |
| 212 | + config: cfg, |
| 213 | + progressChan: make(chan tea.Cmd, 100), |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +## Documentation |
| 218 | + |
| 219 | +### README |
| 220 | +- Keep README.md up-to-date with features |
| 221 | +- Include screenshots for visual features |
| 222 | +- Document configuration options |
| 223 | +- Provide usage examples |
| 224 | + |
| 225 | +### Code Documentation |
| 226 | +- Use godoc-compatible comments |
| 227 | +- Document exported types and functions |
| 228 | +- Include examples in complex functions |
| 229 | + |
| 230 | +## Security |
| 231 | + |
| 232 | +### Credentials |
| 233 | +- Never commit credentials or secrets |
| 234 | +- Use environment variables or config files |
| 235 | +- `.gitignore` includes common credential files |
| 236 | + |
| 237 | +### Validation |
| 238 | +- Validate all user input |
| 239 | +- Sanitize file paths |
| 240 | +- Verify S3 operations before execution |
| 241 | + |
| 242 | +## Contributing Workflow |
| 243 | + |
| 244 | +1. Create a feature branch from `main` |
| 245 | +2. Make changes following these guidelines |
| 246 | +3. Write/update tests for your changes |
| 247 | +4. Run the full test suite locally |
| 248 | +5. Run linters and fix any issues |
| 249 | +6. Build for multiple platforms to ensure compatibility |
| 250 | +7. Submit a PR to `main` |
| 251 | +8. Ensure CI passes before requesting review |
| 252 | + |
| 253 | +## Additional Notes |
| 254 | + |
| 255 | +- The project uses Bubble Tea for TUI, which has its own event loop and message-passing architecture |
| 256 | +- Be careful with goroutine management to avoid leaks |
| 257 | +- The cache system is critical for performance—don't bypass it without good reason |
| 258 | +- The web viewer uses embedded HTML/JS, not a separate frontend build process |
0 commit comments