rebuild sort table if sort changes, add manage.py commands to rebuild filter, find and sort indexes

This commit is contained in:
j 2013-09-23 15:35:18 +00:00
parent acb74219ee
commit d056dbe13c
5 changed files with 143 additions and 1 deletions

View file

@ -0,0 +1,32 @@
# -*- 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, transaction
from django.db.models import fields
from django.conf import settings
settings.RELOAD_CONFIG = False
import app.monkey_patch
from ... import models
import clip.models
class Command(BaseCommand):
help = 'update filters, run after adding new filters'
args = ''
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options):
ids = [i['id'] for i in models.Item.objects.all().values('id')]
for id in ids:
try:
i = models.Item.objects.get(pk=id)
if options['debug']:
print i
i.update_facets()
except:
pass

View file

@ -0,0 +1,31 @@
# -*- 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, transaction
from django.db.models import fields
from django.conf import settings
settings.RELOAD_CONFIG = False
import app.monkey_patch
from ... import models
import clip.models
class Command(BaseCommand):
help = 'update find values, run after adding new keys'
args = ''
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options):
ids = [i['id'] for i in models.Item.objects.all().values('id')]
for id in ids:
try:
i = models.Item.objects.get(pk=id)
print i
i.update_find()
except:
pass

View file

@ -0,0 +1,34 @@
# -*- 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, transaction
from django.db.models import fields
from django.conf import settings
settings.RELOAD_CONFIG = False
import app.monkey_patch
from ... import models
import clip.models
class Command(BaseCommand):
help = 'update find, sort, filter indexes'
args = ''
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options):
ids = [i['id'] for i in models.Item.objects.all().values('id')]
for id in ids:
try:
i = models.Item.objects.get(pk=id)
if options['debug']:
print i
i.update_facets()
i.update_sort()
i.update_find()
except:
pass

View file

@ -0,0 +1,31 @@
# -*- 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, transaction
from django.db.models import fields
from django.conf import settings
settings.RELOAD_CONFIG = False
import app.monkey_patch
from ... import models
class Command(BaseCommand):
help = 'update sort values, run after changing sort keys'
args = ''
option_list = BaseCommand.option_list + (
make_option('--debug', action='store_true', dest='debug',
default=False, help='print sql commans'),
)
def handle(self, **options):
ids = [i['id'] for i in models.Item.objects.all().values('id')]
for id in ids:
try:
i = models.Item.objects.get(pk=id)
if options['debug']:
print i
i.update_sort()
except:
pass

View file

@ -32,6 +32,7 @@ class Command(BaseCommand):
connection.introspection.data_types_reverse[row[1]]) for row in db_rows]) connection.introspection.data_types_reverse[row[1]]) for row in db_rows])
model_fields = ['item_id'] + [f.name for f in models.ItemSort._meta.fields] model_fields = ['item_id'] + [f.name for f in models.ItemSort._meta.fields]
rebuild = False
changes = [] changes = []
for name in db_types: for name in db_types:
@ -49,6 +50,7 @@ class Command(BaseCommand):
sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name,
table_name, name) table_name, name)
changes.append(sql) changes.append(sql)
rebuild = True
elif f.__class__.__name__ != db_types[name]: elif f.__class__.__name__ != db_types[name]:
sql = 'ALTER TABLE "%s" DROP COLUMN "%s"' % (table_name, name ) sql = 'ALTER TABLE "%s" DROP COLUMN "%s"' % (table_name, name )
changes.append(sql) changes.append(sql)
@ -57,6 +59,7 @@ class Command(BaseCommand):
sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name,
table_name, name) table_name, name)
changes.append(sql) changes.append(sql)
rebuild = True
elif db_types[name] == 'CharField' and db_fields[name][3] != f.max_length: elif db_types[name] == 'CharField' and db_fields[name][3] != f.max_length:
sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s' % (table_name, name, sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s' % (table_name, name,
col_type) col_type)
@ -64,6 +67,7 @@ class Command(BaseCommand):
sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL' % (table_name, name, sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL' % (table_name, name,
f.null and "DROP" or "SET") f.null and "DROP" or "SET")
changes.append(sql) changes.append(sql)
rebuild = True
#also update clip index #also update clip index
table_name = clip.models.Clip._meta.db_table table_name = clip.models.Clip._meta.db_table
@ -98,9 +102,19 @@ class Command(BaseCommand):
sql = 'BEGIN' sql = 'BEGIN'
changes.append(sql) changes.append(sql)
if changes: if changes:
print "Database needs to be updated, plase wait..." print "Updating database schema..."
for sql in changes: for sql in changes:
if options['debug']: if options['debug']:
print sql print sql
cursor.execute(sql) cursor.execute(sql)
transaction.commit_unless_managed() transaction.commit_unless_managed()
if rebuild:
print "Updating sort values..."
ids = [i['id'] for i in models.Item.objects.all().values('id')]
for id in ids:
#try:
i = models.Item.objects.get(pk=id)
print i
i.update_sort()
#except:
# pass