From aa673c43dfdfb8605ec222ac38c5ef2cf1e6350f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Far=C3=ADas=20Santana?= Date: Wed, 28 Aug 2024 13:38:32 +0200 Subject: [PATCH] fix: Move update to inside transaction block --- posthog/warehouse/api/saved_query.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/posthog/warehouse/api/saved_query.py b/posthog/warehouse/api/saved_query.py index 41454f3ae530d..db437861f967a 100644 --- a/posthog/warehouse/api/saved_query.py +++ b/posthog/warehouse/api/saved_query.py @@ -80,19 +80,22 @@ def create(self, validated_data): return view def update(self, instance: Any, validated_data: Any) -> Any: - view: DataWarehouseSavedQuery = super().update(instance, validated_data) + with transaction.atomic(): + view: DataWarehouseSavedQuery = super().update(instance, validated_data) - try: - view.columns = view.get_columns() - view.external_tables = view.s3_tables - except Exception as err: - raise serializers.ValidationError(str(err)) + try: + view.columns = view.get_columns() + view.external_tables = view.s3_tables + except RecursionError: + raise serializers.ValidationError("Model contains a cycle") + + except Exception as err: + raise serializers.ValidationError(str(err)) - with transaction.atomic(): view.save() try: - DataWarehouseModelPath.objects.create_from_saved_query(view) + DataWarehouseModelPath.objects.update_from_saved_query(view) except Exception: logger.exception("Failed to update model path when updating view %s", view.name)