From d056dbe13c4dd0f299b64b3cd2cf3c07ea4ab82a Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 23 Sep 2013 15:35:18 +0000 Subject: [PATCH] rebuild sort table if sort changes, add manage.py commands to rebuild filter, find and sort indexes --- .../management/commands/rebuild_filter.py | 32 +++++++++++++++++ .../item/management/commands/rebuild_find.py | 31 +++++++++++++++++ .../management/commands/rebuild_indexes.py | 34 +++++++++++++++++++ .../item/management/commands/rebuild_sort.py | 31 +++++++++++++++++ .../item/management/commands/sync_itemsort.py | 16 ++++++++- 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 pandora/item/management/commands/rebuild_filter.py create mode 100644 pandora/item/management/commands/rebuild_find.py create mode 100644 pandora/item/management/commands/rebuild_indexes.py create mode 100644 pandora/item/management/commands/rebuild_sort.py diff --git a/pandora/item/management/commands/rebuild_filter.py b/pandora/item/management/commands/rebuild_filter.py new file mode 100644 index 000000000..05cf0ecca --- /dev/null +++ b/pandora/item/management/commands/rebuild_filter.py @@ -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 diff --git a/pandora/item/management/commands/rebuild_find.py b/pandora/item/management/commands/rebuild_find.py new file mode 100644 index 000000000..4dca7c149 --- /dev/null +++ b/pandora/item/management/commands/rebuild_find.py @@ -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 diff --git a/pandora/item/management/commands/rebuild_indexes.py b/pandora/item/management/commands/rebuild_indexes.py new file mode 100644 index 000000000..9651f4c02 --- /dev/null +++ b/pandora/item/management/commands/rebuild_indexes.py @@ -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 diff --git a/pandora/item/management/commands/rebuild_sort.py b/pandora/item/management/commands/rebuild_sort.py new file mode 100644 index 000000000..b167d3802 --- /dev/null +++ b/pandora/item/management/commands/rebuild_sort.py @@ -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 diff --git a/pandora/item/management/commands/sync_itemsort.py b/pandora/item/management/commands/sync_itemsort.py index 69804621e..981ecb9bf 100644 --- a/pandora/item/management/commands/sync_itemsort.py +++ b/pandora/item/management/commands/sync_itemsort.py @@ -32,6 +32,7 @@ class Command(BaseCommand): 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] + rebuild = False changes = [] for name in db_types: @@ -49,6 +50,7 @@ class Command(BaseCommand): sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, table_name, name) changes.append(sql) + rebuild = True elif f.__class__.__name__ != db_types[name]: sql = 'ALTER TABLE "%s" DROP COLUMN "%s"' % (table_name, name ) changes.append(sql) @@ -57,6 +59,7 @@ class Command(BaseCommand): sql = 'CREATE INDEX "%s_%s_idx" ON "%s" ("%s")' % (table_name, name, table_name, name) changes.append(sql) + rebuild = True 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, col_type) @@ -64,6 +67,7 @@ class Command(BaseCommand): sql = 'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL' % (table_name, name, f.null and "DROP" or "SET") changes.append(sql) + rebuild = True #also update clip index table_name = clip.models.Clip._meta.db_table @@ -98,9 +102,19 @@ class Command(BaseCommand): sql = 'BEGIN' changes.append(sql) if changes: - print "Database needs to be updated, plase wait..." + print "Updating database schema..." for sql in changes: if options['debug']: print sql cursor.execute(sql) 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