Refactor app.main for clearer database setup#2586
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2586 +/- ##
==========================================
+ Coverage 78.29% 78.37% +0.07%
==========================================
Files 281 282 +1
Lines 20611 20653 +42
Branches 2102 2104 +2
==========================================
+ Hits 16137 16186 +49
+ Misses 4426 4423 -3
+ Partials 48 44 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
945df3b to
80ef4ef
Compare
- separate registering of blueprints and creating DB connections - avoid using unclear db.init_app(), instead explicitly register request handlers to open/close DB connection(s) - explicitly set db.database and db.replica, also in test fixtures
80ef4ef to
a9ce7b3
Compare
4c1a780 to
9f5c4ce
Compare
- fix circular import of db into models.utils - add test to avoid forgetting to update MODELS when creating new subclass
This reverts commit 21740bb.
There was a problem hiding this comment.
Pull request overview
This PR refactors backend database setup to make app.main() and database/model binding more explicit, including a move away from playhouse.flask_utils.FlaskDB and introducing a central, explicit MODELS registry.
Changes:
- Replace
FlaskDB-based database integration with a customDatabaseManagerand explicit request handlers. - Introduce
boxtribute_server.models.MODELSas the authoritative list of Peewee models, and update binding logic to use it. - Move/adjust raw SQL helpers (
execute_sql,current_database) and update tests + cron/scripts accordingly.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| back/test/unit_tests/test_db.py | Adds unit coverage for current_database() failure mode when unbound. |
| back/test/model_tests/test_utils.py | Updates model utility tests to use new DB helpers and direct Peewee cursor results. |
| back/test/model_tests/test_all.py | Adds checks around explicit MODELS vs Model.__subclasses__(). |
| back/test/endpoint_tests/test_app.py | Removes replica-usage test from endpoint tests. |
| back/test/data/init.py | Removes test-data-owned MODELS derivation via __subclasses__(). |
| back/test/conftest.py | Updates app/db test fixture setup to use register_blueprints + explicit db wiring and MODELS. |
| back/test/auth0_integration_tests/test_operations.py | Adds replica-usage integration test asserting replica connection/binding. |
| back/test/auth0_integration_tests/test_cron.py | Adds staging reseed-db integration test. |
| back/test/auth0_integration_tests/conftest.py | Updates teardown to reset model bindings via MODELS. |
| back/scripts/list_models.py | Switches model listing script to import MODELS from application package. |
| back/boxtribute_server/models/utils.py | Removes SQL helper from models utils; adjusts atomic blocks to use model-bound DB. |
| back/boxtribute_server/models/init.py | Adds explicit imports + MODELS tuple as canonical model registry. |
| back/boxtribute_server/graph_ql/loaders.py | Uses model-bound DB (History._meta.database) for session config SQL. |
| back/boxtribute_server/db.py | Refactors DB manager, introduces current_database() and execute_sql() utilities, updates replica binding. |
| back/boxtribute_server/cron/reseed_db.py | Migrates reseed operations to current_database() / execute_sql(). |
| back/boxtribute_server/cron/housekeeping.py | Uses User._meta.database atomic context instead of global DB handle. |
| back/boxtribute_server/cron/data_faking.py | Uses model-bound DB for raw SQL execution. |
| back/boxtribute_server/cli/main.py | Binds CLI DB to explicit MODELS list. |
| back/boxtribute_server/business_logic/warehouse/box/mutations.py | Switches imports to use db.execute_sql. |
| back/boxtribute_server/business_logic/statistics/crud.py | Removes explicit db selection parameter; relies on binding context + new execute_sql. |
| back/boxtribute_server/app.py | Refactors app setup: blueprint registration separated; DB setup made explicit in main(). |
| back/README.md | Minor doc fixes/updates to reflect refactor. |
b5f1f18 to
453a956
Compare
|
|
||
|
|
||
| def test_current_database_without_binding(): | ||
| with pytest.raises(RuntimeError): |
There was a problem hiding this comment.
add match, also next test
| org_ids = {b.id: b.organisation_id for b in bases} | ||
|
|
||
| cursor = db.database.execute_sql( | ||
| cursor = Base._meta.database.execute_sql( |
There was a problem hiding this comment.
use current_database() ?
No description provided.