Skip to content

Commit

Permalink
Add database schema initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
berrydenhartog committed Jun 11, 2024
1 parent e5347bf commit fbb3e2c
Show file tree
Hide file tree
Showing 40 changed files with 471 additions and 492 deletions.
24 changes: 0 additions & 24 deletions .env.test

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ __pypackages__/
# tad tool
tad.log*
database.sqlite3
output/
8 changes: 6 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"request": "launch",
"module": "uvicorn",
"justMyCode": false,
"args": [ "--log-level", "warning" ,"tad.main:app"],
"args": [
"--log-level",
"warning",
"tad.main:app"
],
"cwd": "${workspaceFolder}/",
"env": {
"PYTHONPATH": "${workspaceFolder}"
Expand All @@ -20,7 +24,7 @@
"request": "launch",
"module": "pytest",
"cwd": "${workspaceFolder}",
"justMyCode": true,
"justMyCode": false,
"args": []
}
]
Expand Down
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ USER tad

COPY --chown=root:root --chmod=755 ./tad /app/tad
COPY --chown=root:root --chmod=755 alembic.ini /app/alembic.ini
COPY --chown=root:root --chmod=755 .env /app/.env
COPY --chown=root:root --chmod=755 prod.env /app/.env
COPY --chown=root:root --chmod=755 LICENSE /app/LICENSE
COPY --chown=tad:tad --chmod=755 docker-entrypoint.sh /app/docker-entrypoint.sh

ENV PYTHONPATH=/app/
WORKDIR /app/

CMD ["python", "-m", "uvicorn", "--host", "0.0.0.0", "tad.main:app", "--log-level", "warning" ]
ENV PATH="/app/:$PATH"

CMD [ "docker-entrypoint.sh" ]
7 changes: 4 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ services:
db:
condition: service_healthy
env_file:
- path: .env
- path: prod.env
required: true
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?Variable not set}
- ENVIRONMENT=demo
ports:
- 8000:8000
healthcheck:
Expand All @@ -25,7 +26,7 @@ services:
- app-db-data:/var/lib/postgresql/data/pgdata
- ./database/:/docker-entrypoint-initdb.d/:cached
env_file:
- path: .env
- path: prod.env
required: true
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
Expand All @@ -40,7 +41,7 @@ services:
ports:
- 8080:8080
environment:
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL:-[email protected]}
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL:-[email protected]}
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD:?Variable not set}
- PGADMIN_LISTEN_PORT=${PGADMIN_LISTEN_PORT:-8080}
depends_on:
Expand Down
3 changes: 1 addition & 2 deletions database/init-user-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ set -e
# todo(berry): make user and database variables
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER tad WITH PASSWORD 'changethis';
CREATE DATABASE tad;
GRANT ALL PRIVILEGES ON DATABASE tad TO tad;
CREATE DATABASE tad OWNER tad;
EOSQL
51 changes: 51 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

DATABASE_MIGRATE=""
HOST="0.0.0.0"
LOGLEVEL="warning"
PORT="8000"

while getopts "dh:l:p:" opt; do
case $opt in
d)
DATABASE_MIGRATE="True"
;;
h)
HOST=$OPTARG
;;
l)
LOGLEVEL=$OPTARG
;;
p)
PORT=$OPTARG
;;
:)
echo "Option -${OPTARG} requires an argument."
exit 1
;;

?)
echo "Invalid option: $OPTARG"

echo "Usage: docker-entrypoint.sh [-d] [-h host] [-l loglevel]"
exit 1
;;
esac
done

echo "DATABASE_MIGRATE: $DATABASE_MIGRATE"
echo "HOST: $HOST"
echo "LOGLEVEL: $LOGLEVEL"
echo "PORT: $PORT"


if [ -z $DATABASE_MIGRATE ]; then
echo "Upgrading database"
if ! alembic upgrade head; then
echo "Failed to upgrade database"
exit 1
fi
fi

echo "Starting server"
python -m uvicorn --host "$HOST" tad.main:app --port "$PORT" --log-level "$LOGLEVEL"
40 changes: 35 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions .env → prod.env
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Domain
DOMAIN=localhost

# Environment: local, staging, production
ENVIRONMENT=local
PROJECT_NAME="TAD"
# Environment: local, production, demo
ENVIRONMENT=production

# TAD backend
BACKEND_CORS_ORIGINS="http://localhost,https://localhost,http://127.0.0.1,https://127.0.0.1"
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pyyaml = "^6.0.1"
pytest = "^8.2.1"
coverage = "^7.5.3"
httpx = "^0.27.0"
urllib3 = "^2.2.1"
playwright = "^1.44.0"
pytest-playwright = "^0.5.0"

Expand Down Expand Up @@ -107,10 +106,12 @@ title = "tad"
testpaths = [
"tests"
]
addopts = "--strict-markers"
addopts = "--strict-markers -v -q"
filterwarnings = [
"ignore::UserWarning"
]
log_cli = true
log_cli_level = "INFO"

[tool.liccheck]
level = "PARANOID"
Expand Down
5 changes: 5 additions & 0 deletions script/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -x

docker build . -t ghcr.io/minbzk/tad:latest "$@"
2 changes: 1 addition & 1 deletion script/format
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -x

ruff format $@
ruff format "$@"
3 changes: 1 addition & 2 deletions script/lint
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bash

set -e
set -x

ruff check --fix $@
ruff check --fix "$@"
8 changes: 4 additions & 4 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
set -e
set -x

coverage run -m pytest $@
if [ $? -ne 0 ]; then

if ! coverage run -m pytest "$@" ; then
echo "Test failed"
exit 1
fi

coverage report
coverage html
coverage lcov
pyright $@
if [ $? -ne 0 ]; then

if ! pyright; then
echo "Typecheck failed"
exit 1
fi
4 changes: 1 addition & 3 deletions tad/api/routes/deps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from fastapi.templating import Jinja2Templates
from jinja2 import Environment

from tad.core.config import settings

env = Environment(
autoescape=True,
)
templates = Jinja2Templates(directory=settings.TEMPLATE_DIR, env=env)
templates = Jinja2Templates(directory="tad/site/templates/", env=env)
9 changes: 1 addition & 8 deletions tad/api/routes/root.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
from fastapi import APIRouter
from fastapi.responses import FileResponse, RedirectResponse

from tad.core.config import settings
from fastapi.responses import RedirectResponse

router = APIRouter()


@router.get("/")
async def base() -> RedirectResponse:
return RedirectResponse("/pages/")


@router.get("/favicon.ico", include_in_schema=False)
async def favicon():
return FileResponse(settings.STATIC_DIR + "/favicon.ico")
Loading

0 comments on commit fbb3e2c

Please sign in to comment.