Detect missing tables in DB health check and always show schema fix button
This commit is contained in:
parent
cda4d57859
commit
a3fd5938a5
2 changed files with 29 additions and 11 deletions
23
app.py
23
app.py
|
|
@ -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')
|
||||
|
|
|
|||
Reference in a new issue