Fixed #29015 -- Added an exception if the PostgreSQL database name is too long.

This commit is contained in:
priyanshsaxena 2018-01-18 23:45:16 +05:30 committed by Tim Graham
parent 6d1f576945
commit 6b3d292043
2 changed files with 19 additions and 0 deletions

View File

@ -149,6 +149,12 @@ class DatabaseWrapper(BaseDatabaseWrapper):
raise ImproperlyConfigured( raise ImproperlyConfigured(
"settings.DATABASES is improperly configured. " "settings.DATABASES is improperly configured. "
"Please supply the NAME value.") "Please supply the NAME value.")
if len(settings_dict['NAME'] or '') > self.ops.max_name_length():
raise ImproperlyConfigured(
'Database names longer than %d characters are not supported by '
'PostgreSQL. Supply a shorter NAME in settings.DATABASES.'
% self.ops.max_name_length()
)
conn_params = { conn_params = {
'database': settings_dict['NAME'] or 'postgres', 'database': settings_dict['NAME'] or 'postgres',
**settings_dict['OPTIONS'], **settings_dict['OPTIONS'],

View File

@ -2,6 +2,7 @@ import unittest
import warnings import warnings
from unittest import mock from unittest import mock
from django.core.exceptions import ImproperlyConfigured
from django.db import DatabaseError, connection, connections from django.db import DatabaseError, connection, connections
from django.test import TestCase from django.test import TestCase
@ -39,6 +40,18 @@ class Tests(TestCase):
self.assertEqual(len(w), 1) self.assertEqual(len(w), 1)
self.assertEqual(w[0].message.__class__, RuntimeWarning) self.assertEqual(w[0].message.__class__, RuntimeWarning)
def test_database_name_too_long(self):
from django.db.backends.postgresql.base import DatabaseWrapper
settings = connection.settings_dict.copy()
max_name_length = connection.ops.max_name_length()
settings['NAME'] = 'a' + (max_name_length * 'a')
msg = (
'Database names longer than %d characters are not supported by '
'PostgreSQL. Supply a shorter NAME in settings.DATABASES.'
) % max_name_length
with self.assertRaisesMessage(ImproperlyConfigured, msg):
DatabaseWrapper(settings).get_connection_params()
def test_connect_and_rollback(self): def test_connect_and_rollback(self):
""" """
PostgreSQL shouldn't roll back SET TIME ZONE, even if the first PostgreSQL shouldn't roll back SET TIME ZONE, even if the first