forked from 0x2620/pandora
create and update GNI indexes
This commit is contained in:
parent
90758ca156
commit
c748527921
2 changed files with 43 additions and 5 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue