Fixed #26698 -- Fixed PostgreSQL dbshell crash on an empty database name.

This commit is contained in:
mieciu 2016-06-02 14:03:03 -07:00 committed by Tim Graham
parent 4a4d7f980e
commit 19ff506878
3 changed files with 36 additions and 33 deletions

View File

@ -17,14 +17,14 @@ class DatabaseClient(BaseDatabaseClient):
executable_name = 'psql' executable_name = 'psql'
@classmethod @classmethod
def runshell_db(cls, settings_dict): def runshell_db(cls, conn_params):
args = [cls.executable_name] args = [cls.executable_name]
host = settings_dict.get('HOST', '') host = conn_params.get('host', '')
port = settings_dict.get('PORT', '') port = conn_params.get('port', '')
name = settings_dict.get('NAME', '') dbname = conn_params.get('database', '')
user = settings_dict.get('USER', '') user = conn_params.get('user', '')
passwd = settings_dict.get('PASSWORD', '') passwd = conn_params.get('password', '')
if user: if user:
args += ['-U', user] args += ['-U', user]
@ -32,7 +32,7 @@ class DatabaseClient(BaseDatabaseClient):
args += ['-h', host] args += ['-h', host]
if port: if port:
args += ['-p', str(port)] args += ['-p', str(port)]
args += [name] args += [dbname]
temp_pgpass = None temp_pgpass = None
try: try:
@ -43,7 +43,7 @@ class DatabaseClient(BaseDatabaseClient):
print_( print_(
_escape_pgpass(host) or '*', _escape_pgpass(host) or '*',
str(port) or '*', str(port) or '*',
_escape_pgpass(name) or '*', _escape_pgpass(dbname) or '*',
_escape_pgpass(user) or '*', _escape_pgpass(user) or '*',
_escape_pgpass(passwd), _escape_pgpass(passwd),
file=temp_pgpass, file=temp_pgpass,
@ -63,4 +63,4 @@ class DatabaseClient(BaseDatabaseClient):
del os.environ['PGPASSFILE'] del os.environ['PGPASSFILE']
def runshell(self): def runshell(self):
DatabaseClient.runshell_db(self.connection.settings_dict) DatabaseClient.runshell_db(self.connection.get_connection_params())

View File

@ -23,3 +23,6 @@ Bugfixes
* Fixed ``HStoreField`` to raise a ``ValidationError`` instead of crashing on * Fixed ``HStoreField`` to raise a ``ValidationError`` instead of crashing on
non-dictionary JSON input (:ticket:`26672`). non-dictionary JSON input (:ticket:`26672`).
* Fixed ``dbshell`` crash on PostgreSQL with an empty database name
(:ticket:`26698`).

View File

@ -37,11 +37,11 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
def test_basic(self): def test_basic(self):
self.assertEqual( self.assertEqual(
self._run_it({ self._run_it({
'NAME': 'dbname', 'database': 'dbname',
'USER': 'someuser', 'user': 'someuser',
'PASSWORD': 'somepassword', 'password': 'somepassword',
'HOST': 'somehost', 'host': 'somehost',
'PORT': 444, 'port': '444',
}), ( }), (
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'], ['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
b'somehost:444:dbname:someuser:somepassword', b'somehost:444:dbname:someuser:somepassword',
@ -51,10 +51,10 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
def test_nopass(self): def test_nopass(self):
self.assertEqual( self.assertEqual(
self._run_it({ self._run_it({
'NAME': 'dbname', 'database': 'dbname',
'USER': 'someuser', 'user': 'someuser',
'HOST': 'somehost', 'host': 'somehost',
'PORT': 444, 'port': '444',
}), ( }), (
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'], ['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
None, None,
@ -64,11 +64,11 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
def test_column(self): def test_column(self):
self.assertEqual( self.assertEqual(
self._run_it({ self._run_it({
'NAME': 'dbname', 'database': 'dbname',
'USER': 'some:user', 'user': 'some:user',
'PASSWORD': 'some:password', 'password': 'some:password',
'HOST': '::1', 'host': '::1',
'PORT': 444, 'port': '444',
}), ( }), (
['psql', '-U', 'some:user', '-h', '::1', '-p', '444', 'dbname'], ['psql', '-U', 'some:user', '-h', '::1', '-p', '444', 'dbname'],
b'\\:\\:1:444:dbname:some\\:user:some\\:password', b'\\:\\:1:444:dbname:some\\:user:some\\:password',
@ -78,11 +78,11 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
def test_escape_characters(self): def test_escape_characters(self):
self.assertEqual( self.assertEqual(
self._run_it({ self._run_it({
'NAME': 'dbname', 'database': 'dbname',
'USER': 'some\\user', 'user': 'some\\user',
'PASSWORD': 'some\\password', 'password': 'some\\password',
'HOST': 'somehost', 'host': 'somehost',
'PORT': 444, 'port': '444',
}), ( }), (
['psql', '-U', 'some\\user', '-h', 'somehost', '-p', '444', 'dbname'], ['psql', '-U', 'some\\user', '-h', 'somehost', '-p', '444', 'dbname'],
b'somehost:444:dbname:some\\\\user:some\\\\password', b'somehost:444:dbname:some\\\\user:some\\\\password',
@ -106,11 +106,11 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
self.skipTest("Your locale can't run this test.") self.skipTest("Your locale can't run this test.")
self.assertEqual( self.assertEqual(
self._run_it({ self._run_it({
'NAME': 'dbname', 'database': 'dbname',
'USER': username_str, 'user': username_str,
'PASSWORD': password_str, 'password': password_str,
'HOST': 'somehost', 'host': 'somehost',
'PORT': 444, 'port': '444',
}), ( }), (
['psql', '-U', username_str, '-h', 'somehost', '-p', '444', 'dbname'], ['psql', '-U', username_str, '-h', 'somehost', '-p', '444', 'dbname'],
pgpass_bytes, pgpass_bytes,