Compare commits

...

13 Commits

Author SHA1 Message Date
-LAN-
4919e138e2 Fix migration base revision 2025-12-20 02:37:17 +08:00
-LAN-
eadf137b75 Delete soft-deleted conversations before dropping is_deleted column
Use sa.table() with sa.delete() and sa.true() to generate cross-database
compatible SQL that works in both online and offline modes without
requiring schema reflection.
2025-12-20 02:37:17 +08:00
-LAN-
4ad79538e2 Simplify is_deleted migration: drop column without backup 2025-12-20 02:37:17 +08:00
-LAN-
5efe443d85 Drop offline guards in is_deleted migration 2025-12-20 02:37:17 +08:00
-LAN-
53c6c27a98 Use SQLAlchemy expressions for is_deleted migration backup 2025-12-20 02:37:17 +08:00
-LAN-
26c2ad3d2b Handle offline migrations and MySQL/PostgreSQL booleans 2025-12-20 02:37:17 +08:00
-LAN-
b31b0446dd Make conversation is_deleted migration cross-database safe 2025-12-20 02:37:16 +08:00
-LAN-
ab142a302e Fix migration down_revision after merge 2025-12-20 02:37:16 +08:00
-LAN-
62f2288460 refactor: use SQLAlchemy inspector for database-agnostic table existence check
Replace PostgreSQL-specific information_schema query with SQLAlchemy's
inspector API that works across all supported database types.
2025-12-20 02:37:16 +08:00
-LAN-
4e8e53c579 refactor: extract backup table name as module constant
Share backup_table_name between upgrade() and downgrade() functions
to avoid duplication and ensure consistency.
2025-12-20 02:37:16 +08:00
-LAN-
d3490ebb0f refactor: backup soft-deleted conversations instead of deleting them
- Create backup table conversations_4f02b6704509_bak for soft-deleted records
- Only create backup table if soft-deleted records exist
- Support recovery during downgrade migration
- Drop backup table after successful restoration
2025-12-20 02:37:16 +08:00
-LAN-
b8885fa029 fix: delete soft-deleted conversations before dropping is_deleted column
Ensure any existing soft-deleted conversations are removed from the
database before dropping the is_deleted column in the migration.
2025-12-20 02:37:16 +08:00
-LAN-
8e666b4704 refactor: remove unused is_deleted field from conversations table
Remove the is_deleted field that was never utilized for soft deletion.
This simplifies queries and reduces unnecessary database overhead.

Fixes #25017
2025-12-20 02:37:14 +08:00
5 changed files with 31 additions and 9 deletions

View File

@@ -338,9 +338,7 @@ class CompletionConversationApi(Resource):
current_user, _ = current_account_with_tenant()
args = CompletionConversationQuery.model_validate(request.args.to_dict(flat=True)) # type: ignore
query = sa.select(Conversation).where(
Conversation.app_id == app_model.id, Conversation.mode == "completion", Conversation.is_deleted.is_(False)
)
query = sa.select(Conversation).where(Conversation.app_id == app_model.id, Conversation.mode == "completion")
if args.keyword:
query = query.join(Message, Message.conversation_id == Conversation.id).where(
@@ -452,7 +450,7 @@ class ChatConversationApi(Resource):
.subquery()
)
query = sa.select(Conversation).where(Conversation.app_id == app_model.id, Conversation.is_deleted.is_(False))
query = sa.select(Conversation).where(Conversation.app_id == app_model.id)
if args.keyword:
keyword_filter = f"%{args.keyword}%"

View File

@@ -0,0 +1,29 @@
"""remove unused is_deleted from conversations
Revision ID: e5d7a95e676f
Revises: d57accd375ae
Create Date: 2025-11-27 18:27:09.006691
"""
import sqlalchemy as sa
from alembic import op
revision = "e5d7a95e676f"
down_revision = "d57accd375ae"
branch_labels = None
depends_on = None
def upgrade():
conversations = sa.table("conversations", sa.column("is_deleted", sa.Boolean))
op.execute(sa.delete(conversations).where(conversations.c.is_deleted == sa.true()))
with op.batch_alter_table("conversations", schema=None) as batch_op:
batch_op.drop_column("is_deleted")
def downgrade():
with op.batch_alter_table("conversations", schema=None) as batch_op:
batch_op.add_column(
sa.Column("is_deleted", sa.BOOLEAN(), server_default=sa.text("false"), autoincrement=False, nullable=False)
)

View File

@@ -676,8 +676,6 @@ class Conversation(Base):
"MessageAnnotation", backref="conversation", lazy="select", passive_deletes="all"
)
is_deleted: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"))
@property
def inputs(self) -> dict[str, Any]:
inputs = self._inputs.copy()

View File

@@ -47,7 +47,6 @@ class ConversationService:
return InfiniteScrollPagination(data=[], limit=limit, has_more=False)
stmt = select(Conversation).where(
Conversation.is_deleted == False,
Conversation.app_id == app_model.id,
Conversation.from_source == ("api" if isinstance(user, EndUser) else "console"),
Conversation.from_end_user_id == (user.id if isinstance(user, EndUser) else None),
@@ -166,7 +165,6 @@ class ConversationService:
Conversation.from_source == ("api" if isinstance(user, EndUser) else "console"),
Conversation.from_end_user_id == (user.id if isinstance(user, EndUser) else None),
Conversation.from_account_id == (user.id if isinstance(user, Account) else None),
Conversation.is_deleted == False,
)
.first()
)

View File

@@ -149,7 +149,6 @@ class TestWebConversationService:
from_end_user_id=user.id if isinstance(user, EndUser) else None,
from_account_id=user.id if isinstance(user, Account) else None,
dialogue_count=0,
is_deleted=False,
)
from extensions.ext_database import db