Skip to content

Commit ab93283

Browse files
committed
Merge branch 'release/3.2.1'
2 parents 296db8b + bd165db commit ab93283

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
# All of these options are optional, so you can remove them if you are happy with the defaults
2222
concurrent_skipping: 'same_content'
2323
skip_after_successful_duplicate: 'true'
24+
paths_ignore: '["**/README.md"]'
2425
pytest:
2526
needs: pre_job
2627
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ python3 -m fastapi_template
3232

3333
One of the coolest features is that this project is extremely small and handy.
3434
You can choose between different databases and even ORMs.
35-
Currently SQLAlchemy1.4 and TortoiseORM are supported.
35+
Currently SQLAlchemy1.4, TortoiseORM and Ormar are supported.
3636

3737
TUI and CLI and excellent code documentation.
3838

@@ -56,7 +56,7 @@ $ python -m fastapi_template --help
5656
usage: FastAPI template [-h] [--version] [--name PROJECT_NAME]
5757
[--description PROJECT_DESCRIPTION]
5858
[--db {none,sqlite,mysql,postgresql}]
59-
[--orm {sqlalchemy,tortoise}]
59+
[--orm {ormar,sqlalchemy,tortoise}]
6060
[--ci {none,gitlab,github}] [--redis] [--migrations]
6161
[--kube] [--dummy] [--routers] [--swagger] [--force]
6262

@@ -68,7 +68,7 @@ optional arguments:
6868
Project description
6969
--db {none,sqlite,mysql,postgresql}
7070
Database
71-
--orm {sqlalchemy,tortoise}
71+
--orm {ormar,sqlalchemy,tortoise}
7272
ORM
7373
--ci {none,gitlab,github}
7474
Choose CI support

fastapi_template/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def parse_args():
5656
"--orm",
5757
help="ORM",
5858
type=str,
59-
choices=list(map(attrgetter("value"), ORM)),
59+
choices=[orm.value for orm in ORM if orm != ORM.none],
6060
default=None,
6161
dest="orm",
6262
)
@@ -65,7 +65,7 @@ def parse_args():
6565
help="Choose CI support",
6666
default=None,
6767
type=str,
68-
choices=list(map(attrgetter("value"), CIType)),
68+
choices=[ci.value for ci in CIType],
6969
dest="ci_type",
7070
)
7171
parser.add_argument(
@@ -192,7 +192,7 @@ def read_user_input(current_context: BuilderContext) -> BuilderContext:
192192
current_context.orm = radiolist_dialog(
193193
"ORM",
194194
text="Which ORM do you want?",
195-
values=[(orm, orm.value) for orm in list(ORM)],
195+
values=[(orm, orm.value) for orm in list(ORM) if orm != ORM.none],
196196
).run()
197197
if current_context.orm is None:
198198
raise KeyboardInterrupt()

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 %}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi_template"
3-
version = "3.2.0"
3+
version = "3.2.1"
44
description = "Feature-rich robust FastAPI template"
55
authors = ["Pavel Kirilin <[email protected]>"]
66
packages = [{ include = "fastapi_template" }]

0 commit comments

Comments
 (0)