This repository is used to run the backend of meticulous. It handles ESP32 serial communication, shot management, profiles, sounds, OTA updates, and exposes a REST + Socket.IO API.
This project uses uv for dependency management. Dependencies are declared in pyproject.toml and pinned in uv.lock.
curl -LsSf https://astral.sh/uv/install.sh | shuv syncThis installs the base dependencies into a .venv managed by uv.
Some dependencies require system C libraries that are only available on the target machine or in the Docker build environment:
# On the target machine (installs gpiod, PyGObject, pycairo, pyparted)
uv sync --group machineFor development tools (black, flake8, pre-commit, pytest):
uv sync --group dev# Add a new dependency
uv add <package>
# Add a dev dependency
uv add --group dev <package>
# Update the lock file after manual edits to pyproject.toml
uv lockNOTE: As we are running the backend on a debian bookworm system, the installed python3 binary is set automatically set to 3.11.2. In order to avoid possible syntax issues or feature missmatches, install the aforementioned version in the system you develop in, preferably in a virtual environment, its possible to make use of the pyenv tool to help with the installation and management of multiple python versions
- Formatter: black (line-length 96)
- Linter: flake8
- Pre-commit hooks are configured in
.pre-commit-config.yaml
# Install pre-commit hooks
uv run pre-commit install
# Run manually
uv run pre-commit run --all-filesTo allow developers to run the backend without a physical coffee machine, we have implemented a Docker configuration. Follow these steps:
# Branch
git fetch origin
git switch main
# Docker compose
docker compose run --build -p 8080:8080 backendIf you are on Linux, start the backend directly using the emulation script:
./run_emulated.shThis sets up environment variables for local development and runs the backend with uv run.
Use uv run to execute commands within the managed virtualenv:
uv run python3 back.py
uv run pytest
uv run black --check .
uv run flake8You can interact with the backend using the command line interface. For instance, you can enter the commands l and r to move the dial left or right, respectively.
This project uses Alembic for managing database migrations. Follow these steps to handle any changes in the database structure:
-
Modify Database Models: Edit
database_models.pyto reflect the required changes in your database structure. You can:- Add or modify tables, columns, or constraints.
- Remember, this file is the single source of truth for the database schema.
-
Generate a Migration Script: Run the following command to create a new migration script:
uv run alembic revision --autogenerate -m "Brief description of change"- A new script will be generated in the
alembic/versionsdirectory. - Open the generated script and review the
upgrade()anddowngrade()functions. - Ensure these functions accurately reflect your intended changes, and modify them if necessary.
- A new script will be generated in the
-
Apply the Migration: Update your local database to the latest version with:
uv run alembic upgrade head
- Simply push your changes to the
mainbranch. - Other machines will automatically apply the migrations using the
db_migration_updater.pyscript.
If you need to revert to a previous database version:
-
Identify the Revision: Find the desired revision ID from the scripts in the
alembic/versionsdirectory. -
Set the Stable Version: Update the version in
db_migration_updater.py:MIGRATION_VERSION_STABLE = "revision_id" # e.g., "ebb6a77afd0e"
-
Automatic Downgrade: The system will automatically downgrade the database to the specified version.
- Testing: Always test your migration scripts locally before deploying them.
- Collaboration: Communicate with your team when making significant changes to the database schema.
- Documentation: Keep your migration messages clear to track the evolution of the database.