Skip to content

Commit bd165db

Browse files
authored
Fixed database initialization. (#43)
Description: * Added function to populate tables on startup for sqlalchemy and ormar. * Added tortoise startup parameter. * Fixed tortoise config. * Fixed k8s config files. Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 1ad18d5 commit bd165db

5 files changed

Lines changed: 53 additions & 7 deletions

File tree

fastapi_template/template/{{cookiecutter.project_name}}/deploy/kube/app.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ spec:
2828
args:
2929
- -c
3030
- >-
31+
{%- if cookiecutter.enable_migrations == "True" %}
3132
{%- if cookiecutter.orm in ['sqlalchemy', 'ormar'] %}
3233
alembic upgrade head &&
3334
{%- elif cookiecutter.orm == 'tortoise' %}
3435
aerich upgrade &&
3536
{%- endif %}
37+
{%- endif %}
3638
python -m {{cookiecutter.project_name }}
3739
{%- endif %}
3840
env:
3941
- name: {{cookiecutter.project_name | upper }}_HOST
4042
value: "0.0.0.0"
4143
- name: {{cookiecutter.project_name | upper }}_WORKERS_COUNT
4244
value: "10"
43-
{%- if cookiecutter.db_info.name != "none" %}
44-
{%- if cookiecutter.db_info.name != "sqlite" %}
45+
{%- if cookiecutter.db_info.name not in ["none", "sqlite"] %}
4546
- name: {{cookiecutter.project_name | upper }}_DB_HOST
4647
value: "{{cookiecutter.kube_name}}-db-service"
4748
{%- endif %}
48-
{%- endif %}
4949
{%- if cookiecutter.enable_redis == 'True' %}
5050
- name: {{cookiecutter.project_name | upper }}_REDIS_HOST
5151
value: "{{cookiecutter.kube_name}}-redis-service"

fastapi_template/template/{{cookiecutter.project_name}}/deploy/kube/db.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ spec:
5353
- port: {{cookiecutter.db_info.port}}
5454
targetPort: {{cookiecutter.db_info.port}}
5555
---
56+
{%- if cookiecutter.enable_migrations == "True" %}
5657
apiVersion: batch/v1
5758
kind: Job
5859
metadata:
@@ -87,5 +88,5 @@ spec:
8788
command: ["./wait-for-it.sh", "-t", "60", "{{cookiecutter.kube_name}}-db-service:{{cookiecutter.db_info.port}}"]
8889
restartPolicy: Never
8990

90-
9191
---
92+
{%- endif %}

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_tortoise/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"apps": {
1111
"models": {
12-
"models": ["aerich.models"] + MODELS_MODULES,
12+
"models": MODELS_MODULES {%- if cookiecutter.enable_migrations == "True" %} + ["aerich.models"] {%- endif %} ,
1313
"default_connection": "default",
1414
},
1515
},

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/application.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ def get_app() -> FastAPI:
5757
{% endif %}
5858

5959
{%- if cookiecutter.orm == 'tortoise' %}
60-
register_tortoise(app, config=TORTOISE_CONFIG, add_exception_handlers=True)
60+
register_tortoise(
61+
app,
62+
config=TORTOISE_CONFIG,
63+
add_exception_handlers=True,
64+
{%- if cookiecutter.enable_migrations == "False" %}
65+
generate_schemas=True,
66+
{%- endif %}
67+
)
6168
{%- endif %}
6269

6370
return app

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
{%- if cookiecutter.orm == "ormar" %}
1212
from {{cookiecutter.project_name}}.db.config import database
13+
{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
14+
from sqlalchemy.engine import create_engine
15+
from {{cookiecutter.project_name}}.db.meta import meta
16+
from {{cookiecutter.project_name}}.db.models import load_all_models
17+
{%- endif %}
1318
{%- endif %}
1419

1520
{%- if cookiecutter.orm == "sqlalchemy" %}
@@ -21,6 +26,11 @@
2126
)
2227
from sqlalchemy.orm import sessionmaker
2328

29+
{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
30+
from {{cookiecutter.project_name}}.db.meta import meta
31+
from {{cookiecutter.project_name}}.db.models import load_all_models
32+
{%- endif %}
33+
2434

2535
def _setup_db(app: FastAPI) -> None:
2636
"""
@@ -47,11 +57,34 @@ def _setup_db(app: FastAPI) -> None:
4757

4858
{% if cookiecutter.enable_redis == "True" %}
4959
def _setup_redis(app: FastAPI) -> None:
60+
"""
61+
Initialize redis connection.
62+
63+
:param app: current FastAPI app.
64+
"""
5065
app.state.redis_pool = aioredis.ConnectionPool.from_url(
5166
str(settings.redis_url),
5267
)
5368
{%- endif %}
5469

70+
{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
71+
{%- if cookiecutter.orm in ["ormar", "sqlalchemy"] %}
72+
async def _create_tables() -> None:
73+
"""Populates tables in the database."""
74+
load_all_models()
75+
{%- if cookiecutter.orm == "ormar" %}
76+
engine = create_engine(str(settings.db_url))
77+
with engine.connect() as connection:
78+
meta.create_all(connection)
79+
engine.dispose()
80+
{%- elif cookiecutter.orm == "sqlalchemy" %}
81+
engine = create_async_engine(str(settings.db_url))
82+
async with engine.begin() as connection:
83+
await connection.run_sync(meta.create_all)
84+
await engine.dispose()
85+
{%- endif %}
86+
{%- endif %}
87+
{%- endif %}
5588

5689
def startup(app: FastAPI) -> Callable[[], Awaitable[None]]:
5790
"""
@@ -67,9 +100,14 @@ def startup(app: FastAPI) -> Callable[[], Awaitable[None]]:
67100
async def _startup() -> None: # noqa: WPS430
68101
{%- if cookiecutter.orm == "sqlalchemy" %}
69102
_setup_db(app)
70-
{% elif cookiecutter.orm == "ormar" %}
103+
{%- elif cookiecutter.orm == "ormar" %}
71104
await database.connect()
72105
{%- endif %}
106+
{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
107+
{%- if cookiecutter.orm in ["ormar", "sqlalchemy"] %}
108+
await _create_tables()
109+
{%- endif %}
110+
{%- endif %}
73111
{%- if cookiecutter.enable_redis == "True" %}
74112
_setup_redis(app)
75113
{%- endif %}

0 commit comments

Comments
 (0)