Fix remove_old_logs() failing to delete stale scan events when scan_compare_id is None
Description
The remove_old_logs() function in nettacker/database/db.py fails to remove outdated scan events when using the default APSW/SQLite backend.
When the same target and module are scanned multiple times, previous scan events remain in the database. Consequently, reports generated through logs_to_report_json/html(target) include stale results from earlier scans.
Root Cause
The default configuration uses APSW (use_apsw_for_sqlite = True).
The current SQL query includes:
DELETE FROM scan_events
WHERE target = ? AND module_name = ?
AND scan_unique_id != ? AND scan_unique_id != ?
When scan_compare_id is None, SQLite evaluates scan_unique_id != NULL as NULL, which does not satisfy the WHERE condition.
As a result, no outdated records are deleted.
The SQLAlchemy implementation handles None differently by generating IS NOT NULL, so the issue primarily affects the default APSW implementation.
Expected Behavior
- Remove outdated scan events for the same target and module.
- Preserve events belonging to the current scan.
- Ensure reports contain only the relevant scan results.
- Prevent unnecessary database growth caused by duplicate stale records.
Proposed Solution
Build the SQL WHERE clause dynamically in both APSW and SQLAlchemy implementations.
When scan_compare_id is None or falsy, omit the second scan_unique_id != ? condition entirely.
Affected Files
nettacker/database/db.pytests/unit/database/test_db.py
Acceptance Criteria
- [ ] Correctly delete outdated events when
scan_compare_id=None. - [ ] Preserve the current scan's events.
- [ ] Handle non-None
scan_compare_idcorrectly. - [ ] Add regression tests covering both scenarios.
- [ ] Ensure existing database tests continue to pass.
I've reproduced this against real APSW and SQLAlchemy connections and prepared a fix: https://github.com/euvoor/Nettacker/compare/f333cee7a56acd887205efa7442f113ec6515c1e...cf5e1c32aa64331ae0072d1e532ddfaed9379d9d
The APSW query now guards the optional comparison ID; the SQLAlchemy path is unchanged. Eight regression cases verify committed cleanup and preservation of the current/comparison scans and other targets/modules. make check passes on Python 3.11: 382 unit tests passed, 6 skipped, 2 expected failures, plus the package test and pre-commit checks.
Could you assign this to me if that scope works? I'll hold the PR until assignment.
@euvoor it was not you but @himanchalkaushale who research identified this issue. Please respect that contribution and give them the opportunity to follow through.
The issue remains unassigned because it is awaiting review, alongside more than 100 other issues and work-in-progress PRs with potentially much higher priority to the project. An unassigned issue does not necessarily mean it is available for someone else to take over. Please do your own research.
Thanks for clarifying the assignment status. To be clear, I said I reproduced the issue, not that I discovered it—credit for the diagnosis belongs to @himanchalkaushale.
The patch deliberately differs from the proposed solution in three ways:
- The proposal changes both APSW and SQLAlchemy. Tests against both confirmed SQLAlchemy already works for these cases. My patch fixes APSW without changing the working backend.
- The proposal drops the comparison condition for
Noneor any falsy value. That includes"". If a scan has that ID, dropping the condition would allow its results to be deleted. My patch treats onlyNoneas absent, preserving existing empty-string behavior. - The proposal dynamically builds the SQL condition. My patch keeps one parameterized query and adds a
NULLguard, achieving the fix without conditional query construction.
I also added eight real-database tests covering those distinctions and checking that cleanup preserves unrelated findings. They confirm the deletion is saved by reading through a fresh connection. Before the fix, the APSW/None case fails; afterward, all 76 database tests pass.
That is the additional value of the contribution: fixing the reported failure while avoiding unnecessary changes and preserving existing behavior.
Hi, @securestep9 Thank you for clarifying. I am happy to implement this. I understand that someone else has already looked into this, so I would not like to duplicate any work they have done or miss any helpful information. Could you please confirm if I can take ownership of this issue and work on a pull request? I am happy to implement the fix and verify it against the existing database tests. Thanks!
Hi, @securestep9 Could you plz assign me?