Added Database Schema checks

The database schema is now checked if the correct (and only the correct) tables are present.
dev
peery 3 years ago
parent b9fe65b445
commit 6026e736ba

@ -5,6 +5,9 @@ from psycopg2.errorcodes import UNIQUE_VIOLATION
class DBAdapter:
EXPECTED_TABLES = ['art', 'artist', 'tag', 'topic', 'presence', 'artist_topic', 'art_author',
'art_tag', 'tag_alias', 'tag_implication', 'tag_category']
def __init__(self, user: str, password: str, host: str = "localhost", port: int = 5432, database: str = "artnet"):
self.db = None
self.db_cursor = None
@ -16,6 +19,34 @@ class DBAdapter:
raise ValueError("Invalid DB credentials!")
print("DB connection to {0}:{1}/{2}".format(host, port, database))
if not self.check_tables():
raise ValueError("Invalid DB schema")
def check_tables(self) -> bool:
"""
Check if all the expected tables are present in the connected database.
:return:
"""
self.db_cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
present_tables = self.db_cursor.fetchall()
unknown_tables = []
missing = [x for x in DBAdapter.EXPECTED_TABLES]
for table in present_tables:
table = table[0]
if table not in DBAdapter.EXPECTED_TABLES:
unknown_tables.append(table)
else:
missing.remove(table)
if len(missing) > 0:
print("The following tables are missing from the currently connected database: {0}".format(missing))
return False
if len(unknown_tables) > 0:
print("The following tables are unknown and not expected inside the database: {0}".format(unknown_tables))
return True
def save_image(self, ID: str, title: str, authors: list, path: str, tags: list, link: str, md5_hash: str):
"""
Updates or saves the given image data to the DB

Loading…
Cancel
Save