Commit 242f03ef authored by Aryan Noshirvani's avatar Aryan Noshirvani

Fix dormant telegram_bot bug: implement init_db/log_correction

src/telegram_bot/bot.py has called src.core.database.init_db() and
log_correction() since before this session started - neither function
existed anywhere in the codebase, not even in the original pre-reorg
flat-file layout. The bot was never deployed, so this never surfaced,
but it would have crashed immediately on first use.

Implemented rather than just removing the calls, since they represent
real intended behavior:
- init_db(): idempotent wrapper around the table creation that already
  runs at module import time - a defensive check for the bot running
  as a standalone process that might start before the API ever has.
- log_correction(): a new Correction model + write, recording a
  user's manual song-ID correction (picked from candidates, typed
  manually, or confirmed a Gemini guess) as a simple audit trail -
  what the call site's arguments (user_id, title, artist, source)
  clearly imply was the original intent.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCxXmJVvDFJVHTUi4QgNzi
parent b2ed559a
Pipeline #1000 passed with stage
in 10 minutes and 15 seconds
......@@ -95,6 +95,15 @@ class LiveRoom(Base):
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
class Correction(Base):
__tablename__ = "corrections"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, index=True)
title = Column(String)
artist = Column(String)
source = Column(String)
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
class RemixJob(Base):
__tablename__ = "remix_jobs"
id = Column(Integer, primary_key=True, index=True)
......@@ -117,3 +126,20 @@ def get_db():
yield db
finally:
db.close()
def init_db():
"""Idempotent - tables are already created at import time above. Exists because
src/telegram_bot/bot.py calls this before starting, as a defensive check for when the
bot runs as a standalone process that may start before the API ever has."""
Base.metadata.create_all(bind=engine)
def log_correction(user_id: int, title: str, artist: str, source: str):
"""Records a user's manual song-ID correction/confirmation from the Telegram bot flow
(picking from candidates, typing manually, or confirming a Gemini guess) as a simple
audit trail - was called from bot.py but never implemented anywhere."""
db = SessionLocal()
try:
db.add(Correction(user_id=user_id, title=title, artist=artist, source=source))
db.commit()
finally:
db.close()
from src.core.database import Correction, SessionLocal, init_db, log_correction
def test_init_db_is_idempotent():
# Should not raise even when tables already exist (called at import time too)
init_db()
init_db()
def test_log_correction_persists_a_row():
log_correction(user_id=42, title="Test Song", artist="Test Artist", source="Manual Entry")
db = SessionLocal()
try:
row = (
db.query(Correction)
.filter(Correction.user_id == 42, Correction.title == "Test Song")
.order_by(Correction.id.desc())
.first()
)
assert row is not None
assert row.artist == "Test Artist"
assert row.source == "Manual Entry"
finally:
db.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment