2012-03-30 16:57:07 +02:00
|
|
|
# -*- 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
|
2012-03-30 16:57:07 +02:00
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
2013-01-31 20:41:46 +00:00
|
|
|
from django.db import connection, transaction
|
2012-03-30 16:57:07 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
2013-07-15 12:49:05 +00:00
|
|
|
settings.RELOAD_CONFIG = False
|
2013-05-18 21:43:52 +00:00
|
|
|
import app.monkey_patch
|
2012-03-30 16:57:07 +02:00
|
|
|
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'),
|
|
|
|
|
)
|
2012-03-30 16:57:07 +02:00
|
|
|
|
|
|
|
|
def handle(self, **options):
|
2013-01-31 07:54:39 +00:00
|
|
|
cursor = connection.cursor()
|
2015-04-17 11:34:44 +01:00
|
|
|
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:
|
2015-04-17 11:17:12 +01:00
|
|
|
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:
|
2015-04-17 11:34:44 +01:00
|
|
|
create_index("%s_%s_idx"%(table_name, name), table_name, name)
|
2013-01-31 20:41:46 +00:00
|
|
|
transaction.commit_unless_managed()
|