Switch to psycopg rather than psycopg2

It's a bit more modern and uses dedicated c bindings
This commit is contained in:
Dirk Müller 2024-05-16 11:18:01 +02:00
parent 1e22c2895a
commit 28d5c6e606
No known key found for this signature in database
2 changed files with 9 additions and 11 deletions

View File

@ -1,4 +1,4 @@
sudo zypper in python3-psycopg2
sudo zypper in python3-psycopg
sudo su - postgres
# `createdb -O <LOCAL_USER> imported_git`

View File

@ -1,7 +1,6 @@
import logging
import psycopg2
from psycopg2.extras import LoggingConnection
import psycopg
from lib.config import config
@ -17,11 +16,10 @@ class DB:
# read the connection parameters
params = config(section=self.config_section)
# connect to the PostgreSQL server
self.conn = psycopg2.connect(connection_factory=LoggingConnection, **params)
logger = logging.getLogger(__name__)
self.conn.initialize(logger)
self.conn = psycopg.connect(conninfo=f"dbname={params['database']}")
logging.getLogger("psycopg.pool").setLevel(logging.INFO)
except (Exception, psycopg2.DatabaseError) as error:
except (Exception, psycopg.DatabaseError) as error:
print(error)
raise error
@ -32,7 +30,7 @@ class DB:
# execute a statement
try:
cur.execute("SELECT MAX(version) from scheme")
except psycopg2.errors.UndefinedTable as error:
except psycopg.errors.UndefinedTable as error:
cur.close()
self.close()
self.connect()
@ -146,9 +144,9 @@ class DB:
)
schemes[10] = (
"ALTER TABLE revisions ADD COLUMN request_id INTEGER",
"""ALTER TABLE revisions
"""ALTER TABLE revisions
ADD CONSTRAINT request_id_foreign_key
FOREIGN KEY (request_id)
FOREIGN KEY (request_id)
REFERENCES requests (id)""",
"UPDATE scheme SET version=10",
)
@ -273,7 +271,7 @@ class DB:
cur.execute(command)
# commit the changes
self.conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
except (Exception, psycopg.DatabaseError) as error:
print(error)
self.close()
raise error