Replace Alembic with plain SQL schema
- Fix scalar_first() bug in persistent_conversation.py (use scalars().first()) - Add schema.sql with all 7 tables (users, user_preferences, user_facts, guilds, guild_members, conversations, messages) - Update database.py to run schema.sql on startup - Remove Alembic directory and configuration - Remove alembic from requirements.txt
This commit is contained in:
@@ -2,15 +2,19 @@
|
||||
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from daemon_boyfriend.config import settings
|
||||
from daemon_boyfriend.models.base import Base
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Path to schema.sql (project root)
|
||||
SCHEMA_PATH = Path(__file__).parent.parent.parent.parent / "schema.sql"
|
||||
|
||||
|
||||
class DatabaseService:
|
||||
"""Manages database connections and sessions."""
|
||||
@@ -62,13 +66,24 @@ class DatabaseService:
|
||||
logger.info("Database connection closed")
|
||||
|
||||
async def create_tables(self) -> None:
|
||||
"""Create all tables (for development/testing)."""
|
||||
"""Create all tables from schema.sql."""
|
||||
if not self._engine:
|
||||
raise RuntimeError("Database not initialized")
|
||||
|
||||
if not SCHEMA_PATH.exists():
|
||||
logger.warning(f"schema.sql not found at {SCHEMA_PATH}, skipping table creation")
|
||||
return
|
||||
|
||||
schema_sql = SCHEMA_PATH.read_text()
|
||||
|
||||
async with self._engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
logger.info("Database tables created")
|
||||
# Execute each statement separately (PostgreSQL doesn't like multiple in one)
|
||||
for statement in schema_sql.split(";"):
|
||||
statement = statement.strip()
|
||||
if statement and not statement.startswith("--"):
|
||||
await conn.execute(text(statement))
|
||||
|
||||
logger.info("Database tables created from schema.sql")
|
||||
|
||||
@asynccontextmanager
|
||||
async def session(self) -> AsyncGenerator[AsyncSession, None]:
|
||||
|
||||
@@ -52,7 +52,7 @@ class PersistentConversationManager:
|
||||
stmt = stmt.order_by(Conversation.last_message_at.desc())
|
||||
|
||||
result = await self._session.execute(stmt)
|
||||
conversation = result.scalar_first()
|
||||
conversation = result.scalars().first()
|
||||
|
||||
if conversation:
|
||||
logger.debug(
|
||||
|
||||
Reference in New Issue
Block a user