Skip to content

Refactor DB access to use flask.g + teardown_appcontext #5

@timpara

Description

@timpara

Problem

Every request handler currently opens its own SQLite connection with a boilerplate `try/finally` block. This is repetitive and makes it easy to leak connections on new routes.

Proposal

  • Store the connection on `flask.g` inside a helper `get_request_db()`.
  • Register `@app.teardown_appcontext` to close it.
  • Delete the per-handler `try/finally: db.close()` noise.
  • Helpers `get_setting`/`set_setting` reuse the request connection when one exists.

Acceptance

  • All existing endpoints keep their behaviour (green test suite).
  • Handler code size reduced significantly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions