Skip to content

Commit 5f34582

Browse files
committed
feat: add database schema support and update dependencies
1 parent 5ba972b commit 5f34582

File tree

9 files changed

+53
-28
lines changed

9 files changed

+53
-28
lines changed

.trunk/trunk.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ runtimes:
1717
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
1818
lint:
1919
disabled:
20-
- git-diff-check
2120
- bandit
21+
- git-diff-check
2222
enabled:
2323
2424
2525
26-
26+
2727
2828
29-
- prettier@3.4.2
29+
- prettier@3.5.0
3030
3131
3232

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
# my personal python template playground
2+
13
Simple python template I am experimenting with around a set of overlapping concepts with a Fastapi implementation:
2-
* DDD
3-
* Clean architecture
4-
* Ports/Adapters
5-
* Vertical slice
64

7-
``` bash
8-
dotenv run fastapi dev main.py
9-
dotenv run pytest
5+
- DDD
6+
- Clean architecture
7+
- Ports/Adapters
8+
- Vertical slice
9+
10+
```bash
11+
uv run fastapi dev main.py
12+
uv run pytest
13+
14+
atlas schema inspect -u "sqlite://issues.db" --format "{{ sql . }}" > migrate.sql
1015
```
1116

12-
``` mermaid
17+
```mermaid
1318
graph TD;
1419
analyze_endpoint["/issues/{issue_number}/analyze (POST)"] -->|Depends| configure_repository
1520
analyze_endpoint -->|Depends| configure_unit_of_work
@@ -35,4 +40,4 @@ graph TD;
3540
UnitOfWork_rollback -.-> UnitOfWorkProtocol
3641
UnitOfWork_enter -.-> UnitOfWorkProtocol
3742
UnitOfWork_exit -.-> UnitOfWorkProtocol
38-
```
43+
```

app/domain/aggregate_root.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from sqlmodel import SQLModel
66

7+
from config import settings
8+
79

810
class BaseCommand(Protocol):
911
command_id: str
@@ -19,6 +21,9 @@ class BaseEvent(Protocol):
1921

2022

2123
class AggregateRoot(SQLModel, abc.ABC):
24+
# This sets the database schema in which to create tables for all subclasses of BaseModel
25+
# __tablename__ = "sometable"
26+
__table_args__ = {"schema": settings.database_schema}
2227
version: int = 0
2328

2429
# or handle()

app/resource_adapters/persistence/sqlmodel/database.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_engine(database_url: str | None = None) -> Engine:
3838
_engine = create_engine(database_url, **engine_args)
3939

4040
# Enable WAL mode if configured
41-
if settings.sqlite_wal_mode and database_url.startswith("sqlite"):
41+
if settings.sqlite_wal_mode:
4242
with _engine.connect() as conn:
4343
# https://www.sqlite.org/pragma.html
4444
conn.execute(text("PRAGMA journal_mode=WAL"))
@@ -48,6 +48,14 @@ def get_engine(database_url: str | None = None) -> Engine:
4848
# Initialize database if using SQLModel
4949
if settings.model_config == "sqlmodel" and not settings.migrate_database:
5050
logger.info("Creating database tables...")
51+
52+
if settings.database_schema:
53+
SQLModel.metadata.schema = settings.database_schema
54+
# if not conn.dialect.has_schema(conn, db_schema):
55+
# logger.warning(f"Schema '{db_schema}' not found in database. Creating...")
56+
# conn.execute(sa.schema.CreateSchema(db_schema))
57+
# conn.commit()
58+
5159
SQLModel.metadata.create_all(_engine)
5260
logger.info("Database tables created successfully")
5361

app/tests/conftest.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ async def test_lifespan(app: FastAPI):
4949
app = create_app(lifespan_handler=test_lifespan)
5050

5151

52-
@pytest.fixture(name="session", autouse=True)
53-
@pytest.mark.env("testing")
52+
@pytest.fixture(name="session")
5453
def test_session():
5554
"""Session fixture for testing environment using test database."""
5655
with Session(get_engine()) as session:
@@ -60,16 +59,6 @@ def test_session():
6059
session.commit()
6160

6261

63-
@pytest.fixture(name="session", autouse=True)
64-
@pytest.mark.env("development")
65-
def dev_session():
66-
"""Session fixture for development environment."""
67-
with Session(get_engine()) as session:
68-
yield session
69-
# In development, we might want to keep the data
70-
# or handle cleanup differently
71-
72-
7362
@pytest.fixture(name="client")
7463
def client_fixture():
7564
with TestClient(app) as client:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ authors = [{ name = "Chris Markus", email = "[email protected]" }]
66
requires-python = "~=3.12"
77
readme = "README.md"
88
dependencies = [
9+
"atlas-provider-sqlalchemy>=0.2.4",
910
"dependency-injector>=4.45.0",
1011
"dynaconf>=3.2.7",
1112
"fastapi[standard]>=0.115.6",

settings.test.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
[default]
33
# sqlite:///./test.db sqlite://
44
database_url = "sqlite://"
5+
database_schema = ""
56
# in-memory, sqlmodel
67
model_config = "sqlmodel"
7-
env_smoke_test = "configured"
8-
backend_cors_origins = ["http://testserver"]
98
migrate_database = false
109
sqlite_wal_mode = false
10+
env_smoke_test = "configured"
11+
backend_cors_origins = ["http://testserver"]
1112

1213
# You can still use environment-specific overrides in test settings
1314
[development]

settings.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[default]
22
project_name = "python-template"
3+
# dialect+driver://username:password@host:port/database
34
database_url = "sqlite:///./issues.db"
5+
database_schema = ""
46
migrate_database = false
7+
sqlite_wal_mode = false
58
# in-memory, sqlmodel
69
model_config = "sqlmodel"
710
env_smoke_test = ""
8-
sqlite_wal_mode = false
911

1012
# CORS Settings
1113
backend_cors_origins = []

uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)