0003-misc-adapt-dbshell-command-for-django-3.2-64295.patch
tests/test_ctl.py | ||
---|---|---|
4 | 4 |
import sys |
5 | 5 |
from unittest import mock |
6 | 6 | |
7 |
import django |
|
7 | 8 |
import psycopg2 |
8 | 9 |
import pytest |
9 | 10 |
from django.core.management import CommandError, call_command |
... | ... | |
509 | 510 |
with pytest.raises(CommandError): |
510 | 511 |
call_command('dbshell') # missing tenant name |
511 | 512 | |
512 |
with mock.patch('subprocess.call') as call: |
|
513 |
call.side_effect = lambda *args: 0 |
|
513 |
with mock.patch('subprocess.call' if django.VERSION < (3, 2) else 'subprocess.run') as call:
|
|
514 |
call.side_effect = lambda *args, **kwargs: 0
|
|
514 | 515 |
call_command('dbshell', '--domain', 'example.net') |
515 | 516 |
assert call.call_args[0][-1][0] == 'psql' |
516 | 517 |
assert call.call_args[0][-1][-1] == sql_pub.cfg['postgresql']['database'] |
wcs/ctl/management/commands/dbshell.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU General Public License |
15 | 15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import collections |
|
18 | ||
19 |
import django |
|
17 | 20 |
from django.core.management.base import CommandError |
18 | 21 |
from django.db.backends.postgresql.client import DatabaseClient |
19 | 22 | |
20 | 23 |
from . import TenantCommand |
21 | 24 | |
22 | 25 | |
26 |
class PsqlDatabaseClient(DatabaseClient): |
|
27 |
def __init__(self, pub): |
|
28 |
# emulate a connection object |
|
29 |
self.connection = collections.namedtuple('Connection', ['settings_dict'])({}) |
|
30 |
for key, value in pub.cfg['postgresql'].items(): |
|
31 |
if key == 'database': |
|
32 |
key = 'name' |
|
33 |
self.connection.settings_dict[key.upper()] = value |
|
34 |
super().__init__(self.connection) |
|
35 | ||
36 | ||
23 | 37 |
class Command(TenantCommand): |
24 | 38 |
def add_arguments(self, parser): |
25 | 39 |
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN') |
... | ... | |
28 | 42 |
if not options['domain']: |
29 | 43 |
raise CommandError('missing hostname') |
30 | 44 |
pub = self.init_tenant_publisher(options['domain'], register_tld_names=False) |
31 |
DatabaseClient.runshell_db(conn_params=pub.cfg['postgresql']) |
|
45 |
if django.VERSION < (3, 2): |
|
46 |
DatabaseClient.runshell_db(conn_params=pub.cfg['postgresql']) |
|
47 |
else: |
|
48 |
# signature changed, it has a required parameter in 3.2 |
|
49 |
PsqlDatabaseClient(pub).runshell(()) # noqa pylint: disable=too-many-function-args |
|
32 |
- |