rebuild sort table if sort changes, add manage.py commands to rebuild filter, find and sort indexes
This commit is contained in:
parent
acb74219ee
commit
d056dbe13c
5 changed files with 143 additions and 1 deletions
32
pandora/item/management/commands/rebuild_filter.py
Normal file
32
pandora/item/management/commands/rebuild_filter.py
Normal 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
|
31
pandora/item/management/commands/rebuild_find.py
Normal file
31
pandora/item/management/commands/rebuild_find.py
Normal 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
|
34
pandora/item/management/commands/rebuild_indexes.py
Normal file
34
pandora/item/management/commands/rebuild_indexes.py
Normal 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
|
31
pandora/item/management/commands/rebuild_sort.py
Normal file
31
pandora/item/management/commands/rebuild_sort.py
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue