fixed formatting and other suggestions

This commit is contained in:
2022-10-26 11:58:01 +02:00
parent f14c609db2
commit 1656d73340
7 changed files with 205 additions and 229 deletions

View File

@@ -22,20 +22,19 @@ class DB:
def schema_version(self):
# create a cursor
cur = self.conn.cursor()
with self.conn.cursor() as cur:
# execute a statement
try:
cur.execute("SELECT MAX(version) from scheme")
except psycopg2.errors.UndefinedTable as error:
cur.close()
self.close()
self.connect()
return 0
# execute a statement
try:
cur.execute("SELECT MAX(version) from scheme")
except psycopg2.errors.UndefinedTable as error:
cur.close()
self.close()
self.connect()
return 0
db_version = cur.fetchone()
db_version = cur.fetchone()
cur.close()
return db_version[0]
def close(self):
@@ -170,15 +169,13 @@ class DB:
if (schema_version + 1) not in schemes:
return
try:
cur = self.conn.cursor()
# create table one by one
for version, commands in schemes.items():
if version <= schema_version:
continue
for command in commands:
cur.execute(command)
# close communication with the PostgreSQL database server
cur.close()
with self.conn.cursor() as cur:
# create table one by one
for version, commands in schemes.items():
if version <= schema_version:
continue
for command in commands:
cur.execute(command)
# commit the changes
self.conn.commit()
except (Exception, psycopg2.DatabaseError) as error: