-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
dbshell_plus
management command.
- Loading branch information
1 parent
8d3cf98
commit ff2e810
Showing
4 changed files
with
68 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# coding: utf-8 | ||
__version__ = '1.0' | ||
__version__ = '1.0.1' |
Empty file.
Empty file.
67 changes: 67 additions & 0 deletions
67
lib/django_dbshell_plus/management/commands/dbshell_plus.py
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,67 @@ | ||
import errno | ||
import subprocess | ||
|
||
from django.db import connections | ||
from django.core.management.commands import dbshell | ||
|
||
|
||
class Command(dbshell.Command): | ||
|
||
def handle(self, **options): | ||
connection = connections[options.get('database')] | ||
|
||
dbclis = { | ||
'postgresql': 'pgcli', | ||
'mysql': 'mycli' | ||
} | ||
|
||
cmd = dbclis.get(connection.vendor) | ||
if cmd: | ||
try: | ||
getattr(self, cmd)(connection) | ||
return | ||
except OSError, e: | ||
if e.errno != errno.ENOENT: | ||
self.stderr.write("Could not start %s: %s" % (cmd, str(e))) | ||
|
||
super(Command, self).handle(**options) | ||
|
||
def pgcli(self, connection): | ||
# argument code copied from Django | ||
settings_dict = connection.settings_dict | ||
args = ['pgcli'] | ||
if settings_dict['USER']: | ||
args += ["-U", settings_dict['USER']] | ||
if settings_dict['HOST']: | ||
args.extend(["-h", settings_dict['HOST']]) | ||
if settings_dict['PORT']: | ||
args.extend(["-p", str(settings_dict['PORT'])]) | ||
args += [settings_dict['NAME']] | ||
|
||
subprocess.call(args) | ||
|
||
def mycli(self, connection): | ||
# argument code copied from Django | ||
settings_dict = connection.settings_dict | ||
args = ['mycli'] | ||
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME']) | ||
user = settings_dict['OPTIONS'].get('user', settings_dict['USER']) | ||
passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD']) | ||
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST']) | ||
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT']) | ||
|
||
if user: | ||
args += ["--user=%s" % user] | ||
if passwd: | ||
args += ["--pass=%s" % passwd] | ||
if host: | ||
if '/' in host: | ||
args += ["--socket=%s" % host] | ||
else: | ||
args += ["--host=%s" % host] | ||
if port: | ||
args += ["--port=%s" % port] | ||
if db: | ||
args += [db] | ||
|
||
subprocess.call(args) |