pandora/pandora/item/management/commands/sqlfindindex.py

46 lines
1.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2016-02-18 16:19:26 +05:30
from __future__ import print_function
2013-01-31 07:54:39 +00:00
from optparse import make_option
from django.core.management.base import BaseCommand
2013-01-31 20:41:46 +00:00
from django.db import connection, transaction
from django.conf import settings
settings.RELOAD_CONFIG = False
import app.monkey_patch
from ... import models
class Command(BaseCommand):
"""
print sql statement to add trigram
"""
help = 'sql create statements for find tables to use trigram index'
args = ''
2013-01-31 07:54:39 +00:00
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options):
2013-01-31 07:54:39 +00:00
cursor = connection.cursor()
def create_index(index, table, key):
2013-01-31 07:54:39 +00:00
sql = 'CREATE INDEX "%s" ON "%s" USING gin ("%s" gin_trgm_ops)' % (index, table, key)
if options['debug']:
2016-02-18 16:19:26 +05:30
print(sql)
2013-01-31 07:54:39 +00:00
cursor.execute(sql)
if settings.DB_GIN_TRGM:
import entity.models
for table_name, name in (
(models.ItemFind._meta.db_table, 'value'), # Item Find
(models.Clip._meta.db_table, 'findvalue'), # Clip Find
(entity.models.Find._meta.db_table, 'value'),# Entity Find
):
cursor = connection.cursor()
indexes = connection.introspection.get_indexes(cursor, table_name)
if name not in indexes:
create_index("%s_%s_idx"%(table_name, name), table_name, name)
2013-01-31 20:41:46 +00:00
transaction.commit_unless_managed()