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:
parent
7ab8388741
commit
3f9de1449c
4 changed files with 37 additions and 10 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# Theme Song Booth
|
# 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.
|
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.
|
||||||
|
|
||||||
|
|
|
||||||
2
VERSION
2
VERSION
|
|
@ -1 +1 @@
|
||||||
0.4.0
|
0.4.1
|
||||||
|
|
|
||||||
13
app.py
13
app.py
|
|
@ -1493,3 +1493,16 @@ def init_db_command():
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Development-only entry point. Production uses gunicorn (see Dockerfile).
|
# Development-only entry point. Production uses gunicorn (see Dockerfile).
|
||||||
app.run(debug=True, host='0.0.0.0')
|
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)
|
||||||
|
|
|
||||||
16
models.py
16
models.py
|
|
@ -184,8 +184,9 @@ 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):
|
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 = get_db()
|
||||||
|
try:
|
||||||
db.execute(
|
db.execute(
|
||||||
"""INSERT INTO revision_history
|
"""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)
|
(request_id, revision_count, note, old_song_a_path, old_song_b_path, new_song_a_path, new_song_b_path)
|
||||||
|
|
@ -193,6 +194,19 @@ def log_revision(request_id, revision_count, note, old_a=None, old_b=None, new_a
|
||||||
(request_id, revision_count, note, old_a, old_b, new_a, new_b)
|
(request_id, revision_count, note, old_a, old_b, new_a, new_b)
|
||||||
)
|
)
|
||||||
db.commit()
|
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):
|
def list_revision_history(request_id):
|
||||||
|
|
|
||||||
Reference in a new issue