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
|
|
|
|
|
|
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 = ''
|
2016-02-19 18:19:01 +00:00
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
|
parser.add_argument('--debug', action='store_true', dest='debug',
|
2016-06-26 16:55:58 +02:00
|
|
|
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()
|
2016-06-26 16:55:58 +02:00
|
|
|
|
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
|
2016-10-05 00:00:03 +02:00
|
|
|
import document.models
|
2016-06-26 16:55:58 +02:00
|
|
|
for table, column in (
|
2016-12-31 23:31:17 +01:00
|
|
|
(models.ItemFind._meta.db_table, 'value'), # Item Find
|
|
|
|
|
(models.Clip._meta.db_table, 'findvalue'), # Clip Find
|
|
|
|
|
(models.Annotation._meta.db_table, 'findvalue'), # Annotation Find
|
|
|
|
|
(entity.models.Find._meta.db_table, 'value'), # Entity Find
|
|
|
|
|
(document.models.Find._meta.db_table, 'value'), # Document Find
|
2015-04-17 11:17:12 +01:00
|
|
|
):
|
|
|
|
|
cursor = connection.cursor()
|
2016-06-26 16:55:58 +02:00
|
|
|
indexes = connection.introspection.get_indexes(cursor, table)
|
|
|
|
|
drop = []
|
|
|
|
|
if column in indexes:
|
|
|
|
|
sql = "SELECT indexname, indexdef FROM pg_catalog.pg_indexes " + \
|
2016-12-31 23:31:17 +01:00
|
|
|
"WHERE indexdef LIKE '%ON {table}%' AND indexdef LIKE '%{column}%'".format(table=table, column=column)
|
2016-06-26 16:55:58 +02:00
|
|
|
cursor.execute(sql)
|
|
|
|
|
for r in cursor:
|
|
|
|
|
if 'USING gin' not in r[1]:
|
|
|
|
|
drop.append(r[0])
|
|
|
|
|
if drop:
|
|
|
|
|
for idx in drop:
|
|
|
|
|
sql = 'DROP INDEX ' + idx
|
|
|
|
|
if options['debug']:
|
|
|
|
|
print(sql)
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
|
indexes = connection.introspection.get_indexes(cursor, table)
|
|
|
|
|
if column not in indexes:
|
|
|
|
|
create_index("%s_%s_idx" % (table, column), table, column)
|
2016-02-19 18:19:01 +00:00
|
|
|
transaction.commit()
|