Skip to content

Commit

Permalink
Update server
Browse files Browse the repository at this point in the history
  • Loading branch information
slimovich committed Jul 1, 2020
1 parent 3209c51 commit f6b2d06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

from src.api.api import api_router
from src.core.config import (
DB_NAME,
LOGGING_CONFIG,
SERVER_ADRESS,
SERVER_LOG_LEVEL,
SERVER_PORT,
)
from src.core.db import db
from src.utils.sql import existing_database

LOGGER = logging.getLogger(__name__)

Expand All @@ -31,12 +33,13 @@ def create_app() -> FastAPI:

@app.on_event("startup")
async def _startup() -> None:
try:
LOGGER.info("Create tables")
# await db.gino.drop_all()
await db.gino.create_all()
except Exception as e:
LOGGER.error(f"Error in startup for tables creation => {e}")
LOGGER.info("Check existing database")
database: bool = await existing_database(db, DB_NAME)

if not database:
LOGGER.error(f"please create the required database before running the server db_name = {DB_NAME}")
else:
LOGGER.info("database checked")


def run() -> None:
Expand Down
14 changes: 14 additions & 0 deletions src/utils/sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


async def existing_database(db, name) -> bool:
""" Return the names of existing database """

query = """
SELECT datname
FROM pg_database;
"""
raws = await db.all(db.text(query))
for raw in raws:
if raw[0] == name:
return True
return False

0 comments on commit f6b2d06

Please sign in to comment.