Skip to content

Instantly share code, notes, and snippets.

@siegy22
Last active February 7, 2017 11:19
Show Gist options
  • Save siegy22/de62a4528381da6498cb4bf191c1322a to your computer and use it in GitHub Desktop.
Save siegy22/de62a4528381da6498cb4bf191c1322a to your computer and use it in GitHub Desktop.
import os
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = "Drops your database using psql command"
def add_arguments(self, parser):
parser.add_argument(
'-R', '--router', action='store', dest='router', default='default',
help='Use this router-database other then defined in settings.py')
def handle(self, *args, **options):
router = options.get('router')
dbinfo = settings.DATABASES.get(router)
if dbinfo is None:
raise CommandError("Unknown database router %s" % router)
dbname = dbinfo.get('NAME')
drop_command = "DROP DATABASE {0};".format(dbname)
os.system('psql postgres -q -c "{0}"'.format(drop_command))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment