Skip to content

Commit

Permalink
Make models a folder
Browse files Browse the repository at this point in the history
  • Loading branch information
berrydenhartog committed May 10, 2024
1 parent 39d43f7 commit f9204fd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 24 deletions.
16 changes: 16 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ When poetry is done installing all dependencies you can start using the tool.
poetry run python -m uvicorn tad.main:app
```

## Database

We support most SQL database types. You can use the variable `APP_DATABASE_SCHEME` to change the database. The default scheme is sqlite.

If you change the `models` at tad/models of the application you can generate a new migration file
```shell
alembic revision --autogenerate -m "a message"
```

Please make sure you check the auto generated file in tad/migrations/

to upgrade to the latest version of the database schema use
```shell
alembic upgrade head
```

## Building TAD with Containers

Containers allows to package software, make it portable, and isolated. Before you can run a container you first need a container runtime. There are several available, but al lot of users use [docker desktop](https://www.docker.com/products/docker-desktop/).
Expand Down
8 changes: 4 additions & 4 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ script_location = tad/migrations
# black.options = -l 79 REVISION_SCRIPT_FILENAME

# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
# hooks = ruff
# ruff.type = exec
# ruff.executable = %(here)s/.venv/bin/ruff
# ruff.options = --fix REVISION_SCRIPT_FILENAME
hooks = ruff
ruff.type = exec
ruff.executable = ruff
ruff.options = check --fix REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
Expand Down
5 changes: 5 additions & 0 deletions tad/core/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Literal

EnvironmentType = Literal["local", "staging", "production"]
LoggingLevelType = Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
DatabaseSchemaType = Literal["sqlite", "postgresql", "mysql", "oracle"]
9 changes: 5 additions & 4 deletions tad/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
if config.config_file_name is not None:
fileConfig(config.config_file_name)

from tad.models import SQLModel # noqa
# todo(berry): automaticly import all models
from tad.models.hero import Hero # noqa

target_metadata = SQLModel.metadata
target_metadata = [Hero.metadata]


def get_url():
scheme = os.getenv("SQLALCHEMY_SCHEME", "sqlite")

if scheme == "sqlite":
file = os.getenv("SQLITE_FILE", "./database")
return f"{scheme}://{file}"
file = os.getenv("SQLITE_FILE", "/database")
return f"{scheme}:///{file}"

user = os.getenv("APP_DATABASE_USER", "postgres")
password = os.getenv("APP_DATABASE_PASSWORD", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
"""empty message
"""Added hero test table
Revision ID: 99fb931b1324
Revision ID: 17f5b05e9ec3
Revises:
Create Date: 2024-05-02 12:04:56.694709
Create Date: 2024-05-10 13:57:16.989570
"""

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "99fb931b1324"
revision: str = "17f5b05e9ec3"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"hero",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("secret_name", sa.String(), nullable=False),
sa.Column("age", sa.Integer(), nullable=True),
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade() -> None:
pass
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("hero")
# ### end Alembic commands ###
Empty file added tad/models/__init__.py
Empty file.
File renamed without changes.
16 changes: 8 additions & 8 deletions tad/utils/mask.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from typing import Any


class DataMasker:
class Mask:
def __init__(self, mask_value: str = "***MASKED***", mask_keywords: list[str] | None = None):
if mask_keywords is None:
mask_keywords = []
self.mask_value = mask_value

# default keywords to mask
self.keywords: list[str] = ["password", "secret", "database_uri"]
self.keywords: list[str] = ["password", "secret"]
self.keywords.extend(mask_keywords or [])

def mask_data( # noqa: C901
def secrets( # noqa: C901ß
self, data: str | list[Any] | dict[Any, Any] | set[Any]
) -> str | list[Any] | dict[Any, Any] | set[Any]:
if isinstance(data, dict):
masked_dict: dict[str | int, str | int] = {}
masked_dict: dict[Any, Any] = {}
for key, value in data.items():
if isinstance(key, str):
for keyword in self.keywords:
Expand All @@ -29,8 +29,8 @@ def mask_data( # noqa: C901
return masked_dict

elif isinstance(data, list):
masked_list: list[str | int] = []
item: str | int
masked_list: list[Any] = []
item: Any
for item in data:
if isinstance(item, str):
for keyword in self.keywords:
Expand All @@ -44,9 +44,9 @@ def mask_data( # noqa: C901

return masked_list
elif isinstance(data, set):
masked_set: set[str | int] = set()
masked_set: set[Any] = set()

item: str | int
item: Any
for item in data:
if isinstance(item, str):
for keyword in self.keywords:
Expand Down

0 comments on commit f9204fd

Please sign in to comment.