Skip to content

Commit

Permalink
Merge pull request #27 from AlmogCohen/add-drop-db-command
Browse files Browse the repository at this point in the history
add drop_pg_db command
  • Loading branch information
Rich Jones committed Jul 27, 2018
2 parents c7044b3 + 199e5f8 commit e71aa60
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions zappa_django_utils/management/commands/drop_pg_db.py
@@ -0,0 +1,29 @@
from psycopg2 import connect
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from django.core.management.base import BaseCommand
from django.conf import settings


class Command(BaseCommand):
help = 'Drop the Postgres database completely'

def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('Starting to drop DB..'))

dbname = settings.DATABASES['default']['NAME']
user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
host = settings.DATABASES['default']['HOST']

self.stdout.write(self.style.SUCCESS('Connecting to host..'))
con = connect(dbname='postgres', user=user, host=host, password=password)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)

self.stdout.write(self.style.SUCCESS("Dropping database '{}'".format(dbname)))
cur = con.cursor()
cur.execute('DROP DATABASE ' + dbname)
cur.close()

con.close()

self.stdout.write(self.style.SUCCESS('All done!'))

0 comments on commit e71aa60

Please sign in to comment.