Skip to content

Commit

Permalink
Merge branch 'release/3.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Oct 13, 2021
2 parents 296db8b + bd165db commit ab93283
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: 'same_content'
skip_after_successful_duplicate: 'true'
paths_ignore: '["**/README.md"]'
pytest:
needs: pre_job
if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ python3 -m fastapi_template

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

TUI and CLI and excellent code documentation.

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

Expand All @@ -68,7 +68,7 @@ optional arguments:
Project description
--db {none,sqlite,mysql,postgresql}
Database
--orm {sqlalchemy,tortoise}
--orm {ormar,sqlalchemy,tortoise}
ORM
--ci {none,gitlab,github}
Choose CI support
Expand Down
6 changes: 3 additions & 3 deletions fastapi_template/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def parse_args():
"--orm",
help="ORM",
type=str,
choices=list(map(attrgetter("value"), ORM)),
choices=[orm.value for orm in ORM if orm != ORM.none],
default=None,
dest="orm",
)
Expand All @@ -65,7 +65,7 @@ def parse_args():
help="Choose CI support",
default=None,
type=str,
choices=list(map(attrgetter("value"), CIType)),
choices=[ci.value for ci in CIType],
dest="ci_type",
)
parser.add_argument(
Expand Down Expand Up @@ -192,7 +192,7 @@ def read_user_input(current_context: BuilderContext) -> BuilderContext:
current_context.orm = radiolist_dialog(
"ORM",
text="Which ORM do you want?",
values=[(orm, orm.value) for orm in list(ORM)],
values=[(orm, orm.value) for orm in list(ORM) if orm != ORM.none],
).run()
if current_context.orm is None:
raise KeyboardInterrupt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ spec:
args:
- -c
- >-
{%- if cookiecutter.enable_migrations == "True" %}
{%- if cookiecutter.orm in ['sqlalchemy', 'ormar'] %}
alembic upgrade head &&
{%- elif cookiecutter.orm == 'tortoise' %}
aerich upgrade &&
{%- endif %}
{%- endif %}
python -m {{cookiecutter.project_name }}
{%- endif %}
env:
- name: {{cookiecutter.project_name | upper }}_HOST
value: "0.0.0.0"
- name: {{cookiecutter.project_name | upper }}_WORKERS_COUNT
value: "10"
{%- if cookiecutter.db_info.name != "none" %}
{%- if cookiecutter.db_info.name != "sqlite" %}
{%- if cookiecutter.db_info.name not in ["none", "sqlite"] %}
- name: {{cookiecutter.project_name | upper }}_DB_HOST
value: "{{cookiecutter.kube_name}}-db-service"
{%- endif %}
{%- endif %}
{%- if cookiecutter.enable_redis == 'True' %}
- name: {{cookiecutter.project_name | upper }}_REDIS_HOST
value: "{{cookiecutter.kube_name}}-redis-service"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ spec:
- port: {{cookiecutter.db_info.port}}
targetPort: {{cookiecutter.db_info.port}}
---
{%- if cookiecutter.enable_migrations == "True" %}
apiVersion: batch/v1
kind: Job
metadata:
Expand Down Expand Up @@ -87,5 +88,5 @@ spec:
command: ["./wait-for-it.sh", "-t", "60", "{{cookiecutter.kube_name}}-db-service:{{cookiecutter.db_info.port}}"]
restartPolicy: Never


---
{%- endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"apps": {
"models": {
"models": ["aerich.models"] + MODELS_MODULES,
"models": MODELS_MODULES {%- if cookiecutter.enable_migrations == "True" %} + ["aerich.models"] {%- endif %} ,
"default_connection": "default",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ def get_app() -> FastAPI:
{% endif %}

{%- if cookiecutter.orm == 'tortoise' %}
register_tortoise(app, config=TORTOISE_CONFIG, add_exception_handlers=True)
register_tortoise(
app,
config=TORTOISE_CONFIG,
add_exception_handlers=True,
{%- if cookiecutter.enable_migrations == "False" %}
generate_schemas=True,
{%- endif %}
)
{%- endif %}

return app
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

{%- if cookiecutter.orm == "ormar" %}
from {{cookiecutter.project_name}}.db.config import database
{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
from sqlalchemy.engine import create_engine
from {{cookiecutter.project_name}}.db.meta import meta
from {{cookiecutter.project_name}}.db.models import load_all_models
{%- endif %}
{%- endif %}

{%- if cookiecutter.orm == "sqlalchemy" %}
Expand All @@ -21,6 +26,11 @@
)
from sqlalchemy.orm import sessionmaker

{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
from {{cookiecutter.project_name}}.db.meta import meta
from {{cookiecutter.project_name}}.db.models import load_all_models
{%- endif %}


def _setup_db(app: FastAPI) -> None:
"""
Expand All @@ -47,11 +57,34 @@ def _setup_db(app: FastAPI) -> None:

{% if cookiecutter.enable_redis == "True" %}
def _setup_redis(app: FastAPI) -> None:
"""
Initialize redis connection.
:param app: current FastAPI app.
"""
app.state.redis_pool = aioredis.ConnectionPool.from_url(
str(settings.redis_url),
)
{%- endif %}

{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
{%- if cookiecutter.orm in ["ormar", "sqlalchemy"] %}
async def _create_tables() -> None:
"""Populates tables in the database."""
load_all_models()
{%- if cookiecutter.orm == "ormar" %}
engine = create_engine(str(settings.db_url))
with engine.connect() as connection:
meta.create_all(connection)
engine.dispose()
{%- elif cookiecutter.orm == "sqlalchemy" %}
engine = create_async_engine(str(settings.db_url))
async with engine.begin() as connection:
await connection.run_sync(meta.create_all)
await engine.dispose()
{%- endif %}
{%- endif %}
{%- endif %}

def startup(app: FastAPI) -> Callable[[], Awaitable[None]]:
"""
Expand All @@ -67,9 +100,14 @@ def startup(app: FastAPI) -> Callable[[], Awaitable[None]]:
async def _startup() -> None: # noqa: WPS430
{%- if cookiecutter.orm == "sqlalchemy" %}
_setup_db(app)
{% elif cookiecutter.orm == "ormar" %}
{%- elif cookiecutter.orm == "ormar" %}
await database.connect()
{%- endif %}
{%- if cookiecutter.db_info.name != "none" and cookiecutter.enable_migrations == "False" %}
{%- if cookiecutter.orm in ["ormar", "sqlalchemy"] %}
await _create_tables()
{%- endif %}
{%- endif %}
{%- if cookiecutter.enable_redis == "True" %}
_setup_redis(app)
{%- endif %}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fastapi_template"
version = "3.2.0"
version = "3.2.1"
description = "Feature-rich robust FastAPI template"
authors = ["Pavel Kirilin <[email protected]>"]
packages = [{ include = "fastapi_template" }]
Expand Down

0 comments on commit ab93283

Please sign in to comment.