Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from AlmogCohen/add-drop-db-command
add drop_pg_db command
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!')) |