Skip to content

Commit

Permalink
Fixed reset_db with psycopg3 (same patch like for drop_test_database) (
Browse files Browse the repository at this point in the history
…#1821)

* Fixed reset_db with psycopg3 (same patch like for drop_test_database)

* Fixed tests

---------

Co-authored-by: Jann Haber <jann.haber@selfnet.de>
  • Loading branch information
jannh and Jann Haber committed Jun 5, 2023
1 parent 8c7fae1 commit de7dfac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions django_extensions/management/commands/reset_db.py
Expand Up @@ -148,7 +148,7 @@ def handle(self, *args, **options):
else:
import psycopg2 as Database # NOQA

conn_params = {'database': 'template1'}
conn_params = {'dbname': 'template1'}
if user:
conn_params['user'] = user
if password:
Expand All @@ -159,7 +159,10 @@ def handle(self, *args, **options):
conn_params['port'] = database_port

connection = Database.connect(**conn_params)
connection.set_isolation_level(0) # autocommit false
if has_psycopg3:
connection.autocommit = True
else:
connection.set_isolation_level(0) # autocommit false
cursor = connection.cursor()

if options['close_sessions']:
Expand Down
4 changes: 2 additions & 2 deletions tests/management/commands/test_reset_db.py
Expand Up @@ -174,7 +174,7 @@ def test_should_drop_and_create_database_and_print_success_messsage(self, m_stdo
with mock.patch.dict("sys.modules", **mock_kwargs):
call_command('reset_db', '--noinput', verbosity=2)

m_database.connect.assert_called_once_with(database='template1', host='127.0.0.1', password='bar', port='5432', user='foo')
m_database.connect.assert_called_once_with(dbname='template1', host='127.0.0.1', password='bar', port='5432', user='foo')

m_cursor.execute.assert_has_calls(expected_calls, any_order=False)
self.assertEqual("Reset successful.\n", m_stdout.getvalue())
Expand All @@ -198,7 +198,7 @@ def test_should_drop_create_database_close_sessions_and_print_success_messsage(s
with mock.patch.dict("sys.modules", **mock_kwargs):
call_command('reset_db', '--noinput', '--close-sessions', verbosity=2)

m_database.connect.assert_called_once_with(database='template1', host='127.0.0.1', password='bar', port='5432', user='foo')
m_database.connect.assert_called_once_with(dbname='template1', host='127.0.0.1', password='bar', port='5432', user='foo')

m_cursor.execute.assert_has_calls(expected_calls, any_order=False)
self.assertEqual("Reset successful.\n", m_stdout.getvalue())

0 comments on commit de7dfac

Please sign in to comment.