Skip to content

[SECURITY] Automated DB backup/restore + data corruption detection - closes #62 #63#82

Merged
sjackson0109 merged 8 commits into
sjackson0109:mainfrom
Ibrahim-3d:feat/62-63-database-backup-validation
May 24, 2026
Merged

[SECURITY] Automated DB backup/restore + data corruption detection - closes #62 #63#82
sjackson0109 merged 8 commits into
sjackson0109:mainfrom
Ibrahim-3d:feat/62-63-database-backup-validation

Conversation

@Ibrahim-3d

Copy link
Copy Markdown
Collaborator

Summary

Changes

  • New: app/pt_backup.py
  • Modified: app/pt_validation.pyDataIntegrityValidator class added (no breaking changes)
  • New: app/test_backup_validation.py — 35 tests

Usage

# Backup
from pt_backup import DatabaseBackupManager
mgr = DatabaseBackupManager("order_management.db", max_backups=30)
mgr.start_scheduler()  # auto-backup every 24h

# Manual + restore
record = mgr.create_backup()
mgr.restore(record.backup_id)

# Data integrity
from pt_validation import DataIntegrityValidator
violations = DataIntegrityValidator.check_ohlcv_consistency(candle)
result = DataIntegrityValidator.check_batch_integrity(records, ["price", "volume"])

Test plan

  • 35 unit tests pass
  • Backup checksum tamper detection tested
  • Restore atomicity verified (tmp → rename)
  • Retention pruning verified
  • OHLCV violations all caught
  • NaN/Inf detection across all numeric types

Closes #62
Closes #63

Copilot AI review requested due to automatic review settings May 17, 2026 20:36
@Ibrahim-3d Ibrahim-3d requested a review from sjackson0109 as a code owner May 17, 2026 20:36
@sjackson0109 sjackson0109 self-assigned this May 17, 2026
@sjackson0109 sjackson0109 added component-risk Risk management component-database Database operations component-monitoring Monitoring and observability components component-architecture System architecture and design components labels May 17, 2026
sjackson0109
sjackson0109 previously approved these changes May 17, 2026

@sjackson0109 sjackson0109 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Another outstanding contribution from Ibrahim.

@Ibrahim-3d

Copy link
Copy Markdown
Collaborator Author

Local CI Simulation Results

Ran all workflow steps locally against this branch prior to owner approval. Results:

Code Quality & Testing

Check Tool Result
Code formatting black --check ✅ Pass — 0 files need reformatting
Import ordering isort --check ✅ Pass
Linting (hard errors) flake8 --select=E9,F63,F7,F82 ✅ Pass — 0 syntax/undefined-name errors
Linting (extended) flake8 --exit-zero ✅ Pass — 0 warnings in new files
Unit tests pytest ✅ All tests pass (see table below)

Unit Test Results

File Tests Result
test_circuit_breaker.py 20 ✅ All pass
test_credentials_rotation.py 30 ✅ All pass
test_error_handler.py 22 ✅ All pass
test_backup_validation.py 35 ✅ All pass
test_database_manager.py 29 ✅ All pass
test_security_logger.py 25 ✅ All pass

CI/CD Pipeline

Workflow Status Reason
Code Quality & Testing ⏳ Awaiting owner approval Fork PR — first-time contributor gate
PowerTrader AI+ CI/CD ⏳ Awaiting owner approval Fork PR — first-time contributor gate
Project Management ⏳ Awaiting owner approval Fork PR — first-time contributor gate

All three workflows are queued with action_required status. This is GitHub's security gate for PRs from fork contributors. Once approved by @sjackson0109, they will execute against the same code that was verified locally above.

pt_backup.py: DatabaseBackupManager with SHA-256 verified SQLite backups,
WAL checkpoint, PRAGMA integrity_check, point-in-time restore, retention
pruning, and background scheduler.

pt_validation.py: DataIntegrityValidator with NaN/Inf detection, OHLCV
cross-field consistency checks, Z-score price spike detection, checksum
verification, and batch integrity scanning.

- 35 unit tests, all passing
@Ibrahim-3d Ibrahim-3d force-pushed the feat/62-63-database-backup-validation branch from d569b0f to 59b1284 Compare May 18, 2026 18:39
@sjackson0109

Copy link
Copy Markdown
Owner

@Ibrahim-3d can you review the pipeline failures and refactor to re-try submission?

Black check failed in CI due to rebase conflict resolution leaving
pt_validation.py unformatted. Auto-formatted with Black.
@Ibrahim-3d

Copy link
Copy Markdown
Collaborator Author

Pipeline failure investigated and fixed. The CI job was failing on Black formatting — specifically (the file that had a merge conflict during the rebase onto sjackson0109's Black-reformatted main). The conflict was resolved correctly for logic but the file was left unformatted. Fixed: ran Black on and committed. All other 97 files were already compliant — Black reported '1 file would be reformatted, 97 files would be left unchanged'.

@Ibrahim-3d

Copy link
Copy Markdown
Collaborator Author

Manual Testing Guide — PR #82: Automated DB Backup/Restore + Corruption Detection

Prerequisites

git fetch fork && git checkout feat/62-63-database-backup-validation
cd app

1. Run unit tests

python -m pytest test_backup_validation.py -v

Expected: All 35 tests pass.

2. CI Black formatting check (this was the pipeline failure)

uvx black --check app/pt_validation.py
# or: black --check app/pt_validation.py

Expected: 1 file would be left unchanged — no reformatting needed.

3. Smoke test — backup and restore

python -c "
import tempfile, os, sqlite3
from pt_backup_validation import DatabaseBackupManager  # adjust import if needed

with tempfile.TemporaryDirectory() as d:
    db = os.path.join(d, 'trade.db')
    backup = os.path.join(d, 'trade.db.bak')
    conn = sqlite3.connect(db)
    conn.execute('CREATE TABLE orders (id INTEGER PRIMARY KEY, symbol TEXT)')
    conn.execute(\"INSERT INTO orders VALUES (1, 'BTC-USD')\")
    conn.commit()
    conn.close()
    print('DB created. Run backup + restore manually via BackupManager.')
    print('Verify: backup file exists, restore reproduces the row.')
"

4. Verify data integrity checksum detects corruption

Refer to test_backup_validation.py::TestDataIntegrityValidator — run those tests individually to confirm hash mismatch detection works on your filesystem.

Rollback

git checkout main -- app/pt_validation.py

@Ibrahim-3d

Copy link
Copy Markdown
Collaborator Author

/gemini review

@Ibrahim-3d

Ibrahim-3d commented May 21, 2026

Copy link
Copy Markdown
Collaborator Author

Hold this one for me please - I want to walk back some unintended changes first. While building the new validator I also stripped docstrings and inline comments off the existing InputValidator methods, which wasn't part of the issue scope and just adds noise to the review. Going to restore those and keep this PR limited to the actual backup + integrity work. Back shortly.

PR82 review feedback: the prior commit removed docstrings and inline
comments from existing InputValidator methods that were unrelated to
the data-integrity additions. Rebuild pt_validation.py from main and
re-apply only the issue-scoped additions (DataCorruptionError,
DataIntegrityValidator, hashlib/math/Tuple imports, module docstring
update).
@Ibrahim-3d

Ibrahim-3d commented May 23, 2026

Copy link
Copy Markdown
Collaborator Author

Done. I rebuilt pt_validation.py from main so every original docstring and inline comment in InputValidator is back the way it was, then re-applied only the additions this issue actually needs (DataCorruptionError, DataIntegrityValidator, plus the hashlib/math/Tuple imports it needs). Diff is now focused on just the new stuff. 35 tests passing.

@Ibrahim-3d

Copy link
Copy Markdown
Collaborator Author

@copilot review

@sjackson0109 sjackson0109 merged commit fc806cf into sjackson0109:main May 24, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component-architecture System architecture and design components component-database Database operations component-monitoring Monitoring and observability components component-risk Risk management

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] Add comprehensive data validation layers [SECURITY] Create automated backup and restore procedures

2 participants