Skip to content

Commit

Permalink
Merge pull request #310 from uhh-lt/fix-startup-migrations
Browse files Browse the repository at this point in the history
Always run alembic migrations during startup
  • Loading branch information
bigabig authored Dec 18, 2023
2 parents 44b88f3 + d7162b3 commit 19939bb
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
6 changes: 6 additions & 0 deletions backend/src/app/core/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from loguru import logger

import config
from migration.migrate import run_required_migrations


def startup(sql_echo: bool = False, reset_data: bool = False) -> None:
Expand Down Expand Up @@ -53,6 +54,11 @@ def delete_progress_indicator_file():
try:
config.verify_config()

if not startup_in_progress:
# If we're the first uvicorn worker to start,
# run database migrations
run_required_migrations()

# start and init services
__init_services__(
create_root_repo_directory_structure=not startup_in_progress,
Expand Down
2 changes: 0 additions & 2 deletions backend/src/backend_api_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ API_PRODUCTION_WORKERS=${API_PRODUCTION_WORKERS:-10}
# assert ray is reachable
[ "${RAY_ENABLED}" = "True" ] && ./test_ray.sh

python run_migrations.py

if [ "${API_PRODUCTION_MODE}" -ge 1 ]; then
# start api in production mode without hot reload and only X worker
uvicorn --log-level "${LOG_LEVEL}" --port "${API_PORT}" --host "0.0.0.0" --workers "${API_PRODUCTION_WORKERS}" main:app
Expand Down
7 changes: 0 additions & 7 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from uvicorn.main import uvicorn

from app.core.authorization.authz_user import ForbiddenError
from migration.migrate import run_required_migrations

from app.core.startup import startup # isort: skip

Expand Down Expand Up @@ -279,12 +278,6 @@ def main() -> None:
port is not None and isinstance(port, int) and port > 0
), "The API port has to be a positive integer! E.g. 8081"

# Migrations are usually run in the docker entrypoint script.
# When the backend is run in development mode outside a container,
# the `main` function we're in is used instead.
# In that case, we'll need to run the migrations now.
run_required_migrations()

is_debug = conf.api.production_mode == "0"

uvicorn.run(
Expand Down
2 changes: 0 additions & 2 deletions backend/src/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
from app.core.authorization.authz_user import AuthzUser
from app.core.data.orm.project import ProjectORM
from app.core.startup import startup
from migration.migrate import run_required_migrations

os.environ["RAY_ENABLED"] = "False"

# Flo: just do it once. We have to check because if we start the main function, unvicorn will import this
# file once more manually, so it would be executed twice.
STARTUP_DONE = bool(int(os.environ.get("STARTUP_DONE", "0")))
if not STARTUP_DONE:
run_required_migrations()
startup(reset_data=True)
os.environ["STARTUP_DONE"] = "1"

Expand Down

0 comments on commit 19939bb

Please sign in to comment.