Detect missing tables in DB health check and always show schema fix button

This commit is contained in:
Troll (Hermes Agent) 2026-08-04 16:59:54 +00:00
parent cda4d57859
commit a3fd5938a5
2 changed files with 29 additions and 11 deletions

23
app.py
View file

@ -1072,7 +1072,7 @@ def admin_settings():
n /= 1024
return f"{n:.2f} TB"
# Health check: verify expected columns exist.
# Health check: verify expected columns exist and expected tables exist.
expected_cols = {
'id', 'created_at', 'name', 'email', 'hobbies', 'notable_facts',
'style_genre', 'extra_requests', 'vocal_gender', 'status', 'suno_title', 'suno_style',
@ -1080,16 +1080,27 @@ def admin_settings():
'approval_notified_at', 'preview_sent_at', 'delivery_sent_at',
'square_payment_ref', 'admin_alert_email', 'player_token', 'revision_note', 'revision_count', 'operator_notes', 'stems_link'
}
health = {'ok': True, 'missing_columns': [], 'message': 'Database schema looks good.'}
expected_tables = {'requests', 'revision_history'}
health = {'ok': True, 'missing_columns': [], 'missing_tables': [], 'message': 'Database schema looks good.'}
try:
db = get_db()
cur = db.execute("SELECT name FROM sqlite_master WHERE type='table'")
existing_tables = {row['name'] for row in cur.fetchall()}
missing_tables = sorted(expected_tables - existing_tables)
cur = db.execute('PRAGMA table_info(requests)')
existing_cols = {row['name'] for row in cur.fetchall()}
missing = sorted(expected_cols - existing_cols)
if missing:
health = {'ok': False, 'missing_columns': missing, 'message': f'Missing columns: {", ".join(missing)}'}
missing_cols = sorted(expected_cols - existing_cols)
if missing_tables or missing_cols:
parts = []
if missing_tables:
parts.append(f"missing tables: {', '.join(missing_tables)}")
if missing_cols:
parts.append(f"missing columns: {', '.join(missing_cols)}")
health = {'ok': False, 'missing_columns': missing_cols, 'missing_tables': missing_tables, 'message': 'Database schema issues: ' + '; '.join(parts)}
except Exception as e:
health = {'ok': False, 'missing_columns': [], 'message': f'Could not inspect table: {e}'}
health = {'ok': False, 'missing_columns': [], 'missing_tables': [], 'message': f'Could not inspect database: {e}'}
if request.method == 'POST':
action = request.form.get('action')

View file

@ -201,17 +201,24 @@
<!-- Database health check -->
<div class="section">
<h2>Database Health</h2>
{% if health.ok %}
<p class="status-ok">✅ {{ health.message }}</p>
{% else %}
{% if not health.ok %}
<p class="status-bad">❌ {{ health.message }}</p>
{% if health.missing_columns %}
<p>Missing columns: {{ health.missing_columns | join(', ') }}</p>
{% endif %}
{% if health.missing_tables %}
<p>Missing tables: {{ health.missing_tables | join(', ') }}</p>
{% endif %}
<form method="POST" action="{{ url_for('admin_settings') }}">
<input type="hidden" name="action" value="fix_db">
<button type="submit">Fix Missing Columns</button>
<button type="submit">Fix Database Schema</button>
</form>
{% else %}
<p class="status-ok">✅ {{ health.message }}</p>
<form method="POST" action="{{ url_for('admin_settings') }}" style="margin-top:.5rem">
<input type="hidden" name="action" value="fix_db">
<button type="submit" class="secondary">Recheck / Apply Schema</button>
</form>
{% endif %}
{% endif %}
</div>