Skip to content

Commit ff2e810

Browse files
Add dbshell_plus management command.
1 parent 8d3cf98 commit ff2e810

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# coding: utf-8
2-
__version__ = '1.0'
2+
__version__ = '1.0.1'

lib/django_dbshell_plus/management/__init__.py

Whitespace-only changes.

lib/django_dbshell_plus/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import errno
2+
import subprocess
3+
4+
from django.db import connections
5+
from django.core.management.commands import dbshell
6+
7+
8+
class Command(dbshell.Command):
9+
10+
def handle(self, **options):
11+
connection = connections[options.get('database')]
12+
13+
dbclis = {
14+
'postgresql': 'pgcli',
15+
'mysql': 'mycli'
16+
}
17+
18+
cmd = dbclis.get(connection.vendor)
19+
if cmd:
20+
try:
21+
getattr(self, cmd)(connection)
22+
return
23+
except OSError, e:
24+
if e.errno != errno.ENOENT:
25+
self.stderr.write("Could not start %s: %s" % (cmd, str(e)))
26+
27+
super(Command, self).handle(**options)
28+
29+
def pgcli(self, connection):
30+
# argument code copied from Django
31+
settings_dict = connection.settings_dict
32+
args = ['pgcli']
33+
if settings_dict['USER']:
34+
args += ["-U", settings_dict['USER']]
35+
if settings_dict['HOST']:
36+
args.extend(["-h", settings_dict['HOST']])
37+
if settings_dict['PORT']:
38+
args.extend(["-p", str(settings_dict['PORT'])])
39+
args += [settings_dict['NAME']]
40+
41+
subprocess.call(args)
42+
43+
def mycli(self, connection):
44+
# argument code copied from Django
45+
settings_dict = connection.settings_dict
46+
args = ['mycli']
47+
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME'])
48+
user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
49+
passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
50+
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
51+
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT'])
52+
53+
if user:
54+
args += ["--user=%s" % user]
55+
if passwd:
56+
args += ["--pass=%s" % passwd]
57+
if host:
58+
if '/' in host:
59+
args += ["--socket=%s" % host]
60+
else:
61+
args += ["--host=%s" % host]
62+
if port:
63+
args += ["--port=%s" % port]
64+
if db:
65+
args += [db]
66+
67+
subprocess.call(args)

0 commit comments

Comments
 (0)