Skip to content

Commit

Permalink
Add SQLAlchemy Helpers integration
Browse files Browse the repository at this point in the history
Signed-off-by: Akashdeep Dhar <[email protected]>
  • Loading branch information
gridhead committed Jul 2, 2024
1 parent 00fde81 commit 0396893
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
19 changes: 3 additions & 16 deletions webhook_to_fedora_messaging/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
11 changes: 3 additions & 8 deletions webhook_to_fedora_messaging/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
4 changes: 3 additions & 1 deletion webhook_to_fedora_messaging/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0396893

Please sign in to comment.