From c74852792132bf1befde3de3b89d68cedddaa120 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 31 Jan 2013 07:54:39 +0000 Subject: [PATCH] create and update GNI indexes --- .../item/management/commands/sqlfindindex.py | 34 +++++++++++++++++-- .../item/management/commands/sync_itemsort.py | 14 ++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/pandora/item/management/commands/sqlfindindex.py b/pandora/item/management/commands/sqlfindindex.py index 4b30c61d..c6d99af3 100644 --- a/pandora/item/management/commands/sqlfindindex.py +++ b/pandora/item/management/commands/sqlfindindex.py @@ -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) + diff --git a/pandora/item/management/commands/sync_itemsort.py b/pandora/item/management/commands/sync_itemsort.py index 16640286..03080210 100644 --- a/pandora/item/management/commands/sync_itemsort.py +++ b/pandora/item/management/commands/sync_itemsort.py @@ -4,6 +4,7 @@ from optparse import make_option from django.core.management.base import BaseCommand from django.db import connection, transaction +from django.conf import settings from django.db.models import fields import monkey_patch.models @@ -75,6 +76,10 @@ class Command(BaseCommand): if name not in model_fields: sql = 'ALTER TABLE "%s" DROP COLUMN "%s"' % (table_name, name) changes.append(sql) + if settings.DB_GIN_TRGM: + gin_indexes = ['%s_findvalue' % n for n in settings.CONFIG['clipLayers']] + else: + gin_indexes = [] for f in clip.models.Clip._meta.fields: if not f.primary_key and not isinstance(f, fields.related.ForeignKey): name = f.name @@ -82,8 +87,13 @@ class Command(BaseCommand): if name not in db_fields: sql = 'ALTER TABLE "%s" ADD COLUMN "%s" %s' % (table_name, name, col_type) changes.append(sql) - sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, - table_name, name) + if name in gin_indexes: + sql = 'CREATE INDEX "%s_%s_idx" ON "%s" USING gin ("%s" gin_trgm_ops);' % ( + table_name, name, table_name, name + ) + else: + sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, + table_name, name) changes.append(sql) sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (table_name, name) changes.append(sql)