Skip to content

Commit

Permalink
feat!(db): initial work towards #9
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobCoffee committed Nov 26, 2023
1 parent 96bcf7e commit 5e9aee5
Show file tree
Hide file tree
Showing 33 changed files with 1,107 additions and 90 deletions.
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ repos:
typing-extensions,
"uvicorn[standard]",
uvloop>=0.18.0,
litestar,
asyncpg,
"litestar[cli,structlog,standard]",
types-click,
asyncpg-stubs,
polyfactory,
discord-py,
]
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ docs: docs-clean ## Dump the existing built docs and rebuild them
@echo "=> Building documentation"
@$(PDM_RUN_BIN) sphinx-build -M html docs docs/_build/ -E -a -j auto --keep-going

# =============================================================================
# Database
# =============================================================================
migrations: ## Generate database migrations
@echo "ATTENTION: This operation will create a new database migration for any defined models changes."
@while [ -z "$$MIGRATION_MESSAGE" ]; do read -r -p "Migration message: " MIGRATION_MESSAGE; done ;
@$(ENV_PREFIX)app database make-migrations --autogenerate -m "$${MIGRATION_MESSAGE}"

.PHONY: migrate
migrate: ## Apply database migrations
@echo "ATTENTION: Will apply all database migrations."
@$(ENV_PREFIX)app database upgrade

# =============================================================================
# Main
# =============================================================================
Expand Down
35 changes: 35 additions & 0 deletions docker-compose.infra.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: "3.9"

services:
db:
image: postgres:latest
ports:
- "5432:5432"
hostname: db
environment:
POSTGRES_USER: "byte"
POSTGRES_PASSWORD: "bot"
POSTGRES_DB: "byte"

volumes:
- db-data:/var/lib/postgresql/data
restart: unless-stopped

logging:
options:
max-size: 10m
max-file: "3"

healthcheck:
test:
- CMD
- pg_isready
- -U
- app
interval: 2s
timeout: 3s
retries: 40

volumes:
db-data: {}
cache-data: {}
13 changes: 13 additions & 0 deletions docs/web/api/domain/db/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
========
Database
========

This is the database layer serving all domains in the application.

.. toctree::
:titlesonly:
:caption: Database Domain API Reference
:glob:
:hidden:

*
16 changes: 16 additions & 0 deletions docs/web/api/domain/db/models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
======
models
======

Model definition for the domain.

API Reference
-------------

.. automodule:: src.server.domain.db.models
:members:

Mermaid Diagram
---------------

.. autoclasstree:: src.server.domain.db.models
2 changes: 2 additions & 0 deletions docs/web/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ API Reference
domain/*
domain/web/index
domain/system/index
domain/db/index

.. toctree::
:titlesonly:
Expand All @@ -31,3 +32,4 @@ API Reference

lib/*
lib/log/index
lib/db/index
9 changes: 9 additions & 0 deletions docs/web/api/lib/db/base.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
====
base
====

API Reference
-------------

.. automodule:: src.server.lib.db.base
:members:
13 changes: 13 additions & 0 deletions docs/web/api/lib/db/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
================
Database Library
================

Documentation for the Database library.


.. toctree::
:titlesonly:

base
utils
orm
9 changes: 9 additions & 0 deletions docs/web/api/lib/db/orm.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
===
orm
===

API Reference
-------------

.. automodule:: src.server.lib.db.orm
:members:
9 changes: 9 additions & 0 deletions docs/web/api/lib/db/utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=====
utils
=====

API Reference
-------------

.. automodule:: src.server.lib.db.utils
:members:
38 changes: 33 additions & 5 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ dependencies = [
"litestar[jwt,opentelemetry,prometheus,standard,structlog]>=2.0.0b4",
"pydantic-settings>=2.1.0",
"anyio>=3.7.1",
"advanced-alchemy>=0.5.4",
"advanced-alchemy>=0.5.5",
"certifi>=2023.11.17",
"asyncpg>=0.29.0",
]
requires-python = ">=3.11,<4.0"
readme = "README.md"
Expand Down Expand Up @@ -97,7 +98,7 @@ lint = [
]

[project.scripts]
app = "litestar.__main__:run_cli"
app = "src.__main__:run_cli"

[project.entry-points."litestar.commands"]
run-bot = "src.cli:run_bot"
Expand Down
30 changes: 30 additions & 0 deletions src/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Application Entrypoint."""
from __future__ import annotations

__all__ = ("run_cli",)


def run_cli() -> None:
"""Application Entrypoint."""
import os
import sys
from pathlib import Path

current_path = Path(__file__).parent.parent.resolve()
sys.path.append(str(current_path))
os.environ.setdefault("LITESTAR_APP", "src.app:app")
try:
from litestar.__main__ import run_cli as run_litestar_cli

except ImportError as exc:
print( # noqa: T201
"Could not load required libraries. ",
"Please check your installation and make sure you activated any necessary virtual environment",
)
print(exc) # noqa: T201
sys.exit(1)
run_litestar_cli()


if __name__ == "__main__":
run_cli()
11 changes: 9 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from typing import TYPE_CHECKING

from src.server.lib import db

if TYPE_CHECKING:
from litestar import Litestar

Expand All @@ -15,6 +17,7 @@ def create_app() -> Litestar:
Returns:
Litestar: The Litestar application.
"""
from advanced_alchemy.exceptions import RepositoryError
from litestar import Litestar
from pydantic import SecretStr

Expand All @@ -33,6 +36,7 @@ def create_app() -> Litestar:
# Handlers
exception_handlers={
exceptions.ApplicationError: exceptions.exception_to_http_response,
RepositoryError: exceptions.exception_to_http_response,
},
route_handlers=[*domain.routes],
# Configs
Expand All @@ -51,10 +55,13 @@ def create_app() -> Litestar:
middleware=[log.controller.middleware_factory],
signature_namespace=domain.signature_namespace,
type_encoders={SecretStr: str},
plugins=[],
plugins=[db.plugin],
)


def create_bot() -> None:
"""Application factory to instantiate a Discord bot."""
"""Application factory to instantiate a Discord bot.
.. todo:: Move into this file.
"""
...
1 change: 0 additions & 1 deletion src/database/__init__.py

This file was deleted.

60 changes: 0 additions & 60 deletions src/database/models.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/server/domain/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Domain for database models."""
from src.server.domain.db import models

__all__ = ["models"]
Loading

0 comments on commit 5e9aee5

Please sign in to comment.