Fixed #29534 -- Made dbshell use rlwrap on Oracle if available.

This commit is contained in:
Mariusz Felisiak 2018-10-26 01:39:42 +02:00 committed by Tim Graham
parent 9a88c6dd6a
commit c6525bea9e
3 changed files with 41 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import shutil
import subprocess import subprocess
from django.db.backends.base.client import BaseDatabaseClient from django.db.backends.base.client import BaseDatabaseClient
@ -5,8 +6,12 @@ from django.db.backends.base.client import BaseDatabaseClient
class DatabaseClient(BaseDatabaseClient): class DatabaseClient(BaseDatabaseClient):
executable_name = 'sqlplus' executable_name = 'sqlplus'
wrapper_name = 'rlwrap'
def runshell(self): def runshell(self):
conn_string = self.connection._connect_string() conn_string = self.connection._connect_string()
args = [self.executable_name, "-L", conn_string] args = [self.executable_name, "-L", conn_string]
wrapper_path = shutil.which(self.wrapper_name)
if wrapper_path:
args = [wrapper_path, *args]
subprocess.check_call(args) subprocess.check_call(args)

View File

@ -185,6 +185,9 @@ Management Commands
* :djadmin:`inspectdb` now introspects :class:`~django.db.models.DurationField` * :djadmin:`inspectdb` now introspects :class:`~django.db.models.DurationField`
for Oracle and PostgreSQL. for Oracle and PostgreSQL.
* On Oracle, :djadmin:`dbshell` is wrapped with ``rlwrap``, if available.
``rlwrap`` provides a command history and editing of keyboard input.
Migrations Migrations
~~~~~~~~~~ ~~~~~~~~~~

View File

@ -0,0 +1,33 @@
from unittest import mock, skipUnless
from django.db import connection
from django.db.backends.oracle.client import DatabaseClient
from django.test import SimpleTestCase
@skipUnless(connection.vendor == 'oracle', 'Oracle tests')
class OracleDbshellTests(SimpleTestCase):
def _run_dbshell(self, rlwrap=False):
"""Run runshell command and capture its arguments."""
def _mock_subprocess_call(*args):
self.subprocess_args = tuple(*args)
return 0
client = DatabaseClient(connection)
self.subprocess_args = None
with mock.patch('subprocess.call', new=_mock_subprocess_call):
with mock.patch('shutil.which', return_value='/usr/bin/rlwrap' if rlwrap else None):
client.runshell()
return self.subprocess_args
def test_without_rlwrap(self):
self.assertEqual(
self._run_dbshell(rlwrap=False),
('sqlplus', '-L', connection._connect_string()),
)
def test_with_rlwrap(self):
self.assertEqual(
self._run_dbshell(rlwrap=True),
('/usr/bin/rlwrap', 'sqlplus', '-L', connection._connect_string()),
)