misc: add trigram index on unaccented role names (#87906)
gitea/authentic/pipeline/head This commit looks good Details

This commit is contained in:
Benjamin Dauvergne 2024-03-28 17:25:21 +01:00
parent 7e04fa9374
commit a73235b076
4 changed files with 51 additions and 1 deletions

View File

@ -0,0 +1,29 @@
# Generated by Django 3.2.23 on 2024-03-28 16:50
import django.contrib.postgres.indexes
import django.db.models.expressions
import django.db.models.functions.text
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('a2_rbac', '0039_set_user_view_permissions_by_ou'),
]
operations = [
migrations.AddIndex(
model_name='role',
index=django.contrib.postgres.indexes.GinIndex(
django.contrib.postgres.indexes.OpClass(
django.db.models.functions.text.Upper(
django.db.models.expressions.Func(
django.db.models.expressions.F('name'), function='immutable_unaccent'
)
),
'public.gin_trgm_ops',
),
name='name_idx',
),
),
]

View File

@ -23,10 +23,12 @@ from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres import indexes as postgresql_indexes
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models.functions import Upper
from django.db.models.query import Prefetch, Q
from django.urls import reverse
from django.utils.text import slugify
@ -614,6 +616,14 @@ class Role(AbstractBase):
condition=models.Q(service__isnull=True, ou__isnull=True, admin_scope_ct__isnull=True),
),
]
indexes = [
postgresql_indexes.GinIndex(
postgresql_indexes.OpClass(
Upper(models.Func(models.F('name'), function='immutable_unaccent')), 'public.gin_trgm_ops'
),
name='name_idx',
),
]
def natural_key(self):
return [

View File

@ -405,7 +405,7 @@ class ServiceRoleSearchForm(CssClass, PrefixFormMixin, FormWithRequest):
for word in (w.strip() for w in self.cleaned_data.get('text').split(' ')):
if not word:
continue
qs = qs.filter(name__unaccent__icontains=word)
qs = qs.filter(name__immutable_unaccent__icontains=word)
if not app_settings.SHOW_INTERNAL_ROLES and not self.cleaned_data.get('internals'):
qs = qs.exclude(slug__startswith='_')
return qs

View File

@ -1,4 +1,5 @@
from django.contrib.postgres.lookups import Unaccent as PGUnaccent
from django.db.models import CharField, TextField, Transform
from django.db.models.functions import Concat
from django.db.models.functions import ConcatPair as DjConcatPair
@ -7,6 +8,16 @@ class Unaccent(PGUnaccent):
function = 'immutable_unaccent'
class UnaccentTransform(Transform):
bilateral = True
lookup_name = 'immutable_unaccent'
function = 'immutable_unaccent'
CharField.register_lookup(UnaccentTransform)
TextField.register_lookup(UnaccentTransform)
class ConcatPair(DjConcatPair):
"""Django ConcatPair does not implement as_postgresql, using CONCAT as a default.