mirror of
https://github.com/evroon/bracket.git
synced 2026-02-18 15:11:50 -05:00
Autorun alembics (#455)
Runs alembic migration automatically on fastapi startup. Can be disabled using config setting.
This commit is contained in:
2
.github/workflows/backend.yml
vendored
2
.github/workflows/backend.yml
vendored
@@ -50,7 +50,7 @@ jobs:
|
||||
working-directory: backend
|
||||
|
||||
- name: Run pylint
|
||||
run: pipenv run pylint alembic bracket tests
|
||||
run: pipenv run pylint bracket tests cli.py
|
||||
working-directory: backend
|
||||
|
||||
- name: Run ruff format
|
||||
|
||||
@@ -6,7 +6,7 @@ name = "pypi"
|
||||
[packages]
|
||||
aiohttp = ">=3.8.3"
|
||||
aiopg = ">=1.4.0"
|
||||
alembic = ">=1.9.1"
|
||||
alembic = ">=1.13.1"
|
||||
bcrypt = "4.1.2"
|
||||
click = ">=8.1.3"
|
||||
databases = {extras = ["asyncpg"], version = "<=0.7.0"}
|
||||
|
||||
@@ -9,6 +9,8 @@ from starlette.middleware.cors import CORSMiddleware
|
||||
from starlette.responses import JSONResponse, Response
|
||||
from starlette.staticfiles import StaticFiles
|
||||
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from bracket.config import Environment, config, environment, init_sentry
|
||||
from bracket.cronjobs.scheduling import start_cronjobs
|
||||
from bracket.database import database
|
||||
@@ -34,11 +36,20 @@ from bracket.utils.logging import logger
|
||||
init_sentry()
|
||||
|
||||
|
||||
def run_migrations() -> None:
|
||||
logger.info("Running migrations")
|
||||
alembic_cfg = Config("alembic.ini")
|
||||
command.upgrade(alembic_cfg, "head")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||
await database.connect()
|
||||
await init_db_when_empty()
|
||||
|
||||
if config.auto_run_migrations and environment is not Environment.CI:
|
||||
run_migrations()
|
||||
|
||||
if environment is Environment.PRODUCTION:
|
||||
start_cronjobs()
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ class Config(BaseSettings):
|
||||
cors_origin_regex: str = ""
|
||||
cors_origins: str = "*"
|
||||
jwt_secret: str
|
||||
auto_run_migrations: bool = True
|
||||
pg_dsn: PostgresDsn = "postgresql://user:pass@localhost:5432/db" # type: ignore[assignment]
|
||||
sentry_dsn: str | None = None
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ from bracket.config import config
|
||||
from bracket.database import database
|
||||
from bracket.logger import get_logger
|
||||
from bracket.utils.db_init import sql_create_dev_db
|
||||
from bracket.utils.security import hash_password
|
||||
|
||||
logger = get_logger("cli")
|
||||
|
||||
@@ -42,7 +43,7 @@ def cli() -> None:
|
||||
|
||||
|
||||
@click.command()
|
||||
def hash_password() -> None:
|
||||
def hash_password_cmd() -> None:
|
||||
if config.admin_password is None:
|
||||
logger.error("No admin password is given")
|
||||
else:
|
||||
@@ -59,5 +60,5 @@ async def create_dev_db() -> None:
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli.add_command(create_dev_db)
|
||||
cli.add_command(hash_password)
|
||||
cli.add_command(hash_password_cmd)
|
||||
cli()
|
||||
|
||||
@@ -6,4 +6,4 @@ ruff --fix .
|
||||
! vulture | grep "unused function\|unused class\|unused method"
|
||||
dmypy run -- --follow-imports=normal --junit-xml= .
|
||||
ENVIRONMENT=CI pytest --cov --cov-report=xml . -vvv
|
||||
pylint alembic bracket tests
|
||||
pylint cli.py bracket tests
|
||||
|
||||
@@ -20,6 +20,8 @@ Copy `ci.env` to `prod.env` and fill in the values:
|
||||
used for production while bracket is still in beta
|
||||
- `ALLOW_INSECURE_HTTP_SSO`: Should not be used in production. Allows use of INSECURE requests for
|
||||
SSO auth.
|
||||
- `AUTO_RUN_MIGRATIONS`: Whether to run (alembic) migrations automatically on startup or not.
|
||||
Migrations can be applied manually using `pipenv run alembic upgrade head`.
|
||||
|
||||
### Backend: Example configuration file
|
||||
|
||||
@@ -36,6 +38,7 @@ SENTRY_DSN='my sentry dsn'
|
||||
ALLOW_USER_REGISTRATION=false
|
||||
ALLOW_INSECURE_HTTP_SSO=false
|
||||
CAPTCHA_SECRET='xxx'
|
||||
AUTO_RUN_MIGRATIONS=true
|
||||
```
|
||||
|
||||
## Frontend
|
||||
|
||||
Reference in New Issue
Block a user