zoo_meta: ajoute une méthode utilitaire pour créer des index trigram (#19092)

This commit is contained in:
Benjamin Dauvergne 2017-12-27 16:55:28 +01:00 committed by Thomas NOEL
parent 0884695546
commit efaafa575d
1 changed files with 19 additions and 0 deletions

View File

@ -87,6 +87,25 @@ class CommonSchema(models.Model):
'WHERE schema_id = %s' % (key, self.id, table, expr, self.id))
cursor.execute(sql)
def create_trigram_index(self, expr):
from zoo.zoo_data.models import Entity, Relation
if isinstance(self, EntitySchema):
table = Entity._meta.db_table
elif isinstance(self, RelationSchema):
table = Relation._meta.db_table
else:
raise NotImplementedError(self)
key = md5(expr).hexdigest()[:8]
gin_sql = ('CREATE INDEX zoo_entity_%s_gin_%s_dynamic_idx ON %s USING gin ((%s) '
'gin_trgm_ops) WHERE schema_id = %s' % (key, self.id, table, expr, self.id))
gist_sql = ('CREATE INDEX zoo_entity_%s_gist_%s_dynamic_idx ON %s USING gist ((%s)'
' gist_trgm_ops) WHERE schema_id = %s' % (key, self.id, table, expr, self.id))
with connection.cursor() as cursor:
cursor.execute(gin_sql)
cursor.execute(gist_sql)
def rebuild_indexes(self):
from zoo.zoo_data.models import Entity