mirror of
https://github.com/MarekWo/mc-webui.git
synced 2026-08-06 16:53:21 +02:00
feat(db): VACUUM after retention and an Optimize button in Backup modal
SQLite DELETE marks pages free but doesn't shrink the file, so the new retention job would keep DBs at their bloated size forever without a follow-up VACUUM. Add db.vacuum() that runs PRAGMA-free VACUUM and reports size_before/size_after/elapsed so callers can surface results. The retention job now calls vacuum() automatically when it deleted at least 1000 rows. Threshold avoids the multi-second VACUUM cost on quiet days. Failure is logged, not raised — a missed VACUUM never crashes the scheduler. Power-user override: new "Optimize now" button in the Database Backup modal triggers VACUUM on demand via POST /api/db/vacuum, alongside a GET /api/db/size that drives the live "Current size" label. This way users don't have to wait until 03:30 to reclaim space after the first big retention pass. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1349,6 +1349,30 @@ class Database:
|
||||
|
||||
return result
|
||||
|
||||
def vacuum(self) -> dict:
|
||||
"""Run VACUUM to reclaim space freed by DELETEs.
|
||||
|
||||
VACUUM rewrites the entire database file, so it briefly takes an
|
||||
exclusive lock — writers wait, readers continue in WAL mode. Caller
|
||||
gets back the size before/after and the duration so it can be
|
||||
surfaced to the user.
|
||||
"""
|
||||
size_before = self.db_path.stat().st_size if self.db_path.exists() else 0
|
||||
started = time.time()
|
||||
conn = sqlite3.connect(str(self.db_path), timeout=30, isolation_level=None)
|
||||
try:
|
||||
conn.execute("VACUUM")
|
||||
finally:
|
||||
conn.close()
|
||||
elapsed = time.time() - started
|
||||
size_after = self.db_path.stat().st_size if self.db_path.exists() else 0
|
||||
return {
|
||||
'size_before': size_before,
|
||||
'size_after': size_after,
|
||||
'freed': size_before - size_after,
|
||||
'elapsed_seconds': round(elapsed, 2),
|
||||
}
|
||||
|
||||
# ================================================================
|
||||
# Backup
|
||||
# ================================================================
|
||||
|
||||
Reference in New Issue
Block a user