From 2ce76cd33f613d056ac6198f5fee8c547af3fca0 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Tue, 24 Aug 2021 21:37:24 +0300
Subject: [PATCH] Fixed SQLITE support.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 .../.gitlab-ci.yml                            |  2 +-
 .../deploy/Dockerfile                         | 20 +++++++------------
 .../deploy/docker-compose.yml                 | 17 +++++++++++++++-
 .../{{cookiecutter.project_name}}/settings.py |  4 ++--
 pyproject.toml                                |  2 +-
 5 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml b/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml
index 87f801c..aa218e2 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/.gitlab-ci.yml
@@ -43,7 +43,7 @@ pytest:
     {%- endif %}
   {%- endif %}
   script:
-    - pytest -vv --cov="{{cookiecutter.project_name}}" .
+    - pytest -vv --cov="{{cookiecutter.project_name}}" . --test-alembic
 
 black:
   extends:
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile
index 7e052a8..07662ea 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/Dockerfile
@@ -4,30 +4,24 @@ RUN apt-get update && apt-get install -y \
     wait-for-it \
     && apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN useradd -m {{cookiecutter.project_name}}
-
-USER {{cookiecutter.project_name}}
-
-ENV PATH="${PATH}:/home/{{cookiecutter.project_name}}/.poetry/bin:/home/{{cookiecutter.project_name}}/.local/bin"
+# ENV PATH="${PATH}:/home/{{cookiecutter.project_name}}/.poetry/bin:/home/{{cookiecutter.project_name}}/.local/bin"
 
 RUN pip install poetry==1.1.8
 
 # Installing requirements
 RUN poetry config virtualenvs.create false
 
-COPY pyproject.toml poetry.lock /home/{{cookiecutter.project_name}}/app/
-WORKDIR /home/{{cookiecutter.project_name}}/app/
+COPY pyproject.toml poetry.lock /app/src/
+WORKDIR /app/src
 
 RUN poetry install --no-dev
 
 # Copying actuall application
-COPY . /home/{{cookiecutter.project_name}}/app/src/
-WORKDIR /home/{{cookiecutter.project_name}}/app/src/
+COPY . /app/src/
 RUN pip install --use-feature=in-tree-build  .
 
-USER root
-RUN chown -R {{cookiecutter.project_name}} /home/{{cookiecutter.project_name}}
-RUN chmod -R 700 /home/{{cookiecutter.project_name}}
-USER {{cookiecutter.project_name}}
+{%- if cookiecutter.db_info.name == "sqlite" %}
+VOLUME [ "/db_data" ]
+{%- endif %}
 
 CMD python -m {{cookiecutter.project_name}}
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml
index 27a7ef0..248b1e3 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml
@@ -10,6 +10,13 @@ services:
       - .env
     environment:
       - {{cookiecutter.project_name | upper }}_HOST=0.0.0.0
+      {%- if cookiecutter.db_info.name == "sqlite" %}
+      - {{cookiecutter.project_name | upper }}_DB_FILE=/db_data/db.sqlite3
+      {%- endif %}
+    {%- if cookiecutter.db_info.name == "sqlite" %}
+    volumes:
+      - {{cookiecutter.project_name}}-db-data:/db_data/
+    {%- endif %}
 
   {%- if cookiecutter.db_info.name == "postgresql" %}
   db:
@@ -35,13 +42,21 @@ services:
       - {{cookiecutter.project_name}}-db-data:/bitnami/mysql/data
   {%- endif %}
 
-  {% if cookiecutter.db_info.name != 'none' %}
+  {% if cookiecutter.enable_alembic == 'True' %}
   migrator:
     build:
       context: .
       dockerfile: ./deploy/Dockerfile
     image: {{cookiecutter.project_name}}:latest
+    {%- if cookiecutter.db_info.name == "sqlite" %}
+    command: alembic upgrade head
+    environment:
+      - {{cookiecutter.project_name | upper }}_DB_FILE=/db_data/db.sqlite3
+    volumes:
+      - {{cookiecutter.project_name}}-db-data:/db_data/
+    {%- else %}
     command: wait-for-it {{cookiecutter.project_name}}-db:{{cookiecutter.db_info.port}} -- alembic upgrade head
+    {%- endif %}
   {%- endif %}
 
   {%- if cookiecutter.enable_redis == "True" %}
diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py
index 03e2368..11f8691 100644
--- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py
+++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py
@@ -21,14 +21,14 @@ class Settings(BaseSettings):
     {%- if cookiecutter.db_info.name != "none" %}
     {%- if cookiecutter.db_info.name == "sqlite" %}
     db_file: Path = TEMP_DIR / "db.sqlite3"
-    {% else %}
+    {%- else %}
     db_host: str = "{{cookiecutter.project_name}}-db"
     db_port: int = {{cookiecutter.db_info.port}}
     db_user: str = "{{cookiecutter.project_name}}"
     db_pass: str = "{{cookiecutter.project_name}}"
     db_base: str = "{{cookiecutter.project_name}}"
-    db_echo: bool = False
     {%- endif %}
+    db_echo: bool = False
     {%- endif %}
 
     {%- if cookiecutter.enable_redis == "True" %}
diff --git a/pyproject.toml b/pyproject.toml
index 7904b9b..2976e9e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "fastapi_template"
-version = "2.1.2"
+version = "2.1.3"
 description = "Feature-rich robust FastAPI template"
 authors = ["Pavel Kirilin <win10@list.ru>"]
 packages = [
-- 
GitLab