Shared database models used by multiple PlanExe services (e.g., frontend_multi_user, future worker_plan_database). Models live here to keep them out of worker_plan_api, which stays worker-focused and lightweight.
model_event.py:EventTypeenum andEventItemSQLAlchemy model.model_taskitem.py:TaskStateenum andTaskItemSQLAlchemy model.model_worker.py:WorkerItemSQLAlchemy model for worker heartbeats.model_nonce.py:NonceItemSQLAlchemy model for nonce tracking.
Add the repo root (containing database_api/) to PYTHONPATH, then:
from database_api.planexe_db_singleton import db
from database_api.model_event import EventType, EventItem
from database_api.model_taskitem import TaskItem, TaskState
from database_api.model_worker import WorkerItem
from database_api.model_nonce import NonceItemEach model expects a db instance to be available in the module namespace (e.g., via from database_api.planexe_db_singleton import db in your service). Keep the models as-is to avoid divergence across services.
- I dislike that
database_apihas a dependency onFlask. Ideally it should only beSQLAlchemyand no flask. However I have tried getting rid ofFlask+flask_sqlalchemyand make it only depend onSQLAlchemy, but it complicate things, and I don't want to maintain complicated code. So I choose the simple way, accepting thatFlaskis a dependency. - The shared
dbobject is aflask_sqlalchemy.SQLAlchemyinstance; models subclassdb.Model, and bothfrontend_multi_user(admin UI) andworker_plan_databaseexpect to initialize it viadb.init_app(app). - Flask’s app/request contexts drive session lifecycle (create/teardown, rollback on errors) and the
.queryhelper that the models and admin views rely on. - The admin UI registers views using
db.session; switching to a plainSQLAlchemybase would break those until session wiring and teardown hooks were rebuilt. - Even the UI-free workers spin up a minimal Flask app purely to bootstrap
dband context handling; a newBasewould need equivalent scoped-session setup and teardown hooks. - Removing
Flaskwould require defining a new declarative base, custom scoped session management, and updating every service (workers + frontend) to use the new base consistently—risky until all consumers are migrated together.