Fix "create database" failed for postgress

This PR stops using the psycopg2’s connections context manager
because "CREATE DATABASE" command must be run outside any
transaction[1].
[1]: https://www.psycopg.org/docs/usage.html#transactions-control

Co-Authored-By: hungnt1 <sudo.nguyenhung@gmail.com>
Co-Authored-By: Hirotaka Wakabayashi <hiwkby@yahoo.com>

Story: 2010761
Task: 48059
Change-Id: I73ec6c659d8ad7216460055077737429c878df33
This commit is contained in:
wu.chunyang 2023-06-16 11:35:22 +08:00
parent 09fd606163
commit cd11d7677e
2 changed files with 13 additions and 2 deletions

View File

@ -0,0 +1,8 @@
---
fixes:
- |
Fix guest-agent failed to start postgres container due to execution
of the "CREATE DATABASE" statement within the context manager of
psycopg library. See the following for details
`Stroy 2010761 <https://storyboard.openstack.org/#!/story/2010761>`__

View File

@ -770,12 +770,15 @@ class PostgresConnection(object):
def _execute_stmt(self, statement, identifiers, data_values, fetch,
autocommit=False):
cmd = self._bind(statement, identifiers)
with psycopg2.connect(self.connect_str) as connection:
connection.autocommit = autocommit
connection = psycopg2.connect(self.connect_str)
connection.autocommit = autocommit
try:
with connection.cursor() as cursor:
cursor.execute(cmd, data_values)
if fetch:
return cursor.fetchall()
finally:
connection.close()
def _bind(self, statement, identifiers):
if identifiers: