Skip to content

Commit

Permalink
clear_index: adopt new behaviour of update_index
Browse files Browse the repository at this point in the history
* By default, all backends are cleared
* --using may be specified multiple times
  • Loading branch information
acdha committed Jan 24, 2013
1 parent a018d7b commit 1bfb090
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions haystack/management/commands/clear_index.py
@@ -1,7 +1,7 @@
from optparse import make_option from optparse import make_option
import sys import sys

from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from haystack.constants import DEFAULT_ALIAS




class Command(BaseCommand): class Command(BaseCommand):
Expand All @@ -10,35 +10,41 @@ class Command(BaseCommand):
make_option('--noinput', action='store_false', dest='interactive', default=True, make_option('--noinput', action='store_false', dest='interactive', default=True,
help='If provided, no prompts will be issued to the user and the data will be wiped out.' help='If provided, no prompts will be issued to the user and the data will be wiped out.'
), ),
make_option("-u", "--using", action="store", type="string", dest="using", default=DEFAULT_ALIAS, make_option("-u", "--using", action="append", dest="using",
help='If provided, chooses a connection to work with.' default=[],
help='Update only the named backend (can be used multiple times). '
'By default all backends will be updated.'
), ),
) )
option_list = BaseCommand.option_list + base_options option_list = BaseCommand.option_list + base_options

def handle(self, **options): def handle(self, **options):
"""Clears out the search index completely.""" """Clears out the search index completely."""
from haystack import connections from haystack import connections
self.verbosity = int(options.get('verbosity', 1)) self.verbosity = int(options.get('verbosity', 1))
self.using = options.get('using')

using = options.get('using')
if not using:
using = connections.connections_info.keys()

if options.get('interactive', True): if options.get('interactive', True):
print print
print "WARNING: This will irreparably remove EVERYTHING from your search index in connection '%s'." % self.using print "WARNING: This will irreparably remove EVERYTHING from your search index in connection '%s'." % "', '".join(using)
print "Your choices after this are to restore from backups or rebuild via the `rebuild_index` command." print "Your choices after this are to restore from backups or rebuild via the `rebuild_index` command."

yes_or_no = raw_input("Are you sure you wish to continue? [y/N] ") yes_or_no = raw_input("Are you sure you wish to continue? [y/N] ")
print print

if not yes_or_no.lower().startswith('y'): if not yes_or_no.lower().startswith('y'):
print "No action taken." print "No action taken."
sys.exit() sys.exit()

if self.verbosity >= 1: if self.verbosity >= 1:
print "Removing all documents from your index because you said so." print "Removing all documents from your index because you said so."


backend = connections[self.using].get_backend() for backend_name in using:
backend.clear() backend = connections[backend_name].get_backend()

backend.clear()

if self.verbosity >= 1: if self.verbosity >= 1:
print "All documents removed." print "All documents removed."

0 comments on commit 1bfb090

Please sign in to comment.