| name | database-use |
| description | Any time database-related activity is required. |
Database use instructions
Configuration & Standards
- Engine: PostgreSQL only (Port: 5433).
- Connection string: sourced from
.envviaDATABASE_URL(seeSQLALCHEMY_DATABASE_URI/DATABASE_URLinconfig.py). - Testing DB:
- By default tests also use the live DB (see
pytestnote below). - Optional override:
TEST_DATABASE_URL(used byTestingConfiginconfig.py).
- By default tests also use the live DB (see
- SQLAlchemy instance: Import
dbfromutils_db/database.py. Do NOT create a new instance inapp.py. - Scripts location:
- Canonical database scripts/utilities live in
utils_db/. - No ad-hoc “root scripts” in the repo root.
- Canonical database scripts/utilities live in
- Execution safety:
- Do NOT paste multi-line SQL or Python into the terminal.
- Draft ad-hoc scripts in
temp/first, then promote reusable ones intoutils_db/.
- Testing CSRF:
TestingConfigdisables CSRF (WTF_CSRF_ENABLED = False) specifically for tests.
Credentials
- Connection credentials:
.env. - Users' credentials:
.envUser passwords in DB are hashed. Hash is stored in the pw_hashed column.
Structure
- Schema documentation:
.roo/docs/database_schema.md - SQLAlchemy model files:
models/models_*.py(eg,models/models_user.py) - SQLAlchemy database instance:
utils_db/database.py - Schema tools:
- Primary:
utils_db/schema_inspector.py - Supplemental:
utils_db/schema_compare.py(writes reports totemp/)
- Primary:
Preferred Utilities
Reuse existing tools in utils_db/ before writing new ones:
utils_db/user_password_utils.pyutils_db/user_management_utils.pyutils_db/media_utils.py
Source of Truth Hierarchy
The formal Source of Truth (SoT) hierarchy for database schema information:
- PGDB (live PostgreSQL)
- models_*.py (SQLAlchemy) (eg,
models/models_user.py) .roo/docs/database_schema.md(generated)
When there is any doubt about a column, see PGDB. If a column is needed or a column name needs to change, always ask user for permission to make the add/change.
Schema Update Workflow
When making schema or data changes, follow this workflow in order:
- Modify PGDB
- Make changes to the live database (see "Credentials" above).
- When creating a script to check or change the database:
- Do NOT paste the script into the terminal.
- Check
utils_db/for a suitable or near-suitable script first; reuse/extend if possible. - If it’s a true one-off, draft it in
temp/first. - If it’s reusable, create/update a
.pyutility inutils_db/.
- Update models
- Update the appropriate SQLAlchemy model file(s) under
models/models_*.py.
- Update the appropriate SQLAlchemy model file(s) under
- Regenerate documentation
- Run:
python utils_db/schema_inspector.py generate-docsto update.roo/docs/database_schema.md.
- Run:
- Log changes
- Record the date and change in
.roo/docs/pgdb_changes.md.
- Record the date and change in
Schema Inspector Utility
The utils_db/schema_inspector.py tool provides commands for schema management:
introspect- Inspect live database schema and display structurecompare-db-models- Compare live database against SQLAlchemy models to identify discrepanciescompare-models-doc- Compare SQLAlchemy models against.roo/docs/database_schema.mdgenerate-docs- Generate/update.roo/docs/database_schema.mdfrom current database statereport- Generate discrepancy reports under.roo/reports/(JSON and Markdown)validate- Verify schema documentation is up-to-date with the database
Usage notes:
- This utility uses the configured database URL (from
.env) and initializesdbfromutils_db/database.py. - Some schema comparison utilities may emit reports under
.roo/reports/when run.
Usage examples:
- Introspect schema:
python utils_db/schema_inspector.py introspect - Compare:
python utils_db/schema_inspector.py compare-db-models - Compare models vs docs:
python utils_db/schema_inspector.py compare-models-doc - Generate docs:
python utils_db/schema_inspector.py generate-docs - Validate docs:
python utils_db/schema_inspector.py validate
Testing
- Run tests with
pytestagainst the live PostgreSQL database. - In
TestingConfig(config.py), CSRF is disabled (WTF_CSRF_ENABLED = False).