create and update GNI indexes

This commit is contained in:
j 2013-01-31 07:54:39 +00:00
commit c748527921
2 changed files with 43 additions and 5 deletions

View file

@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from optparse import make_option
from django.core.management.base import BaseCommand
from django.db import connection
from django.conf import settings
import monkey_patch.models
from ... import models
import time
class Command(BaseCommand):
@ -14,8 +17,33 @@ class Command(BaseCommand):
"""
help = 'sql create statements for find tables to use trigram index'
args = ''
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options):
print 'CREATE INDEX item_itemfind_value_idx ON item_itemfind USING gin (value gin_trgm_ops);'
print ''
print 'CREATE INDEX clip_clip_findvalue_idx ON clip_clip USING gin (findvalue gin_trgm_ops);'
cursor = connection.cursor()
def create_table(index, table, key):
sql = 'CREATE INDEX "%s" ON "%s" USING gin ("%s" gin_trgm_ops)' % (index, table, key)
if options['debug']:
print sql
cursor.execute(sql)
if settings.DB_GIN_TRGM:
table_name = models.ItemFind._meta.db_table
indexes = connection.introspection.get_indexes(cursor, table_name)
name = 'value'
if name not in indexes:
create_table("%s_%s_idx"%(table_name, name), table_name, name)
table_name = models.Clip._meta.db_table
cursor = connection.cursor()
indexes = connection.introspection.get_indexes(cursor, table_name)
name = 'findvalue'
if name not in indexes:
create_table("%s_%s_idx"%(table_name, name), table_name, name)
for name in settings.CONFIG['clipLayers']:
name = '%s_findvalue' % name
if name not in indexes:
create_table("%s_%s_idx"%(table_name, name), table_name, name)