8000 Fixed reset_db with psycopg3 (same patch like for drop_test_database) by jannh · Pull Request #1821 · django-extensions/django-extensions · GitHub
[go: up one dir, main page]

Skip to content

Fixed reset_db with psycopg3 (same patch like for drop_test_database) #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions django_extensions/management/commands/reset_db.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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