local database setup

This commit is contained in:
Yannic Kilcher
2022-12-13 16:19:29 +01:00
parent ceb8f873cc
commit 6bc64459f5
9 changed files with 68 additions and 9 deletions
+10 -1
View File
@@ -2,7 +2,9 @@
## Alembic database init
Please edit `alembic.ini` and specify your database URI in the parameter `sqlalchemy.url = postgresql://<username>:<password>@<host>/<database_name>`.
~~Please edit `alembic.ini` and specify your database URI in the parameter `sqlalchemy.url = postgresql://<username>:<password>@<host>/<database_name>`.~~
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://<username>:<password>@<host>/<database_name>"
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.
+5 -5
View File
@@ -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://<username>:<password>@<host>/<database_name>
# sqlalchemy.url = postgresql://<username>:<password>@<host>/<database_name>
[post_write_hooks]
@@ -64,9 +64,9 @@ sqlalchemy.url = postgresql://<username>:<password>@<host>/<database_name>
# 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
+3 -1
View File
@@ -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:
+5
View File
@@ -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]:
+3
View File
@@ -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)
+22 -2
View File
@@ -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)
+1
View File
@@ -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
+17
View File
@@ -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
+2
View File
@@ -1,3 +1,5 @@
#!/usr/bin/env bash
export DATABASE_URI=postgresql://postgres:postgres@localhost:5432/postgres
uvicorn app.main:app --reload