-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ExpressApp/feature/sqlalchemy
Feature/sqlalchemy
- Loading branch information
Showing
32 changed files
with
498 additions
and
874 deletions.
There are no files selected for viewing
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,3 +1,4 @@ | ||
"""Module as an executable magic file.""" | ||
from boxv2.main import main | ||
|
||
main() |
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
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
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,3 @@ | ||
from .plugin import SQLAlchemyPlugin as Plugin | ||
|
||
__all__ = ["Plugin"] |
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,62 @@ | ||
"""Postgresql database plugin.""" | ||
|
||
from typing import Any, Optional | ||
|
||
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from boxv2.plugin import BasePlugin, BasePluginSettings, HealtCheckData | ||
from boxv2.plugins.sqlalchemy.url_scheme_utils import make_url_async | ||
|
||
|
||
class Settings(BasePluginSettings): | ||
"""Settings for SQLAlchemy ORM plugin.""" | ||
|
||
POSTGRES_DSN: str | ||
SQL_DEBUG: bool = False | ||
|
||
|
||
class SQLAlchemyPlugin(BasePlugin): | ||
"""SQLAlchemy ORM plugin.""" | ||
|
||
settings_class = Settings | ||
_session: Optional[AsyncSession] | ||
engine: Optional[AsyncEngine] | ||
|
||
async def on_startup(self, *args: Any, **kwargs: Any) -> None: | ||
"""Startup hook.""" | ||
self.engine = create_async_engine( | ||
make_url_async(self.settings.POSTGRES_DSN), echo=self.settings.SQL_DEBUG | ||
) | ||
|
||
make_session = sessionmaker( | ||
self.engine, expire_on_commit=False, class_=AsyncSession | ||
) | ||
self._session = make_session() | ||
|
||
async def on_shutdown(self, *args: Any, **kwargs: Any) -> None: | ||
"""Shutdown hook.""" | ||
if self.session is not None: | ||
await self.session.close() | ||
|
||
async def healthcheck(self) -> HealtCheckData: | ||
"""Healthcheck function.""" | ||
try: | ||
async with self.session.begin(): | ||
rows = await self.session.execute("select version()") | ||
except Exception as exc: | ||
return HealtCheckData(healthy=False, information={"error": str(exc)}) | ||
return HealtCheckData( | ||
healthy=True, | ||
info={ | ||
"server_version": rows.scalars().one(), | ||
"dsn": self.settings.POSTGRES_DSN, | ||
}, | ||
) | ||
|
||
@property | ||
def session(self) -> AsyncSession: | ||
"""Return an SQLAlchemy session instance.""" | ||
if self._session is None: | ||
raise RuntimeError("Plugin not yet initialized!") | ||
return self._session |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ate/{{cookiecutter.bot_name}}/alembic.ini → ...ate/{{cookiecutter.bot_name}}/alembic.ini
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
File renamed without changes.
Empty file.
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
File renamed without changes.
Empty file.
31 changes: 31 additions & 0 deletions
31
boxv2/plugins/sqlalchemy/template/{{cookiecutter.bot_name}}/app/db/models.py
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,31 @@ | ||
"""Database models declarations.""" | ||
|
||
from sqlalchemy import Column, Integer, String | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
# All models in project must be inherited from this class | ||
Base = declarative_base() | ||
|
||
|
||
# This is an example model. You may rewrite it a you need or write any other models. | ||
# After it you may run `alembic revision --autogenerate` to generate migrations | ||
# and `alembic upgrade head` to apply it on database. | ||
# Migrations files will be stored at `app/db/migrations/versions` | ||
class Record(Base): | ||
"""Simple database model for example.""" | ||
|
||
__tablename__ = "record" | ||
|
||
id: int = Column(Integer, primary_key=True, autoincrement=True) # noqa: WPS125 | ||
record_data: str = Column(String) | ||
|
||
def __repr__(self) -> str: | ||
"""Show string representation of record.""" | ||
return self.record_data | ||
|
||
|
||
def get_session() -> AsyncSession: | ||
from app.main import app # should not be imported before app initialization | ||
|
||
return app.state.sqlalchemy.session |
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,11 @@ | ||
"""Utility functions for sqlalchemy plugin.""" | ||
|
||
|
||
def make_url_async(url: str) -> str: | ||
"""Add +asyncpg to url scheme.""" | ||
return "postgresql+asyncpg" + url[url.find(":") :] # noqa: WPS336 | ||
|
||
|
||
def make_url_sync(url: str) -> str: | ||
"""Remove +asyncpg from url scheme.""" | ||
return "postgresql" + url[url.find(":") :] # noqa: WPS336 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.