bug: Fix decimal.InvalidOperation on /system for some PG versions

In https://github.com/TandoorRecipes/recipes/pull/2730 the /system page was improved to warn the user if the version of Postgres they are using is out of date and should be updated. The current code attempts to determine the major versions by replacing `00` with `.` and then converting to a `Decimal`. Unfortunately, it appears the only value this method _does not_ work for are initial releases of major versions, like `16.0.0`.

For reference, either Postgres or the PsyCog driver represents the semver values but without the dots, so `16.0.0` becomes `1600000`.

This change removes the string replace and Decimal conversion in favor of using the divmod() function. In this application it will return a tuple with the first element being the major version of Postgres. This is then used as before to compare against deprecated versions.
This commit is contained in:
Rich Schumacher
2024-03-07 08:52:39 -05:00
parent 8e9285a24e
commit 3489216daf

View File

@@ -293,11 +293,10 @@ def system(request):
if postgres:
postgres_current = 16 # will need to be updated as PostgreSQL releases new major versions
from decimal import Decimal
from django.db import connection
postgres_ver = Decimal(str(connection.pg_version).replace('00', '.'))
postgres_ver = divmod(connection.pg_version, 10000)
if postgres_ver >= postgres_current:
database_status = 'success'
database_message = _('Everything is fine!')