Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

magic-removal: Fixed #1631 -- Added 'django-admin.py dbshell' command…

…, which runs the command-line client for your database engine with your connection settings. Thanks, Paul Bissex

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2693 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b59d57e4d23698351d367c8f42d79ed2bc7c295a 1 parent 47333f6
@adrianholovaty adrianholovaty authored
View
12 django/core/management.py
@@ -1045,11 +1045,18 @@ def run_shell(use_plain=False):
code.interact()
run_shell.args = '[--plain]'
+def dbshell():
+ "Runs the command-line client for the current DATABASE_ENGINE."
+ from django.db import runshell
+ runshell()
+dbshell.args = ""
+
# Utilities for command-line script
DEFAULT_ACTION_MAPPING = {
'adminindex': get_admin_index,
'createcachetable' : createcachetable,
+ 'dbshell': dbshell,
'diffsettings': diffsettings,
'inspectdb': inspectdb,
'install': install,
@@ -1072,11 +1079,12 @@ def run_shell(use_plain=False):
NO_SQL_TRANSACTION = (
'adminindex',
'createcachetable',
+ 'dbshell',
'diffsettings',
'install',
'reset',
'sqlindexes',
- 'syncdb'
+ 'syncdb',
)
class DjangoOptionParser(OptionParser):
@@ -1138,7 +1146,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
if action == 'shell':
action_mapping[action](options.plain is True)
- elif action in ('syncdb', 'validate', 'diffsettings'):
+ elif action in ('syncdb', 'validate', 'diffsettings', 'dbshell'):
action_mapping[action]()
elif action == 'inspectdb':
try:
View
1  django/db/__init__.py
@@ -22,6 +22,7 @@
get_introspection_module = lambda: __import__('django.db.backends.%s.introspection' % settings.DATABASE_ENGINE, '', '', [''])
get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, '', '', [''])
+runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, '', '', ['']).runshell()
connection = backend.DatabaseWrapper()
DatabaseError = backend.DatabaseError
View
2  django/db/backends/ado_mssql/client.py
@@ -0,0 +1,2 @@
+def runshell():
+ raise NotImplementedError
View
3  django/db/backends/dummy/client.py
@@ -0,0 +1,3 @@
+from django.db.backends.dummy.base import complain
+
+runshell = complain
View
14 django/db/backends/mysql/client.py
@@ -0,0 +1,14 @@
+from django.conf import settings
+import os
+
+def runshell():
+ args = ['']
+ args += ["--user=%s" % settings.DATABASE_USER]
+ if settings.DATABASE_PASSWORD:
+ args += ["--password=%s" % settings.DATABASE_PASSWORD]
+ if settings.DATABASE_HOST:
+ args += ["--host=%s" % settings.DATABASE_HOST]
+ if settings.DATABASE_PORT:
+ args += ["--port=%s" % settings.DATABASE_PORT]
+ args += [settings.DATABASE_NAME]
+ os.execvp('mysql', args)
View
14 django/db/backends/postgresql/client.py
@@ -0,0 +1,14 @@
+from django.conf import settings
+import os
+
+def runshell():
+ args = ['']
+ args += ["-U%s" % settings.DATABASE_USER]
+ if settings.DATABASE_PASSWORD:
+ args += ["-W"]
+ if settings.DATABASE_HOST:
+ args += ["-h %s" % settings.DATABASE_HOST]
+ if settings.DATABASE_PORT:
+ args += ["-p %s" % settings.DATABASE_PORT]
+ args += [settings.DATABASE_NAME]
+ os.execvp('psql', args)
View
6 django/db/backends/sqlite3/client.py
@@ -0,0 +1,6 @@
+from django.conf import settings
+import os
+
+def runshell():
+ args = ['', settings.DATABASE_NAME]
+ os.execvp('sqlite3', args)
View
15 docs/django-admin.txt
@@ -64,6 +64,21 @@ backend. See the `cache documentation`_ for more information.
.. _cache documentation: http://www.djangoproject.com/documentation/cache/
+dbshell
+-------
+
+Runs the command-line client for the database engine specified in your
+``DATABASE_ENGINE`` setting, with the connection parameters specified in your
+``DATABASE_USER``, ``DATABASE_PASSWORD``, etc., settings.
+
+ * For PostgreSQL, this runs the ``psql`` command-line client.
+ * For MySQL, this runs the ``mysql`` command-line client.
+ * For SQLite, this runs the ``sqlite3`` command-line client.
+
+This command assumes the programs are on your PATH so that a simple call to
+the program name (``psql``, ``mysql``, ``sqlite3``) will find the program in
+the right place.
+
diffsettings
------------
Please sign in to comment.
Something went wrong with that request. Please try again.