mirror of
https://github.com/MarekWo/mc-webui.git
synced 2026-07-06 09:51:14 +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:
@@ -4389,6 +4389,44 @@ def update_retention_settings_api():
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/db/vacuum', methods=['POST'])
|
||||
def vacuum_database_api():
|
||||
"""Run SQLite VACUUM to reclaim space freed by DELETE statements.
|
||||
|
||||
Returned payload includes size_before / size_after / freed (bytes) and
|
||||
elapsed_seconds so the UI can show how much space was reclaimed.
|
||||
"""
|
||||
try:
|
||||
db = _get_db()
|
||||
if db is None:
|
||||
return jsonify({'success': False, 'error': 'Database not available'}), 500
|
||||
stats = db.vacuum()
|
||||
logger.info(
|
||||
f"Manual VACUUM: {stats['size_before']:,} -> {stats['size_after']:,} "
|
||||
f"bytes (freed {stats['freed']:,} in {stats['elapsed_seconds']}s)"
|
||||
)
|
||||
return jsonify({'success': True, **stats})
|
||||
except Exception as e:
|
||||
logger.error(f"VACUUM failed: {e}", exc_info=True)
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
|
||||
@api_bp.route('/db/size', methods=['GET'])
|
||||
def get_database_size_api():
|
||||
"""Return current database file size in bytes (for the Optimize DB UI)."""
|
||||
try:
|
||||
db = _get_db()
|
||||
if db is None:
|
||||
return jsonify({'success': False, 'error': 'Database not available'}), 500
|
||||
from pathlib import Path
|
||||
path = Path(db.db_path)
|
||||
size = path.stat().st_size if path.exists() else 0
|
||||
return jsonify({'success': True, 'size': size, 'path': str(path)})
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to read DB size: {e}")
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Read Status (Server-side message read tracking)
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user