Switch to psycopg rather than psycopg2
It's a bit more modern and uses dedicated c bindings
This commit is contained in:
parent
1e22c2895a
commit
28d5c6e606
@ -1,4 +1,4 @@
|
||||
sudo zypper in python3-psycopg2
|
||||
sudo zypper in python3-psycopg
|
||||
sudo su - postgres
|
||||
# `createdb -O <LOCAL_USER> imported_git`
|
||||
|
||||
|
18
lib/db.py
18
lib/db.py
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user