From 6bc64459f57be5e429de2a82fb1c03c33c9ff2b9 Mon Sep 17 00:00:00 2001 From: Yannic Kilcher Date: Tue, 13 Dec 2022 16:19:29 +0100 Subject: [PATCH] local database setup --- backend/README.md | 11 ++++++++++- backend/alembic.ini | 10 +++++----- backend/alembic/env.py | 4 +++- backend/app/config.py | 5 +++++ backend/app/database.py | 3 +++ backend/app/main.py | 24 ++++++++++++++++++++++-- backend/requirements.txt | 1 + backend/scripts/docker-compose.yaml | 17 +++++++++++++++++ backend/scripts/run-local.sh | 2 ++ 9 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 backend/scripts/docker-compose.yaml diff --git a/backend/README.md b/backend/README.md index c75385c2..3c47d5f6 100644 --- a/backend/README.md +++ b/backend/README.md @@ -2,7 +2,9 @@ ## Alembic database init -Please edit `alembic.ini` and specify your database URI in the parameter `sqlalchemy.url = postgresql://:@/`. +~~Please edit `alembic.ini` and specify your database URI in the parameter `sqlalchemy.url = postgresql://:@/`.~~ + +set the DATABASE_URI environment variable to the database URI, alembic will upgrade automatically on startup. ## REST Server Configuration @@ -15,3 +17,10 @@ DATABASE_URI="postgresql://:@/" BACKEND_CORS_ORIGINS=["http://localhost", "http://localhost:4200", "http://localhost:3000", "http://localhost:8080", "https://localhost", "https://localhost:4200", "https://localhost:3000", "https://localhost:8080", "http://dev.ocgpt.laion.ai", "https://stag.ocgpt.laion.ai", "https://ocgpt.laion.ai"] ``` + +## Running the REST Server locally for development + +Run two terminals (note the working directory for each): + +- Terminal 1, to go `backend/scripts` and run `docker-compose up`. This will start postgres. +- Terminal 2, to go `backend` and run `scripts/run-local.sh`. This will start the REST server. diff --git a/backend/alembic.ini b/backend/alembic.ini index aecebb0b..39874aee 100644 --- a/backend/alembic.ini +++ b/backend/alembic.ini @@ -2,7 +2,7 @@ [alembic] # path to migration scripts -script_location = alembic +script_location = %(here)s/alembic # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s # Uncomment the line below if you want the files to be prepended with date and time @@ -55,7 +55,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = postgresql://:@/ +# sqlalchemy.url = postgresql://:@/ [post_write_hooks] @@ -64,9 +64,9 @@ sqlalchemy.url = postgresql://:@/ # detail and examples # format using "black" - use the console_scripts runner, against the "black" entrypoint -# hooks = black -# black.type = console_scripts -# black.entrypoint = black +hooks = black +black.type = console_scripts +black.entrypoint = black # black.options = -l 79 REVISION_SCRIPT_FILENAME # Logging configuration diff --git a/backend/alembic/env.py b/backend/alembic/env.py index af7461d5..ae870d84 100644 --- a/backend/alembic/env.py +++ b/backend/alembic/env.py @@ -1,7 +1,9 @@ # -*- coding: utf-8 -*- from logging.config import fileConfig +import sqlmodel from alembic import context +from app import models # noqa: F401 from sqlalchemy import engine_from_config, pool # this is the Alembic Config object, which provides @@ -17,7 +19,7 @@ if config.config_file_name is not None: # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata -target_metadata = None +target_metadata = sqlmodel.SQLModel.metadata # other values from the config, defined by the needs of env.py, # can be acquired: diff --git a/backend/app/config.py b/backend/app/config.py index d7700d75..b95ca408 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -11,6 +11,11 @@ class Settings(BaseSettings): DATABASE_URI: Optional[PostgresDsn] = None BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] + UPDATE_ALEMBIC: bool = True + + PORT: int = 8000 + FASTAPI_RELOAD: bool = False + UVICONR_WOWKERS: int = 1 @validator("BACKEND_CORS_ORIGINS", pre=True) def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]: diff --git a/backend/app/database.py b/backend/app/database.py index 5191e8c2..89b636cd 100644 --- a/backend/app/database.py +++ b/backend/app/database.py @@ -2,4 +2,7 @@ from app.config import settings from sqlmodel import create_engine +if settings.DATABASE_URI is None: + raise ValueError("DATABASE_URI is not set") + engine = create_engine(settings.DATABASE_URI) diff --git a/backend/app/main.py b/backend/app/main.py index 3cf36425..27edbc88 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,10 +1,15 @@ # -*- coding: utf-8 -*- +from pathlib import Path + +import alembic.command +import alembic.config +import fastapi from app.api.v1.api import api_router from app.config import settings -from fastapi import FastAPI +from loguru import logger from starlette.middleware.cors import CORSMiddleware -app = FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json") +app = fastapi.FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json") # Set all CORS enabled origins if settings.BACKEND_CORS_ORIGINS: @@ -16,4 +21,19 @@ if settings.BACKEND_CORS_ORIGINS: allow_headers=["*"], ) +if settings.UPDATE_ALEMBIC: + + @app.on_event("startup") + def alembic_upgrade(): + logger.info("Attempting to upgrade alembic on startup") + try: + alembic_ini_path = Path(__file__).parent.parent / "alembic.ini" + alembic_cfg = alembic.config.Config(str(alembic_ini_path)) + alembic_cfg.set_main_option("sqlalchemy.url", settings.DATABASE_URI) + alembic.command.upgrade(alembic_cfg, "head") + logger.info("Successfully upgraded alembic on startup") + except Exception: + logger.exception("Alembic upgrade failed on startup") + + app.include_router(api_router, prefix=settings.API_V1_STR) diff --git a/backend/requirements.txt b/backend/requirements.txt index fa6bee77..92668609 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,5 +1,6 @@ alembic==1.8.1 fastapi==0.88.0 +loguru==0.6.0 psycopg2-binary==2.9.5 pydantic==1.9.1 python-dotenv==0.21.0 diff --git a/backend/scripts/docker-compose.yaml b/backend/scripts/docker-compose.yaml new file mode 100644 index 00000000..2b92d6b0 --- /dev/null +++ b/backend/scripts/docker-compose.yaml @@ -0,0 +1,17 @@ +version: "3.7" + +services: + db: + image: postgres + restart: always + ports: + - 5432:5432 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + + adminer: + image: adminer + restart: always + ports: + - 8089:8080 diff --git a/backend/scripts/run-local.sh b/backend/scripts/run-local.sh index d8bd86c6..7c6efabc 100755 --- a/backend/scripts/run-local.sh +++ b/backend/scripts/run-local.sh @@ -1,3 +1,5 @@ #!/usr/bin/env bash +export DATABASE_URI=postgresql://postgres:postgres@localhost:5432/postgres + uvicorn app.main:app --reload