Skip to content

Commit

Permalink
OONI Run v2 DB Migration (#819)
Browse files Browse the repository at this point in the history
Add support for running db migration in docker image.

We should not have a separate process for doing the migration, but it should be runnable from directly inside of the docker image of the installed software component.

* Remove dbmigration codebuild task. We should run them inside of the container repl

* Move almebic script to top level directory

* Fix docker build

* Fix hatch version getting

* Fix db migration

* Fix path to alembic migrations

* Add coverage to gitignore
  • Loading branch information
hellais authored Mar 13, 2024
1 parent f680179 commit 7cc043b
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 446 deletions.
10 changes: 10 additions & 0 deletions ooniapi/services/oonirun/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
*.log
*.pyc
*.swp
*.env
.coverage
coverage.xml
dist/
.venv/
__pycache__/
1 change: 1 addition & 0 deletions ooniapi/services/oonirun/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist
/coverage_html
*.coverage*
12 changes: 10 additions & 2 deletions ooniapi/services/oonirun/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ RUN python -m pip install hatch

COPY . /build

# When you build stuff on macOS you end up with ._ files
# https://apple.stackexchange.com/questions/14980/why-are-dot-underscore-files-created-and-how-can-i-avoid-them
RUN find /build -type f -name '._*' -delete

RUN echo "$BUILD_LABEL" > /build/src/oonirun/BUILD_LABEL

RUN hatch build
Expand All @@ -17,9 +21,13 @@ FROM python:3.11-bookworm as runner

WORKDIR /app

COPY --from=builder /build /build/dist/*.whl /app/

COPY --from=builder /build/README.md /app/
COPY --from=builder /build/dist/*.whl /app/
RUN pip install /app/*whl && rm /app/*whl

COPY --from=builder /build/alembic/ /app/alembic/
COPY --from=builder /build/alembic.ini /app/
RUN rm -rf /app/alembic/__pycache__

CMD ["uvicorn", "oonirun.main:app", "--host", "0.0.0.0", "--port", "80"]
EXPOSE 80
3 changes: 2 additions & 1 deletion ooniapi/services/oonirun/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ IMAGE_NAME ?= ooni/api-$(SERVICE_NAME)
DATE := $(shell python3 -c "import datetime;print(datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%d'))")
GIT_FULL_SHA ?= $(shell git rev-parse HEAD)
SHORT_SHA := $(shell echo ${GIT_FULL_SHA} | cut -c1-8)
PKG_VERSION := $(shell cat pyproject.toml | grep -e 'version\s*=' | cut -d '"' -f2)
PKG_VERSION := $(shell hatch version)

BUILD_LABEL := $(DATE)-$(SHORT_SHA)
VERSION_LABEL = v$(PKG_VERSION)
Expand All @@ -29,6 +29,7 @@ docker-build:
-t ${IMAGE_NAME}:${VERSION_LABEL} \
-t ${IMAGE_NAME}:${ENV_LABEL} \
-
echo "built image: ${IMAGE_NAME}:${BUILD_LABEL} (${IMAGE_NAME}:${VERSION_LABEL} ${IMAGE_NAME}:${ENV_LABEL})"

docker-push:
# We need to use tar -czh to resolve the common dir symlink
Expand Down
15 changes: 15 additions & 0 deletions ooniapi/services/oonirun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Alembic database migrations

When you make changes to the DB schema you will have to run the alembic scripts for generating an appropriate migration file.

This is how you do it:

1. Create the template migration script
```
poetry run alembic revision -m "name of the revision"
```
2. Edit the newly created python file and fill out the `upgrade()` and `downgrade()` function with the relevant code bits
3. You can now run the migration like so:
```
OONI_PG_URL=postgresql://oonipg:oonipg@localhost/oonipg hatch run alembic upgrade head
```
File renamed without changes.
26 changes: 0 additions & 26 deletions ooniapi/services/oonirun/buildspec-dbmigration.yml

This file was deleted.

5 changes: 1 addition & 4 deletions ooniapi/services/oonirun/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ addopts = ["--import-mode=importlib"]
branch = true
parallel = true
source_pkgs = ["oonirun", "tests"]
omit = [
"src/oonirun/common/*",
"src/oonirun/__about__.py"
]
omit = ["src/oonirun/common/*", "src/oonirun/__about__.py"]

[tool.coverage.paths]
oonirun = ["src/oonirun"]
Expand Down
15 changes: 0 additions & 15 deletions ooniapi/services/oonirun/src/oonirun/alembic/Readme.md

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7cc043b

Please sign in to comment.