mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 01:06:11 -06:00
Fixes #11046: Restrict length of indexed search values
This commit is contained in:
parent
d0e0c2ff8b
commit
2d49fc2521
@ -2,6 +2,8 @@ import sys
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
|
import django.db.models.functions.text
|
||||||
|
import django.db.models.lookups
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
@ -39,12 +41,16 @@ class Migration(migrations.Migration):
|
|||||||
('object_id', models.PositiveBigIntegerField()),
|
('object_id', models.PositiveBigIntegerField()),
|
||||||
('field', models.CharField(max_length=200)),
|
('field', models.CharField(max_length=200)),
|
||||||
('type', models.CharField(max_length=30)),
|
('type', models.CharField(max_length=30)),
|
||||||
('value', models.TextField(db_index=True)),
|
('value', models.TextField()),
|
||||||
('weight', models.PositiveSmallIntegerField(default=1000)),
|
('weight', models.PositiveSmallIntegerField(default=1000)),
|
||||||
('object_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')),
|
('object_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'ordering': ('weight', 'object_type', 'object_id'),
|
'ordering': ('weight', 'object_type', 'object_id'),
|
||||||
|
'indexes': (
|
||||||
|
models.Index(condition=models.Q(django.db.models.lookups.LessThan(django.db.models.functions.text.Length('value'), 1024)), fields=['value'], name='extras_cachedvalue_value'),
|
||||||
|
models.Index(condition=models.Q(django.db.models.lookups.LessThan(django.db.models.functions.text.Length('value'), 1024)), fields=['value'], name='extras_cachedvalue_value_like', opclasses=['text_pattern_ops']),
|
||||||
|
)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
migrations.RunPython(
|
migrations.RunPython(
|
||||||
|
@ -2,6 +2,9 @@ import uuid
|
|||||||
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Index, Q
|
||||||
|
from django.db.models.functions import Length
|
||||||
|
from django.db.models.lookups import LessThan
|
||||||
|
|
||||||
from utilities.fields import RestrictedGenericForeignKey
|
from utilities.fields import RestrictedGenericForeignKey
|
||||||
|
|
||||||
@ -9,6 +12,9 @@ __all__ = (
|
|||||||
'CachedValue',
|
'CachedValue',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Maximum cached value length to index (see #11046)
|
||||||
|
INDEX_MAX = 1024
|
||||||
|
|
||||||
|
|
||||||
class CachedValue(models.Model):
|
class CachedValue(models.Model):
|
||||||
id = models.UUIDField(
|
id = models.UUIDField(
|
||||||
@ -36,15 +42,26 @@ class CachedValue(models.Model):
|
|||||||
type = models.CharField(
|
type = models.CharField(
|
||||||
max_length=30
|
max_length=30
|
||||||
)
|
)
|
||||||
value = models.TextField(
|
value = models.TextField()
|
||||||
db_index=True
|
|
||||||
)
|
|
||||||
weight = models.PositiveSmallIntegerField(
|
weight = models.PositiveSmallIntegerField(
|
||||||
default=1000
|
default=1000
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ('weight', 'object_type', 'object_id')
|
ordering = ('weight', 'object_type', 'object_id')
|
||||||
|
indexes = (
|
||||||
|
Index(
|
||||||
|
fields=['value'],
|
||||||
|
name='extras_cachedvalue_value',
|
||||||
|
condition=Q(LessThan(Length('value'), 1024))
|
||||||
|
),
|
||||||
|
Index(
|
||||||
|
fields=['value'],
|
||||||
|
name='extras_cachedvalue_value_like',
|
||||||
|
opclasses=['text_pattern_ops'],
|
||||||
|
condition=Q(LessThan(Length('value'), 1024))
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.object_type} {self.object_id}: {self.field}={self.value}'
|
return f'{self.object_type} {self.object_id}: {self.field}={self.value}'
|
||||||
|
Loading…
Reference in New Issue
Block a user