From ec587398522093f9f2f825dafd679d129e8773da Mon Sep 17 00:00:00 2001 From: Nikolay Tatarinov Date: Wed, 18 Feb 2026 13:06:57 +0300 Subject: [PATCH] Refactor Alembic configuration and update Docker setup - Removed the `alembic.ini` file and migrated its configuration to `pyproject.toml` under `[tool.alembic]`, enhancing project organization. - Updated the `Dockerfile` to copy `pyproject.toml` instead of `alembic.ini`, ensuring the new configuration is utilized during the build process. - Modified `entrypoint.sh` to use the new Alembic configuration from `pyproject.toml` for database migrations. - Updated README documentation to reflect the new Alembic configuration and usage instructions. --- Dockerfile | 2 +- README.md | 2 +- alembic.ini | 39 --------------------------------------- alembic/env.py | 12 +++++++++--- entrypoint.sh | 2 +- pyproject.toml | 5 +++++ 6 files changed, 17 insertions(+), 45 deletions(-) delete mode 100644 alembic.ini diff --git a/Dockerfile b/Dockerfile index e106af1..31e2155 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,7 @@ COPY --from=builder /usr/local/bin /usr/local/bin # Application code (duty_teller package + entrypoint, migrations, webapp) ENV PYTHONPATH=/app -COPY main.py alembic.ini entrypoint.sh ./ +COPY main.py pyproject.toml entrypoint.sh ./ COPY duty_teller/ ./duty_teller/ COPY alembic/ ./alembic/ COPY webapp/ ./webapp/ diff --git a/README.md b/README.md index 12a40db..6f7e35a 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Ensure `.env` exists (e.g. `cp .env.example .env`) and contains `BOT_TOKEN`. - `services/` – Business logic (group duty pin, import); accept session from caller. - `utils/` – Shared date, user, and handover helpers. - `importers/` – Duty-schedule JSON parser. -- `alembic/` – Migrations (use `duty_teller.config.DATABASE_URL` and `duty_teller.db.models.Base`). +- `alembic/` – Migrations; config in `pyproject.toml` under `[tool.alembic]`; URL and metadata from `duty_teller.config` and `duty_teller.db.models.Base`. Run: `alembic -c pyproject.toml upgrade head`. - `webapp/` – Miniapp UI (calendar, duty list); served at `/app`. - `tests/` – Tests; `helpers.py` provides `make_init_data` for auth tests. - `pyproject.toml` – Installable package (`pip install -e .`). diff --git a/alembic.ini b/alembic.ini deleted file mode 100644 index e9ef928..0000000 --- a/alembic.ini +++ /dev/null @@ -1,39 +0,0 @@ -# Alembic config; url is set in env.py from config -[alembic] -script_location = alembic -prepend_sys_path = . -version_path_separator = os - -[loggers] -keys = root,sqlalchemy,alembic - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = WARN -handlers = console -qualname = - -[logger_sqlalchemy] -level = WARN -handlers = -qualname = sqlalchemy.engine - -[logger_alembic] -level = INFO -handlers = -qualname = alembic - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = %(levelname)-5.5s [%(name)s] %(message)s -datefmt = %H:%M:%S diff --git a/alembic/env.py b/alembic/env.py index 6734dd9..14c13c1 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,8 +1,8 @@ """Alembic env: use duty_teller config DATABASE_URL and db.models.Base.""" +import logging import os import sys -from logging.config import fileConfig from sqlalchemy import create_engine from alembic import context @@ -12,9 +12,15 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import duty_teller.config as config from duty_teller.db.models import Base +# Logging when config is in pyproject.toml (no fileConfig) +logging.basicConfig( + format="%(levelname)-5.5s [%(name)s] %(message)s", + datefmt="%H:%M:%S", + level=logging.INFO, +) +logging.getLogger("sqlalchemy.engine").setLevel(logging.WARN) + config_alembic = context.config -if config_alembic.config_file_name is not None: - fileConfig(config_alembic.config_file_name) database_url = config.DATABASE_URL config_alembic.set_main_option("sqlalchemy.url", database_url) diff --git a/entrypoint.sh b/entrypoint.sh index 828dcd2..6558967 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -4,7 +4,7 @@ set -e mkdir -p /app/data chown botuser:botuser /app/data # Apply Alembic migrations (runs as root, creates DB in /app/data) -alembic upgrade head +alembic -c pyproject.toml upgrade head # Ensure new DB file is owned by botuser so the app can write chown -R botuser:botuser /app/data # Run the app as botuser diff --git a/pyproject.toml b/pyproject.toml index eb5f155..5b59c92 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,11 @@ dev = [ where = ["."] include = ["duty_teller*"] +[tool.alembic] +script_location = "alembic" +prepend_sys_path = "." +version_path_separator = "os" + [tool.black] line-length = 120 target-version = ["py311"]