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 18, 2024
1 parent cf8d3d0 commit 3920116
Show file tree
Hide file tree
Showing 47 changed files with 727 additions and 592 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"editor.defaultFormatter": "charliermarsh.ruff"
},
"python.analysis.typeCheckingMode": "strict",
"markiscodecoverage.searchCriteria": "coverage.lcov"
"markiscodecoverage.searchCriteria": "coverage.lcov",
"remote.autoForwardPorts": false,
"remote.restoreForwardedPorts": false
}
}
}
Expand Down
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/
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ repos:
rev: v4.6.0
hooks:
- id: end-of-file-fixer
exclude: ^tad/static/vendor/*
exclude: ^tad/static/vendor/.*
- id: trailing-whitespace
- id: check-yaml
- id: check-json

- id: check-added-large-files
- id: check-merge-conflict
- id: check-toml
Expand Down
17 changes: 12 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@
"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}"
},
"envFile": "${workspaceFolder}/.env.test"
"PYTHONPATH": "${workspaceFolder}",
"DEBUG": "True",
"AUTO_CREATE_SCHEMA": "True",
"ENVIRONMENT": "demo",
"LOGGING_LEVEL": "DEBUG"
}
},
{
"name": "Project: tests",
"type": "debugpy",
"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" ]
15 changes: 6 additions & 9 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ 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,12 +25,10 @@ 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
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?Variable not set}
- SECRET_KEY=${SECRET_KEY:?Variable not set}
healthcheck:
test: ["CMD", "pg_isready", "-q", "-d", "tad", "-U", "tad"]

Expand All @@ -40,16 +38,15 @@ services:
ports:
- 8080:8080
environment:
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL:[email protected]}
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD:?Variable not set}
- PGADMIN_LISTEN_PORT=${PGADMIN_LISTEN_PORT:-8080}
env_file:
- path: prod.env
required: true
depends_on:
db:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-O", "-", "http://localhost:8080/misc/ping"]

#TODO(berry): Traefik

volumes:
app-db-data:
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"
Loading

0 comments on commit 3920116

Please sign in to comment.