Fixes #22662: Fix database overflow when saving Cables with large lengths (#22668)

This commit is contained in:
Martin Hauser 2026-07-14 18:25:56 +02:00 committed by GitHub
parent ebee3578b9
commit bd562dd5c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 1 deletions

View File

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('dcim', '0237_module_remove_local_context_data'),
]
operations = [
migrations.AlterField(
model_name='cable',
name='_abs_length',
field=models.DecimalField(blank=True, decimal_places=4, max_digits=14, null=True),
),
]

View File

@ -133,7 +133,7 @@ class Cable(PrimaryModel):
)
# Stores the normalized length (in meters) for database ordering
_abs_length = models.DecimalField(
max_digits=10,
max_digits=14,
decimal_places=4,
blank=True,
null=True

View File

@ -1,3 +1,5 @@
from decimal import Decimal
from django.core.exceptions import ValidationError
from django.db.models.signals import post_save
from django.test import TestCase, tag
@ -2227,6 +2229,32 @@ class CableTestCase(TestCase):
interface = Interface(device=device, name='tmp', cable=cable)
self.assertIsNone(interface.path)
def test_cable_length_normalization_large_kilometer_value(self):
"""
A large kilometer length must pass validation and fit in the normalized length field.
"""
cable = Cable.objects.first()
cable.length = Decimal('1234')
cable.length_unit = CableLengthUnitChoices.UNIT_KILOMETER
cable.full_clean()
cable.save()
cable.refresh_from_db()
self.assertEqual(cable._abs_length, Decimal('1234000.0000'))
def test_cable_length_normalization_maximum_mile_value(self):
"""
The maximum length value expressed in miles must fit in the normalized length field.
"""
cable = Cable.objects.first()
cable.length = Decimal('999999.99')
cable.length_unit = CableLengthUnitChoices.UNIT_MILE
cable.full_clean()
cable.save()
cable.refresh_from_db()
self.assertEqual(cable._abs_length, Decimal('1609343983.9066'))
class CableTerminationTestCase(TestCase):