Skip to content

Commit

Permalink
db/database.py: improve database logs
Browse files Browse the repository at this point in the history
Signed-off-by: Eduard Kaverinskyi <[email protected]>
  • Loading branch information
EduKav1813 committed Jan 18, 2025
1 parent 0088c6a commit 3972f3d
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions whois/data/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, db_url: str = None):
force=True,
)

self.logger.debug(f'Creating engine for "{db_url}"')
self.engine = db.create_engine(db_url)
self.metadata = db.MetaData()
self.connection = None
Expand All @@ -37,23 +38,23 @@ def is_connected(self) -> bool:
return self.connection is not None

def connect(self) -> None:
self.logger.info(f"Connect to the database for {self.db_name}")
self.logger.info(f'Connect to the database for "{self.db_name}"')
self.connection = self.engine.connect()

def disconnect(self) -> None:
self.logger.info(f"Disconnect to the database for {self.db_name}")
self.logger.info(f'Disconnect to the database for "{self.db_name}"')
if not self.connection:
raise RuntimeError("Cannot close database connection - already closed")
self.connection.close()

def create_db(self) -> None:
"""Ensure that the database exists with given schema."""
self.logger.info(f"Create database {self.db_name}")
self.logger.info(f'Create database "{self.db_name}"')
Base.metadata.create_all(self.engine)

def drop(self) -> None:
"""WARNING: Drops the entire database."""
self.logger.warning(f"Drop database {self.db_name}")
self.logger.warning(f'Drop database "{self.db_name}"')
if not self.is_connected:
self.connect()
Base.metadata.drop_all(self.engine)

0 comments on commit 3972f3d

Please sign in to comment.