From e8e5b6e85a7bb6a58b25865b8b0632238c774a8f Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Tue, 28 Feb 2012 19:14:47 +0530 Subject: [PATCH] Add mgmt command to create trigram indexes. --- chaloBEST/mumbai/management/__init__.py | 0 .../mumbai/management/commands/__init__.py | 0 .../mumbai/management/commands/trgmidx.py | 18 ++++++++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 chaloBEST/mumbai/management/__init__.py create mode 100644 chaloBEST/mumbai/management/commands/__init__.py create mode 100644 chaloBEST/mumbai/management/commands/trgmidx.py diff --git a/chaloBEST/mumbai/management/__init__.py b/chaloBEST/mumbai/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chaloBEST/mumbai/management/commands/__init__.py b/chaloBEST/mumbai/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chaloBEST/mumbai/management/commands/trgmidx.py b/chaloBEST/mumbai/management/commands/trgmidx.py new file mode 100644 index 0000000..000e0d7 --- /dev/null +++ b/chaloBEST/mumbai/management/commands/trgmidx.py @@ -0,0 +1,18 @@ +from django.core.management.base import BaseCommand, CommandError +from django.db import connection +from mumbai import models + +class Command(BaseCommand): + help = "Instantiates the pg_trgm indexes" + + def handle(self, *args, **options): + cursor = connection.cursor() + for name, model in models: + if not hasattr(model, "objects") or \ + not isinstance(model.objects, model.TrigramSearchManager): + continue + table = model._meta.db_table + for column in model.objects.trigram_columns: + cursor.execute(""" + CREATE INDEX %s_%s_trgm_idx ON %s USING gin (%s gin_trgm_ops);""" % ( + table, column, table, column))