summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2018-10-26 01:39:42 +0200
committerTim Graham <timograham@gmail.com>2018-10-25 19:39:42 -0400
commitc6525bea9ec3d1d1e73585ca6e3116bb55afb336 (patch)
treec1b7da006c7ee1ac53854f4b2b6687db2bfd2542
parent9a88c6dd6aa84a1b35e585faa0058a0996cc7ed7 (diff)
Fixed #29534 -- Made dbshell use rlwrap on Oracle if available.
-rw-r--r--django/db/backends/oracle/client.py5
-rw-r--r--docs/releases/2.2.txt3
-rw-r--r--tests/dbshell/test_oracle.py33
3 files changed, 41 insertions, 0 deletions
diff --git a/django/db/backends/oracle/client.py b/django/db/backends/oracle/client.py
index 102e77fd15..4c5070a207 100644
--- a/django/db/backends/oracle/client.py
+++ b/django/db/backends/oracle/client.py
@@ -1,3 +1,4 @@
+import shutil
import subprocess
from django.db.backends.base.client import BaseDatabaseClient
@@ -5,8 +6,12 @@ from django.db.backends.base.client import BaseDatabaseClient
class DatabaseClient(BaseDatabaseClient):
executable_name = 'sqlplus'
+ wrapper_name = 'rlwrap'
def runshell(self):
conn_string = self.connection._connect_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)
diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt
index 8e03f54c3e..5d76289da6 100644
--- a/docs/releases/2.2.txt
+++ b/docs/releases/2.2.txt
@@ -185,6 +185,9 @@ Management Commands
* :djadmin:`inspectdb` now introspects :class:`~django.db.models.DurationField`
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
~~~~~~~~~~
diff --git a/tests/dbshell/test_oracle.py b/tests/dbshell/test_oracle.py
new file mode 100644
index 0000000000..d236a932ab
--- /dev/null
+++ b/tests/dbshell/test_oracle.py
@@ -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()),
+ )