Commit ·
5d1296c
1
Parent(s): f986d90
fix: Use native UUID for PostgreSQL compatibility
Browse files- src/core/database.py +4 -41
src/core/database.py
CHANGED
|
@@ -9,10 +9,6 @@ from sqlalchemy.dialects.postgresql import UUID
|
|
| 9 |
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
| 10 |
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
| 11 |
from sqlalchemy.orm import relationship
|
| 12 |
-
from sqlalchemy.types import TypeDecorator, CHAR
|
| 13 |
-
from sqlalchemy import String as SQLString
|
| 14 |
-
|
| 15 |
-
import os
|
| 16 |
|
| 17 |
# Use the DATABASE_URL from environment variables, with a fallback to SQLite for local development
|
| 18 |
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./knowledge_assistant.db")
|
|
@@ -20,45 +16,12 @@ DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./knowledge_assist
|
|
| 20 |
Base: DeclarativeMeta = declarative_base()
|
| 21 |
|
| 22 |
|
| 23 |
-
class GUID(TypeDecorator):
|
| 24 |
-
"""Platform-independent GUID type.
|
| 25 |
-
Uses PostgreSQL's UUID type, otherwise uses CHAR(36), storing as stringified hex values.
|
| 26 |
-
"""
|
| 27 |
-
impl = CHAR
|
| 28 |
-
cache_ok = True
|
| 29 |
-
|
| 30 |
-
def load_dialect_impl(self, dialect):
|
| 31 |
-
if dialect.name == 'postgresql':
|
| 32 |
-
return dialect.type_descriptor(UUID(as_uuid=True))
|
| 33 |
-
else:
|
| 34 |
-
return dialect.type_descriptor(CHAR(36))
|
| 35 |
-
|
| 36 |
-
def process_bind_param(self, value, dialect):
|
| 37 |
-
if value is None:
|
| 38 |
-
return value
|
| 39 |
-
elif dialect.name == 'postgresql':
|
| 40 |
-
return str(value)
|
| 41 |
-
else:
|
| 42 |
-
if not isinstance(value, uuid.UUID):
|
| 43 |
-
return str(uuid.UUID(value))
|
| 44 |
-
else:
|
| 45 |
-
return str(value)
|
| 46 |
-
|
| 47 |
-
def process_result_value(self, value, dialect):
|
| 48 |
-
if value is None:
|
| 49 |
-
return value
|
| 50 |
-
else:
|
| 51 |
-
if not isinstance(value, uuid.UUID):
|
| 52 |
-
return uuid.UUID(value)
|
| 53 |
-
return value
|
| 54 |
-
|
| 55 |
-
|
| 56 |
class User(SQLAlchemyBaseUserTableUUID, Base):
|
| 57 |
"""User model extending FastAPI-Users base table"""
|
| 58 |
__tablename__ = "users"
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
id = Column(
|
| 62 |
|
| 63 |
# Additional fields beyond the base user table
|
| 64 |
created_at = Column(DateTime, default=datetime.utcnow)
|
|
@@ -69,8 +32,8 @@ class DocumentMetadata(Base):
|
|
| 69 |
"""Document metadata model for tracking user uploads"""
|
| 70 |
__tablename__ = "documents"
|
| 71 |
|
| 72 |
-
id = Column(
|
| 73 |
-
user_id = Column(
|
| 74 |
filename = Column(String(255), nullable=False)
|
| 75 |
original_size = Column(Integer)
|
| 76 |
chunks_count = Column(Integer)
|
|
|
|
| 9 |
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
| 10 |
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
| 11 |
from sqlalchemy.orm import relationship
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Use the DATABASE_URL from environment variables, with a fallback to SQLite for local development
|
| 14 |
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./knowledge_assistant.db")
|
|
|
|
| 16 |
Base: DeclarativeMeta = declarative_base()
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
class User(SQLAlchemyBaseUserTableUUID, Base):
|
| 20 |
"""User model extending FastAPI-Users base table"""
|
| 21 |
__tablename__ = "users"
|
| 22 |
|
| 23 |
+
# Use the standard UUID type for PostgreSQL
|
| 24 |
+
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
| 25 |
|
| 26 |
# Additional fields beyond the base user table
|
| 27 |
created_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
| 32 |
"""Document metadata model for tracking user uploads"""
|
| 33 |
__tablename__ = "documents"
|
| 34 |
|
| 35 |
+
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
| 36 |
+
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
| 37 |
filename = Column(String(255), nullable=False)
|
| 38 |
original_size = Column(Integer)
|
| 39 |
chunks_count = Column(Integer)
|