Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock Receipt during validation and other concurrency improvements #217

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

npazosmendez
Copy link
Contributor

No description provided.

@npazosmendez npazosmendez changed the title Lock during validation and other concurrency improvements Lock Receipt during validation and other concurrency improvements Jun 28, 2024
Copy link
Owner

@WhyNotHugo WhyNotHugo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se ve bien, sólo tengo feedback sobre detalles menores.

)
if cae_data.Observaciones:
with transaction.atomic():
qs = Receipt.objects.filter(id__in=ids, receipt_number__isnull=False).select_for_update()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podés reusar el QuerySet existente acá:

Suggested change
qs = Receipt.objects.filter(id__in=ids, receipt_number__isnull=False).select_for_update()
qs = qs.select_for_update()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eso intenté en un principio, pero me da esta excepción:

self = <django.db.backends.utils.CursorWrapper object at 0x7f219c6762a0>
sql = 'SELECT "afip_receipt"."id", "afip_receipt"."point_of_sales_id", "afip_receipt"."receipt_type_id", "afip_receipt"."con..."."id" = %s AND "afip_receiptvalidation"."id" IS NULL) ORDER BY "afip_receipt"."receipt_number" ASC LIMIT 1 FOR UPDATE'
params = (12,), ignored_wrapper_args = (False, {'connection': <DatabaseWrapper vendor='postgresql' alias='default'>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7f219c6762a0>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        # Raise a warning during app initialization (stored_app_configs is only
        # ever set during testing).
        if not apps.ready and not apps.stored_app_configs:
            warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                # params default might be backend specific.
                return self.cursor.execute(sql)
            else:
>               return self.cursor.execute(sql, params)
E               django.db.utils.NotSupportedError: FOR UPDATE cannot be applied to the nullable side of an outer join

.tox/live/lib/python3.12/site-packages/django/db/backends/utils.py:105: NotSupportedError

Me parece que el problema es el qs = self.filter(validation__isnull=True) de un par de lineas más arriba. Por lo que leí, Postgres dice que no puede lockear las filas según un criterio como ese, porque en cualquier momento podría suceder que validation__isnull=True si se agregara un Validation que apunte al Receipt; tendría que lockear la tabla Validations entera supongo, o algo similar.

Decime si estoy tirando fruta, no sé mucho PG pero menos aun Django


# Remove the number from ones that failed to validate:
qs.filter(validation__isnull=True).update(receipt_number=None)
Receipt.objects.filter(id__in=ids, validation__isnull=True).update(receipt_number=None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants