Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>tags/1.8.1
| - name: ast-grep | - name: ast-grep | ||||
| run: | | run: | | ||||
| uvx --from ast-grep-cli sg --pattern 'db.session.query($WHATEVER).filter($HERE)' --rewrite 'db.session.query($WHATEVER).where($HERE)' -l py --update-all | uvx --from ast-grep-cli sg --pattern 'db.session.query($WHATEVER).filter($HERE)' --rewrite 'db.session.query($WHATEVER).where($HERE)' -l py --update-all | ||||
| uvx --from ast-grep-cli sg --pattern 'session.query($WHATEVER).filter($HERE)' --rewrite 'session.query($WHATEVER).where($HERE)' -l py --update-all | |||||
| - name: mdformat | - name: mdformat | ||||
| run: | | run: | | ||||
| uvx mdformat . | uvx mdformat . |
| message_id = str(args["message_id"]) | message_id = str(args["message_id"]) | ||||
| message = db.session.query(Message).filter(Message.id == message_id, Message.app_id == app_model.id).first() | |||||
| message = db.session.query(Message).where(Message.id == message_id, Message.app_id == app_model.id).first() | |||||
| if not message: | if not message: | ||||
| raise NotFound("Message Not Exists.") | raise NotFound("Message Not Exists.") |
| strategies = ( | strategies = ( | ||||
| db.session.query(TenantPluginAutoUpgradeStrategy) | db.session.query(TenantPluginAutoUpgradeStrategy) | ||||
| .filter( | |||||
| .where( | |||||
| TenantPluginAutoUpgradeStrategy.upgrade_time_of_day >= now_seconds_of_day, | TenantPluginAutoUpgradeStrategy.upgrade_time_of_day >= now_seconds_of_day, | ||||
| TenantPluginAutoUpgradeStrategy.upgrade_time_of_day | TenantPluginAutoUpgradeStrategy.upgrade_time_of_day | ||||
| < now_seconds_of_day + AUTO_UPGRADE_MINIMAL_CHECKING_INTERVAL, | < now_seconds_of_day + AUTO_UPGRADE_MINIMAL_CHECKING_INTERVAL, |
| with db.session.begin_nested(): | with db.session.begin_nested(): | ||||
| message_data = ( | message_data = ( | ||||
| db.session.query(Message.id, Message.conversation_id) | db.session.query(Message.id, Message.conversation_id) | ||||
| .filter(Message.workflow_run_id.in_(workflow_run_ids)) | |||||
| .where(Message.workflow_run_id.in_(workflow_run_ids)) | |||||
| .all() | .all() | ||||
| ) | ) | ||||
| message_id_list = [msg.id for msg in message_data] | message_id_list = [msg.id for msg in message_data] |
| annotations_to_delete = ( | annotations_to_delete = ( | ||||
| db.session.query(MessageAnnotation, AppAnnotationSetting) | db.session.query(MessageAnnotation, AppAnnotationSetting) | ||||
| .outerjoin(AppAnnotationSetting, MessageAnnotation.app_id == AppAnnotationSetting.app_id) | .outerjoin(AppAnnotationSetting, MessageAnnotation.app_id == AppAnnotationSetting.app_id) | ||||
| .filter(MessageAnnotation.id.in_(annotation_ids)) | |||||
| .where(MessageAnnotation.id.in_(annotation_ids)) | |||||
| .all() | .all() | ||||
| ) | ) | ||||
| def clear_all_annotations(cls, app_id: str) -> dict: | def clear_all_annotations(cls, app_id: str) -> dict: | ||||
| app = ( | app = ( | ||||
| db.session.query(App) | db.session.query(App) | ||||
| .filter(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal") | |||||
| .where(App.id == app_id, App.tenant_id == current_user.current_tenant_id, App.status == "normal") | |||||
| .first() | .first() | ||||
| ) | ) | ||||
| # Query records related to expired messages | # Query records related to expired messages | ||||
| records = ( | records = ( | ||||
| session.query(model) | session.query(model) | ||||
| .filter( | |||||
| .where( | |||||
| model.message_id.in_(batch_message_ids), # type: ignore | model.message_id.in_(batch_message_ids), # type: ignore | ||||
| ) | ) | ||||
| .all() | .all() | ||||
| except Exception: | except Exception: | ||||
| logger.exception("Failed to save %s records", table_name) | logger.exception("Failed to save %s records", table_name) | ||||
| session.query(model).filter( | |||||
| session.query(model).where( | |||||
| model.id.in_(record_ids), # type: ignore | model.id.in_(record_ids), # type: ignore | ||||
| ).delete(synchronize_session=False) | ).delete(synchronize_session=False) | ||||
| with Session(db.engine).no_autoflush as session: | with Session(db.engine).no_autoflush as session: | ||||
| workflow_app_logs = ( | workflow_app_logs = ( | ||||
| session.query(WorkflowAppLog) | session.query(WorkflowAppLog) | ||||
| .filter( | |||||
| .where( | |||||
| WorkflowAppLog.tenant_id == tenant_id, | WorkflowAppLog.tenant_id == tenant_id, | ||||
| WorkflowAppLog.created_at < datetime.datetime.now() - datetime.timedelta(days=days), | WorkflowAppLog.created_at < datetime.datetime.now() - datetime.timedelta(days=days), | ||||
| ) | ) | ||||
| workflow_app_log_ids = [workflow_app_log.id for workflow_app_log in workflow_app_logs] | workflow_app_log_ids = [workflow_app_log.id for workflow_app_log in workflow_app_logs] | ||||
| # delete workflow app logs | # delete workflow app logs | ||||
| session.query(WorkflowAppLog).filter( | |||||
| WorkflowAppLog.id.in_(workflow_app_log_ids), | |||||
| ).delete(synchronize_session=False) | |||||
| session.query(WorkflowAppLog).where(WorkflowAppLog.id.in_(workflow_app_log_ids)).delete( | |||||
| synchronize_session=False | |||||
| ) | |||||
| session.commit() | session.commit() | ||||
| click.echo( | click.echo( |
| def delete_segments(cls, segment_ids: list, document: Document, dataset: Dataset): | def delete_segments(cls, segment_ids: list, document: Document, dataset: Dataset): | ||||
| segments = ( | segments = ( | ||||
| db.session.query(DocumentSegment.index_node_id, DocumentSegment.word_count) | db.session.query(DocumentSegment.index_node_id, DocumentSegment.word_count) | ||||
| .filter( | |||||
| .where( | |||||
| DocumentSegment.id.in_(segment_ids), | DocumentSegment.id.in_(segment_ids), | ||||
| DocumentSegment.dataset_id == dataset.id, | DocumentSegment.dataset_id == dataset.id, | ||||
| DocumentSegment.document_id == document.id, | DocumentSegment.document_id == document.id, |
| with Session(db.engine) as session: | with Session(db.engine) as session: | ||||
| return ( | return ( | ||||
| session.query(TenantPluginAutoUpgradeStrategy) | session.query(TenantPluginAutoUpgradeStrategy) | ||||
| .filter(TenantPluginAutoUpgradeStrategy.tenant_id == tenant_id) | |||||
| .where(TenantPluginAutoUpgradeStrategy.tenant_id == tenant_id) | |||||
| .first() | .first() | ||||
| ) | ) | ||||
| with Session(db.engine) as session: | with Session(db.engine) as session: | ||||
| exist_strategy = ( | exist_strategy = ( | ||||
| session.query(TenantPluginAutoUpgradeStrategy) | session.query(TenantPluginAutoUpgradeStrategy) | ||||
| .filter(TenantPluginAutoUpgradeStrategy.tenant_id == tenant_id) | |||||
| .where(TenantPluginAutoUpgradeStrategy.tenant_id == tenant_id) | |||||
| .first() | .first() | ||||
| ) | ) | ||||
| if not exist_strategy: | if not exist_strategy: | ||||
| with Session(db.engine) as session: | with Session(db.engine) as session: | ||||
| exist_strategy = ( | exist_strategy = ( | ||||
| session.query(TenantPluginAutoUpgradeStrategy) | session.query(TenantPluginAutoUpgradeStrategy) | ||||
| .filter(TenantPluginAutoUpgradeStrategy.tenant_id == tenant_id) | |||||
| .where(TenantPluginAutoUpgradeStrategy.tenant_id == tenant_id) | |||||
| .first() | .first() | ||||
| ) | ) | ||||
| if not exist_strategy: | if not exist_strategy: |
| history = ( | history = ( | ||||
| db.session.query(AppAnnotationHitHistory) | db.session.query(AppAnnotationHitHistory) | ||||
| .filter( | |||||
| .where( | |||||
| AppAnnotationHitHistory.annotation_id == annotation.id, AppAnnotationHitHistory.message_id == message_id | AppAnnotationHitHistory.annotation_id == annotation.id, AppAnnotationHitHistory.message_id == message_id | ||||
| ) | ) | ||||
| .first() | .first() |
| assert result.imported_dsl_version == "" | assert result.imported_dsl_version == "" | ||||
| # Verify no app was created in database | # Verify no app was created in database | ||||
| apps_count = db_session_with_containers.query(App).filter(App.tenant_id == account.current_tenant_id).count() | |||||
| apps_count = db_session_with_containers.query(App).where(App.tenant_id == account.current_tenant_id).count() | |||||
| assert apps_count == 1 # Only the original test app | assert apps_count == 1 # Only the original test app | ||||
| def test_import_app_missing_yaml_url(self, db_session_with_containers, mock_external_service_dependencies): | def test_import_app_missing_yaml_url(self, db_session_with_containers, mock_external_service_dependencies): | ||||
| assert result.imported_dsl_version == "" | assert result.imported_dsl_version == "" | ||||
| # Verify no app was created in database | # Verify no app was created in database | ||||
| apps_count = db_session_with_containers.query(App).filter(App.tenant_id == account.current_tenant_id).count() | |||||
| apps_count = db_session_with_containers.query(App).where(App.tenant_id == account.current_tenant_id).count() | |||||
| assert apps_count == 1 # Only the original test app | assert apps_count == 1 # Only the original test app | ||||
| def test_import_app_invalid_import_mode(self, db_session_with_containers, mock_external_service_dependencies): | def test_import_app_invalid_import_mode(self, db_session_with_containers, mock_external_service_dependencies): | ||||
| ) | ) | ||||
| # Verify no app was created in database | # Verify no app was created in database | ||||
| apps_count = db_session_with_containers.query(App).filter(App.tenant_id == account.current_tenant_id).count() | |||||
| apps_count = db_session_with_containers.query(App).where(App.tenant_id == account.current_tenant_id).count() | |||||
| assert apps_count == 1 # Only the original test app | assert apps_count == 1 # Only the original test app | ||||
| def test_export_dsl_chat_app_success(self, db_session_with_containers, mock_external_service_dependencies): | def test_export_dsl_chat_app_success(self, db_session_with_containers, mock_external_service_dependencies): |
| def test_clear_message_related_tables_no_records_found(self, mock_session, sample_message_ids): | def test_clear_message_related_tables_no_records_found(self, mock_session, sample_message_ids): | ||||
| """Test when no related records are found.""" | """Test when no related records are found.""" | ||||
| with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | ||||
| mock_session.query.return_value.filter.return_value.all.return_value = [] | |||||
| mock_session.query.return_value.where.return_value.all.return_value = [] | |||||
| ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ||||
| ): | ): | ||||
| """Test when records are found and have to_dict method.""" | """Test when records are found and have to_dict method.""" | ||||
| with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | ||||
| mock_session.query.return_value.filter.return_value.all.return_value = sample_records | |||||
| mock_session.query.return_value.where.return_value.all.return_value = sample_records | |||||
| ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ||||
| records.append(record) | records.append(record) | ||||
| # Mock records for first table only, empty for others | # Mock records for first table only, empty for others | ||||
| mock_session.query.return_value.filter.return_value.all.side_effect = [ | |||||
| mock_session.query.return_value.where.return_value.all.side_effect = [ | |||||
| records, | records, | ||||
| [], | [], | ||||
| [], | [], | ||||
| with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | ||||
| mock_storage.save.side_effect = Exception("Storage error") | mock_storage.save.side_effect = Exception("Storage error") | ||||
| mock_session.query.return_value.filter.return_value.all.return_value = sample_records | |||||
| mock_session.query.return_value.where.return_value.all.return_value = sample_records | |||||
| # Should not raise exception | # Should not raise exception | ||||
| ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ||||
| # Should still delete records even if backup fails | # Should still delete records even if backup fails | ||||
| assert mock_session.query.return_value.filter.return_value.delete.called | |||||
| assert mock_session.query.return_value.where.return_value.delete.called | |||||
| def test_clear_message_related_tables_serialization_error_continues(self, mock_session, sample_message_ids): | def test_clear_message_related_tables_serialization_error_continues(self, mock_session, sample_message_ids): | ||||
| """Test that method continues even when record serialization fails.""" | """Test that method continues even when record serialization fails.""" | ||||
| record.id = "record-1" | record.id = "record-1" | ||||
| record.to_dict.side_effect = Exception("Serialization error") | record.to_dict.side_effect = Exception("Serialization error") | ||||
| mock_session.query.return_value.filter.return_value.all.return_value = [record] | |||||
| mock_session.query.return_value.where.return_value.all.return_value = [record] | |||||
| # Should not raise exception | # Should not raise exception | ||||
| ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ||||
| # Should still delete records even if serialization fails | # Should still delete records even if serialization fails | ||||
| assert mock_session.query.return_value.filter.return_value.delete.called | |||||
| assert mock_session.query.return_value.where.return_value.delete.called | |||||
| def test_clear_message_related_tables_deletion_called(self, mock_session, sample_message_ids, sample_records): | def test_clear_message_related_tables_deletion_called(self, mock_session, sample_message_ids, sample_records): | ||||
| """Test that deletion is called for found records.""" | """Test that deletion is called for found records.""" | ||||
| with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | ||||
| mock_session.query.return_value.filter.return_value.all.return_value = sample_records | |||||
| mock_session.query.return_value.where.return_value.all.return_value = sample_records | |||||
| ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ||||
| # Should call delete for each table that has records | # Should call delete for each table that has records | ||||
| assert mock_session.query.return_value.filter.return_value.delete.called | |||||
| assert mock_session.query.return_value.where.return_value.delete.called | |||||
| def test_clear_message_related_tables_logging_output( | def test_clear_message_related_tables_logging_output( | ||||
| self, mock_session, sample_message_ids, sample_records, capsys | self, mock_session, sample_message_ids, sample_records, capsys | ||||
| ): | ): | ||||
| """Test that logging output is generated.""" | """Test that logging output is generated.""" | ||||
| with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | with patch("services.clear_free_plan_tenant_expired_logs.storage") as mock_storage: | ||||
| mock_session.query.return_value.filter.return_value.all.return_value = sample_records | |||||
| mock_session.query.return_value.where.return_value.all.return_value = sample_records | |||||
| ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ClearFreePlanTenantExpiredLogs._clear_message_related_tables(mock_session, "tenant-123", sample_message_ids) | ||||