From 03968939fdca055b5e154997fea1da896b631dd9 Mon Sep 17 00:00:00 2001 From: Akashdeep Dhar Date: Tue, 2 Jul 2024 09:55:15 +0530 Subject: [PATCH] Add SQLAlchemy Helpers integration Signed-off-by: Akashdeep Dhar --- webhook_to_fedora_messaging/database.py | 19 +++---------------- webhook_to_fedora_messaging/main.py | 11 +++-------- webhook_to_fedora_messaging/migrations/env.py | 4 +++- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/webhook_to_fedora_messaging/database.py b/webhook_to_fedora_messaging/database.py index e827500..34a5a53 100644 --- a/webhook_to_fedora_messaging/database.py +++ b/webhook_to_fedora_messaging/database.py @@ -8,20 +8,7 @@ Import the functions we will use in the main code and in migrations. """ -from sqlalchemy_helpers import ( # noqa: F401 - Base, - DatabaseManager, - exists_in_db, - get_or_create, - is_sqlite, -) +from sqlalchemy_helpers import Base, get_or_create, update_or_create, is_sqlite, exists_in_db +from sqlalchemy_helpers.flask_ext import DatabaseExtension, get_or_404, first_or_404 -from webhook_to_fedora_messaging.config import get_config - - -db_config = get_config().database.model_dump() -db = DatabaseManager( - str(db_config["sqlalchemy"]["url"]), - str(db_config["alembic"]["migrations_path"]), - engine_args=db_config["sqlalchemy"], -) +db = DatabaseExtension() diff --git a/webhook_to_fedora_messaging/main.py b/webhook_to_fedora_messaging/main.py index daa740f..f5f7834 100644 --- a/webhook_to_fedora_messaging/main.py +++ b/webhook_to_fedora_messaging/main.py @@ -7,17 +7,13 @@ custom configuration file will be inherently taken from the default values """ -import sys from flask import Flask - +from webhook_to_fedora_messaging.database import db from .config import get_config from .config.defaults import LOGGER_CONFIG - from logging.config import dictConfig - from webhook_to_fedora_messaging.exceptions import ConfigError - import logging @@ -39,9 +35,8 @@ def create_app(): except ConfigError as expt: logging.error(f"Exiting - Reason - {str(expt)}") - main.config.from_mapping( - confdata - ) + main.config.from_mapping(confdata) + db.init_app(main) dictConfig(confdata["logsconf"]) return main diff --git a/webhook_to_fedora_messaging/migrations/env.py b/webhook_to_fedora_messaging/migrations/env.py index ab902a6..046a3db 100644 --- a/webhook_to_fedora_messaging/migrations/env.py +++ b/webhook_to_fedora_messaging/migrations/env.py @@ -7,8 +7,10 @@ from alembic import context from sqlalchemy import engine_from_config, pool +from webhook_to_fedora_messaging.main import create_app from webhook_to_fedora_messaging.config import get_config from webhook_to_fedora_messaging.database import Base +from sqlalchemy_helpers.flask_ext import get_url_from_app # this is the Alembic Config object, which provides @@ -42,7 +44,7 @@ def run_migrations_offline() -> None: script output. """ - url = str(get_config().database.sqlalchemy.url) + url = str(get_url_from_app(create_app)) context.configure( url=url, target_metadata=target_metadata,