Skip to content

Commit

Permalink
Closes #11519: Add a SQL index for IPAddress host value
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Aug 2, 2023
1 parent 0bb86f1 commit 8f27115
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions netbox/ipam/migrations/0067_ipaddress_index_host.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.1.10 on 2023-08-02 12:43

from django.db import migrations, models
import django.db.models.functions.comparison
import ipam.fields
import ipam.lookups


class Migration(migrations.Migration):

dependencies = [
('ipam', '0066_iprange_mark_utilized'),
]

operations = [
migrations.AddIndex(
model_name='ipaddress',
index=models.Index(django.db.models.functions.comparison.Cast(ipam.lookups.Host('address'), output_field=ipam.fields.IPAddressField()), name='ipam_ipaddress_host'),
),
]
5 changes: 5 additions & 0 deletions netbox/ipam/models/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import F
from django.db.models.functions import Cast
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

from ipam.choices import *
from ipam.constants import *
from ipam.fields import IPNetworkField, IPAddressField
from ipam.lookups import Host
from ipam.managers import IPAddressManager
from ipam.querysets import PrefixQuerySet
from ipam.validators import DNSValidator
Expand Down Expand Up @@ -778,6 +780,9 @@ class IPAddress(PrimaryModel):

class Meta:
ordering = ('address', 'pk') # address may be non-unique
indexes = [
models.Index(Cast(Host('address'), output_field=IPAddressField()), name='ipam_ipaddress_host'),
]
verbose_name = 'IP address'
verbose_name_plural = 'IP addresses'

Expand Down

0 comments on commit 8f27115

Please sign in to comment.