-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update sqlalchemy to use standard api instead of async one, sta…
…ndard has better documentation, more community support, and cleaner code
- Loading branch information
1 parent
4a8db27
commit 267bb53
Showing
17 changed files
with
240 additions
and
290 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import Set | ||
from urllib import parse | ||
|
||
from pydantic import field_validator, PostgresDsn, ValidationInfo | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
from regtech_api_commons.oauth2.config import KeycloakSettings | ||
from regtech_regex.regex_config import RegexConfigs | ||
|
||
|
||
JWT_OPTS_PREFIX = "jwt_opts_" | ||
|
||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
env_files_to_load: list[Path | str] = [f"{dir_path}/.env"] | ||
if os.getenv("ENV", "LOCAL") == "LOCAL": | ||
env_files_to_load.append(f"{dir_path}/.env.local") | ||
|
||
|
||
class Settings(BaseSettings): | ||
inst_db_schema: str = "public" | ||
inst_db_name: str | ||
inst_db_user: str | ||
inst_db_pwd: str | ||
inst_db_host: str | ||
inst_db_scheme: str = "postgresql+psycopg2" | ||
# inst_db_scheme: str = "postgresql+asyncpg" | ||
inst_conn: str | None = None | ||
admin_scopes: Set[str] = set(["query-groups", "manage-users"]) | ||
db_logging: bool = True | ||
|
||
def __init__(self, **data): | ||
super().__init__(**data) | ||
|
||
@field_validator("inst_conn", mode="before") | ||
@classmethod | ||
def build_postgres_dsn(cls, field_value, info: ValidationInfo) -> str: | ||
postgres_dsn = PostgresDsn.build( | ||
scheme=info.data.get("inst_db_scheme"), | ||
username=info.data.get("inst_db_user"), | ||
password=parse.quote(str(info.data.get("inst_db_pwd")), safe=""), | ||
host=info.data.get("inst_db_host"), | ||
path=info.data.get("inst_db_name"), | ||
) | ||
return postgres_dsn.unicode_string() | ||
|
||
model_config = SettingsConfigDict(env_file=env_files_to_load, extra="allow") | ||
|
||
|
||
settings = Settings() | ||
|
||
kc_settings = KeycloakSettings(_env_file=env_files_to_load) | ||
|
||
regex_configs = RegexConfigs.instance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
from sqlalchemy.ext.asyncio import ( | ||
create_async_engine, | ||
async_sessionmaker, | ||
async_scoped_session, | ||
) | ||
from asyncio import current_task | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import scoped_session, sessionmaker | ||
from regtech_user_fi_management.config import settings | ||
|
||
engine = create_async_engine(str(settings.inst_conn), echo=settings.db_logging).execution_options( | ||
engine = create_engine(str(settings.inst_conn), echo=settings.db_logging).execution_options( | ||
schema_translate_map={None: settings.inst_db_schema} | ||
) | ||
SessionLocal = async_scoped_session(async_sessionmaker(engine, expire_on_commit=False), current_task) | ||
SessionLocal = scoped_session(sessionmaker(engine, expire_on_commit=False)) | ||
|
||
|
||
async def get_session(): | ||
def get_session(): | ||
session = SessionLocal() | ||
try: | ||
yield session | ||
finally: | ||
await session.close() | ||
session.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.