Fix 500 on customer revision when revision_history table is missing

Deployed databases can persist without the revision_history table,
causing /play/<token>/revise to crash with sqlite3.OperationalError.

- log_revision() now creates the table on the fly if it is missing.
- app.py runs init_db() at import time (safe CREATE TABLE IF NOT EXISTS)
  so new deployments auto-create missing tables on startup.

Bump patch version 0.4.0 -> 0.4.1.
This commit is contained in:
Troll (Hermes Agent) 2026-08-05 13:42:10 +00:00
parent 7ab8388741
commit 3f9de1449c
4 changed files with 37 additions and 10 deletions

View file

@ -1,6 +1,6 @@
# Theme Song Booth
**Version:** `v0.4.0`
**Version:** `v0.4.1`
A Flask web app for a convention booth where visitors request a custom AI-generated theme song, the operator manages the queue, and the final MP3(s) are delivered by email after payment.

View file

@ -1 +1 @@
0.4.0
0.4.1

13
app.py
View file

@ -1493,3 +1493,16 @@ def init_db_command():
if __name__ == '__main__':
# Development-only entry point. Production uses gunicorn (see Dockerfile).
app.run(debug=True, host='0.0.0.0')
# Ensure the database file and expected tables exist when the app is imported by
# gunicorn in production. init_db() uses CREATE TABLE IF NOT EXISTS, so this is
# safe to run on every startup without wiping data.
with app.app_context():
try:
init_db()
except Exception:
# If the database path is not yet reachable (e.g. volume not mounted),
# defer to the first request or the explicit init-db command.
import logging
logging.getLogger('app').warning('Startup init_db() failed; database may need manual initialization.', exc_info=True)

View file

@ -184,15 +184,29 @@ def delete_request(request_id):
def log_revision(request_id, revision_count, note, old_a=None, old_b=None, new_a=None, new_b=None):
"""Record a revision event in the revision_history table."""
"""Record a revision event in the revision_history table, creating the table if it is missing."""
db = get_db()
db.execute(
"""INSERT INTO revision_history
(request_id, revision_count, note, old_song_a_path, old_song_b_path, new_song_a_path, new_song_b_path)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(request_id, revision_count, note, old_a, old_b, new_a, new_b)
)
db.commit()
try:
db.execute(
"""INSERT INTO revision_history
(request_id, revision_count, note, old_song_a_path, old_song_b_path, new_song_a_path, new_song_b_path)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(request_id, revision_count, note, old_a, old_b, new_a, new_b)
)
db.commit()
except sqlite3.OperationalError as e:
if 'no such table' in str(e):
# Schema drift: table missing. Recreate and retry once.
db.executescript(SCHEMA)
db.execute(
"""INSERT INTO revision_history
(request_id, revision_count, note, old_song_a_path, old_song_b_path, new_song_a_path, new_song_b_path)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(request_id, revision_count, note, old_a, old_b, new_a, new_b)
)
db.commit()
else:
raise
def list_revision_history(request_id):