Skip to content

Commit

Permalink
added schemachecker validation function and error class
Browse files Browse the repository at this point in the history
  • Loading branch information
P-T-I committed Oct 10, 2023
1 parent 03d0654 commit a3cdbc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions CveXplore/database/maintenance/DatabaseSchemaChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from CveXplore.database.connection.mongo_db import MongoDBConnection
from CveXplore.errors import DatabaseSchemaError

runPath = os.path.dirname(os.path.realpath(__file__))

Expand All @@ -18,6 +19,33 @@ def __init__(self):

self.logger = logging.getLogger("SchemaChecker")

def validate_schema(self):
if hasattr(self.dbh.connection, "store_schema"):
try:
if (
not self.schema_version["version"]
== list(self.dbh.connection.store_schema.find({}))[0]["version"]
):
if not self.schema_version["rebuild_needed"]:
raise DatabaseSchemaError(
"Database is not on the latest schema version; please update the database!"
)
else:
raise DatabaseSchemaError(
"Database schema is not up to date; please re-populate the database!"
)
else:
return True
except IndexError:
# something went wrong fetching the result from the database; assume re-populate is needed
raise DatabaseSchemaError(
"Database schema is not up to date; please re-populate the database!"
)
else:
raise DatabaseSchemaError(
"Database schema is not up to date; please re-populate the database!"
)

def create_indexes(self):
# hack for db_updater.py to put this class in the posts variable and run the update method
self.logger.info("Updating schema version")
Expand Down
4 changes: 4 additions & 0 deletions CveXplore/errors/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class DatabaseIllegalCollection(DatabaseException):

class UpdateSourceNotFound(DatabaseException):
pass


class DatabaseSchemaError(DatabaseException):
pass

0 comments on commit a3cdbc2

Please sign in to comment.