Skip to content

Commit

Permalink
multi-row inserts are always passed as a single list argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfunley committed Jul 20, 2024
1 parent d1b5a0e commit 4f4740d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pugsql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def transaction(self):

def _execute(self, clause, *multiparams, **params):
if getattr(self._locals, 'session', None):
return self._locals.session.execute(clause, multiparams or params)
if multiparams:
return self._locals.session.execute(clause, *multiparams)
else:
return self._locals.session.execute(clause, params)

if not self.engine:
raise NoConnectionError()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def setUp(self):
self.fixtures.connect(f'postgresql+pg8000://{user_pass}@127.0.0.1')
self.fixtures.setup()

def test_multi_upsert_in_transaction(self):
with self.fixtures.transaction():
self.fixtures.multi_upsert([
{ 'id': 65, 'foo': 'yyy' },
{ 'id': 99, 'foo': '99999' }])

self.assertEqual('yyy', self.fixtures.get_foo(id=65))

def test_multi_upsert(self):
self.fixtures.multi_upsert([
{ 'id': 1, 'foo': 'abcd' },
Expand Down
5 changes: 3 additions & 2 deletions tests/test_pugsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def test_where_in_multiple_parameters(self):

def test_multi_insert(self):
with self.fixtures.transaction() as t:
self.fixtures.insert_user(
self.fixtures.insert_user([
{ 'username': 'joe' },
{ 'username': 'paul' },
{ 'username': 'topper' },
{ 'username': 'mick' })
{ 'username': 'mick' }
])

sr = list(self.fixtures.search_users(username='topper'))
self.assertEqual('topper', sr[0]['username'])
Expand Down

0 comments on commit 4f4740d

Please sign in to comment.