Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""change erole 'bot' -> 'model'

Revision ID: 0002_change_erole_bot_to_model
Revises: 0001_create_initial_tables
Create Date: 2025-11-05 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '0002_change_erole_bot_to_model'
down_revision = '0001_create_initial_tables'
branch_labels = None
depends_on = None


def upgrade() -> None:
bind = op.get_bind()
dialect = bind.dialect.name

# First, update existing data from 'bot' to 'model'
try:
op.execute("""UPDATE messages SET role='model' WHERE role='bot'""")
except Exception:
# best-effort: continue if table doesn't exist or update fails
pass

# Then alter the column enum definition depending on dialect
if dialect in ("mysql", "mysql+pymysql", "mysql+aiomysql"):
# MySQL: modify column to new ENUM
try:
op.execute("""
ALTER TABLE messages
MODIFY COLUMN role ENUM('user','model') NOT NULL
""")
except Exception:
pass
else:
# For other DBs, attempt a generic ALTER using SQLAlchemy Enum
try:
op.alter_column('messages', 'role',
existing_type=sa.Enum('user', 'bot', name='erole'),
type_=sa.Enum('user', 'model', name='erole'),
existing_nullable=False)
except Exception:
pass


def downgrade() -> None:
bind = op.get_bind()
dialect = bind.dialect.name

# revert values from 'model' back to 'bot'
try:
op.execute("""UPDATE messages SET role='bot' WHERE role='model'""")
except Exception:
pass

if dialect in ("mysql", "mysql+pymysql", "mysql+aiomysql"):
try:
op.execute("""
ALTER TABLE messages
MODIFY COLUMN role ENUM('user','bot') NOT NULL
""")
except Exception:
pass
else:
try:
op.alter_column('messages', 'role',
existing_type=sa.Enum('user', 'model', name='erole'),
type_=sa.Enum('user', 'bot', name='erole'),
existing_nullable=False)
except Exception:
pass
21 changes: 0 additions & 21 deletions backend/fastapi/alembic/versions/18af15ae8ac6_.py

This file was deleted.

21 changes: 0 additions & 21 deletions backend/fastapi/alembic/versions/52a8a35fd055_.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def _persist_assistant_message(self, conversation_id: Optional[str], text:
msg = MessageDomain(
id=generate_unique_id("msg"),
conversation_id=conversation_id,
role="bot",
role="model",
content=text,
created_at=get_current_timestamp(),
)
Expand Down
2 changes: 1 addition & 1 deletion backend/fastapi/src/domain/enums/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def from_str(cls, value: Any) -> "ESortOrder":

class ERole(str, Enum):
USER = "user"
BOT = "bot"
MODEL = "model"

@classmethod
def from_str(cls, value: Any) -> "ERole":
Expand Down
2 changes: 1 addition & 1 deletion db/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS conversations (
CREATE TABLE IF NOT EXISTS messages (
id CHAR(255) NOT NULL PRIMARY KEY,
conversation_id CHAR(255) NOT NULL,
role ENUM('user','bot') NOT NULL,
role ENUM('user','model') NOT NULL,
content LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, -- Nội dung lớn, hỗ trợ emoji, HTML, markdown
created_at INT DEFAULT CURRENT_TIMESTAMP,
INDEX idx_conversation_id (conversation_id),
Expand Down
Loading