Update ast-grep pattern for session.query (#24828)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
1
.github/workflows/autofix.yml
vendored
1
.github/workflows/autofix.yml
vendored
@@ -26,6 +26,7 @@ jobs:
|
|||||||
- 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 .
|
||||||
|
@@ -130,7 +130,7 @@ class MessageFeedbackApi(Resource):
|
|||||||
|
|
||||||
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.")
|
||||||
|
@@ -20,7 +20,7 @@ def check_upgradable_plugin_task():
|
|||||||
|
|
||||||
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,
|
||||||
|
@@ -93,7 +93,7 @@ def _delete_batch_with_retry(workflow_run_ids: list[str], attempt_count: int) ->
|
|||||||
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]
|
||||||
|
@@ -282,7 +282,7 @@ class AppAnnotationService:
|
|||||||
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()
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -493,7 +493,7 @@ class AppAnnotationService:
|
|||||||
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()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -62,7 +62,7 @@ class ClearFreePlanTenantExpiredLogs:
|
|||||||
# 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()
|
||||||
@@ -101,7 +101,7 @@ class ClearFreePlanTenantExpiredLogs:
|
|||||||
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)
|
||||||
|
|
||||||
@@ -295,7 +295,7 @@ class ClearFreePlanTenantExpiredLogs:
|
|||||||
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),
|
||||||
)
|
)
|
||||||
@@ -321,9 +321,9 @@ class ClearFreePlanTenantExpiredLogs:
|
|||||||
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(
|
session.query(WorkflowAppLog).where(WorkflowAppLog.id.in_(workflow_app_log_ids)).delete(
|
||||||
WorkflowAppLog.id.in_(workflow_app_log_ids),
|
synchronize_session=False
|
||||||
).delete(synchronize_session=False)
|
)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
click.echo(
|
click.echo(
|
||||||
|
@@ -2346,7 +2346,7 @@ class SegmentService:
|
|||||||
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,
|
||||||
|
@@ -10,7 +10,7 @@ class PluginAutoUpgradeService:
|
|||||||
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()
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ class PluginAutoUpgradeService:
|
|||||||
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:
|
||||||
@@ -54,7 +54,7 @@ class PluginAutoUpgradeService:
|
|||||||
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:
|
||||||
|
@@ -674,7 +674,7 @@ class TestAnnotationService:
|
|||||||
|
|
||||||
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()
|
||||||
|
@@ -166,7 +166,7 @@ class TestAppDslService:
|
|||||||
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):
|
||||||
@@ -191,7 +191,7 @@ class TestAppDslService:
|
|||||||
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):
|
||||||
@@ -215,7 +215,7 @@ class TestAppDslService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 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):
|
||||||
|
@@ -57,7 +57,7 @@ class TestClearFreePlanTenantExpiredLogs:
|
|||||||
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)
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ class TestClearFreePlanTenantExpiredLogs:
|
|||||||
):
|
):
|
||||||
"""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)
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ class TestClearFreePlanTenantExpiredLogs:
|
|||||||
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,
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
@@ -123,13 +123,13 @@ class TestClearFreePlanTenantExpiredLogs:
|
|||||||
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."""
|
||||||
@@ -138,30 +138,30 @@ class TestClearFreePlanTenantExpiredLogs:
|
|||||||
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)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user