feat: Migrate to MySQL and add unified environment configuration
## Database Migration (SQLite → MySQL) - Add Alembic migration framework - Add 'tr_' prefix to all tables to avoid conflicts in shared database - Remove SQLite support, use MySQL exclusively - Add pymysql driver dependency - Change ad_token column to Text type for long JWT tokens ## Unified Environment Configuration - Centralize all hardcoded settings to environment variables - Backend: Extend Settings class in app/core/config.py - Frontend: Use Vite environment variables (import.meta.env) - Docker: Move credentials to environment variables - Update .env.example files with comprehensive documentation ## Test Organization - Move root-level test files to tests/ directory: - test_chat_room.py → tests/test_chat_room.py - test_websocket.py → tests/test_websocket.py - test_realtime_implementation.py → tests/test_realtime_implementation.py - Fix path references in test_realtime_implementation.py Breaking Changes: - CORS now requires explicit origins (no more wildcard) - All database tables renamed with 'tr_' prefix - SQLite no longer supported 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
"""SQLAlchemy models for realtime messaging
|
||||
|
||||
Tables:
|
||||
- messages: Stores all messages sent in incident rooms
|
||||
- message_reactions: User reactions to messages (emoji)
|
||||
- message_edit_history: Audit trail for message edits
|
||||
- tr_messages: Stores all messages sent in incident rooms
|
||||
- tr_message_reactions: User reactions to messages (emoji)
|
||||
- tr_message_edit_history: Audit trail for message edits
|
||||
|
||||
Note: All tables use 'tr_' prefix to avoid conflicts in shared database.
|
||||
"""
|
||||
from sqlalchemy import Column, Integer, String, Text, DateTime, Enum, ForeignKey, UniqueConstraint, Index, BigInteger, JSON
|
||||
from sqlalchemy.orm import relationship
|
||||
@@ -25,10 +27,10 @@ class MessageType(str, enum.Enum):
|
||||
class Message(Base):
|
||||
"""Message model for incident room communications"""
|
||||
|
||||
__tablename__ = "messages"
|
||||
__tablename__ = "tr_messages"
|
||||
|
||||
message_id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
room_id = Column(String(36), ForeignKey("incident_rooms.room_id", ondelete="CASCADE"), nullable=False)
|
||||
room_id = Column(String(36), ForeignKey("tr_incident_rooms.room_id", ondelete="CASCADE"), nullable=False)
|
||||
sender_id = Column(String(255), nullable=False) # User email/ID
|
||||
content = Column(Text, nullable=False)
|
||||
message_type = Column(Enum(MessageType), default=MessageType.TEXT, nullable=False)
|
||||
@@ -42,7 +44,6 @@ class Message(Base):
|
||||
deleted_at = Column(DateTime) # Soft delete timestamp
|
||||
|
||||
# Sequence number for FIFO ordering within a room
|
||||
# Note: Autoincrement doesn't work for non-PK in SQLite, will be set in service layer
|
||||
sequence_number = Column(BigInteger, nullable=False)
|
||||
|
||||
# Relationships
|
||||
@@ -51,22 +52,19 @@ class Message(Base):
|
||||
|
||||
# Indexes for common queries
|
||||
__table_args__ = (
|
||||
Index("ix_messages_room_created", "room_id", "created_at"),
|
||||
Index("ix_messages_room_sequence", "room_id", "sequence_number"),
|
||||
Index("ix_messages_sender", "sender_id"),
|
||||
# PostgreSQL full-text search index on content (commented for SQLite compatibility)
|
||||
# Note: Uncomment when using PostgreSQL with pg_trgm extension enabled
|
||||
# Index("ix_messages_content_search", "content", postgresql_using='gin', postgresql_ops={'content': 'gin_trgm_ops'}),
|
||||
Index("ix_tr_messages_room_created", "room_id", "created_at"),
|
||||
Index("ix_tr_messages_room_sequence", "room_id", "sequence_number"),
|
||||
Index("ix_tr_messages_sender", "sender_id"),
|
||||
)
|
||||
|
||||
|
||||
class MessageReaction(Base):
|
||||
"""Message reaction model for emoji reactions"""
|
||||
|
||||
__tablename__ = "message_reactions"
|
||||
__tablename__ = "tr_message_reactions"
|
||||
|
||||
reaction_id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
message_id = Column(String(36), ForeignKey("messages.message_id", ondelete="CASCADE"), nullable=False)
|
||||
message_id = Column(String(36), ForeignKey("tr_messages.message_id", ondelete="CASCADE"), nullable=False)
|
||||
user_id = Column(String(255), nullable=False) # User email/ID who reacted
|
||||
emoji = Column(String(10), nullable=False) # Emoji character or code
|
||||
|
||||
@@ -79,18 +77,18 @@ class MessageReaction(Base):
|
||||
# Constraints and indexes
|
||||
__table_args__ = (
|
||||
# Ensure unique reaction per user per message
|
||||
UniqueConstraint("message_id", "user_id", "emoji", name="uq_message_reaction"),
|
||||
Index("ix_message_reactions_message", "message_id"),
|
||||
UniqueConstraint("message_id", "user_id", "emoji", name="uq_tr_message_reaction"),
|
||||
Index("ix_tr_message_reactions_message", "message_id"),
|
||||
)
|
||||
|
||||
|
||||
class MessageEditHistory(Base):
|
||||
"""Message edit history model for audit trail"""
|
||||
|
||||
__tablename__ = "message_edit_history"
|
||||
__tablename__ = "tr_message_edit_history"
|
||||
|
||||
edit_id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
message_id = Column(String(36), ForeignKey("messages.message_id", ondelete="CASCADE"), nullable=False)
|
||||
message_id = Column(String(36), ForeignKey("tr_messages.message_id", ondelete="CASCADE"), nullable=False)
|
||||
original_content = Column(Text, nullable=False) # Content before edit
|
||||
edited_by = Column(String(255), nullable=False) # User who made the edit
|
||||
|
||||
@@ -102,5 +100,5 @@ class MessageEditHistory(Base):
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index("ix_message_edit_history_message", "message_id", "edited_at"),
|
||||
Index("ix_tr_message_edit_history_message", "message_id", "edited_at"),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user