Skip to content

Refactor app.main for clearer database setup#2586

Draft
pylipp wants to merge 27 commits intomasterfrom
refactor-app-main
Draft

Refactor app.main for clearer database setup#2586
pylipp wants to merge 27 commits intomasterfrom
refactor-app-main

Conversation

@pylipp
Copy link
Copy Markdown
Contributor

@pylipp pylipp commented Feb 17, 2026

No description provided.

@sentry
Copy link
Copy Markdown

sentry bot commented Feb 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.37%. Comparing base (7305d94) to head (722917e).

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     
Flag Coverage Δ
backend 99.63% <ø> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pylipp pylipp force-pushed the refactor-app-main branch 2 times, most recently from 945df3b to 80ef4ef Compare February 19, 2026 12:00
pylipp added 5 commits March 3, 2026 15:16
- 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
@pylipp pylipp force-pushed the refactor-app-main branch from 80ef4ef to a9ce7b3 Compare March 3, 2026 14:17
@pylipp pylipp force-pushed the refactor-app-main branch from 4c1a780 to 9f5c4ce Compare March 5, 2026 08:10
- fix circular import of db into models.utils
- add test to avoid forgetting to update MODELS when creating new
  subclass
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 custom DatabaseManager and explicit request handlers.
  • Introduce boxtribute_server.models.MODELS as 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.

@pylipp pylipp force-pushed the refactor-app-main branch from b5f1f18 to 453a956 Compare March 26, 2026 08:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated 2 comments.



def test_current_database_without_binding():
with pytest.raises(RuntimeError):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use current_database() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants