Enforce SQLAlchemy-only database connections and Alembic-only schema changes
Les mainteneurs répondent en général sous 3 jours
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
Piste de recherche
Start with the existing rules registry and pytest guards, then inspect policyengine_api/data/v2/catalog/publication_staging.py to understand the two stated exceptions. Add a guard covering the specified code paths, proof fixtures, CI wiring in the existing test job, and guidance in docs/engineering/skills/alembic-migrations.md. Done means the guard rejects direct relational driver use and non-Alembic persistent schema changes, accepts only the exact exceptions, and its tests run in CI. Pull request #3979 is already open.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Problem
The Public API opens every relational database connection through SQLAlchemy today: API v1 builds its Cloud SQL engine in policyengine_api/data/orm.py (the Cloud SQL connector is passed to create_engine as creator=), and API v2 builds its SQLModel engine from a postgresql+psycopg URL in policyengine_api/data/v2/database.py. Schema changes go through the autogenerated Alembic revisions under migrations/v1 and migrations/v2.
Nothing enforces either of these. The rules registry has related rules, but none of them forbids:
- a direct driver connection such as
psycopg.connect(...),pymysql.connect(...)orsqlite3.connect(...), or use of the driver connection beneath SQLAlchemy, in application code, scripts or CI code.api.data.runtime-sql-confinedonly confines raw SQL to the data-access layer, andapi.services.no-direct-queriesonly covers services; - a schema change outside Alembic, such as
metadata.create_all(engine),Table.create(...),op.*outsidemigrations/, orCREATE/ALTER/DROP/TRUNCATESQL text.api.migration.alembic-autogenerated-onlyis doc-only, andapi.migration.alembic-v1-qualified/api.migration.alembic-v2-qualifiedcheck the revisions, not the code around them.
Both have happened before: the Stage 12 provisioning script (removed in d8bd72191) opened psycopg connections directly, and the local database bootstrap added in 6c7804100 called V1Base.metadata.create_all at runtime.
Proposed rule
Register an enforced rule, api.data.sqlalchemy-and-alembic-only: code connects to relational databases only through SQLAlchemy engines, connections and sessions, never by calling a database driver directly, and changes persistent schema only through Alembic revisions.
- Enforce it with a pytest guard that parses non-test code (
policyengine_api/,scripts/,migrations/,.github/, excluding tests and the generated revisions undermigrations/*/versions/). - Declare the two current exceptions exactly, both in
policyengine_api/data/v2/catalog/publication_staging.py:COPY ... FROM STDINthrough the psycopg connection beneath a SQLAlchemyConnection, because SQLAlchemy has no COPY API;STAGING_METADATA.create_allforTEMPORARYtables that Postgres drops at commit, which are not persistent schema.
- The guard should fail if either exception grows or is no longer needed.
- Redis (
policyengine_api/runtime_cache/) is a key-value cache, not a relational database, and is out of scope. - Add proof fixtures, CI wiring through the existing
testjob, and a section indocs/engineering/skills/alembic-migrations.md.
- Langage dominant
- Python
- Étoiles
- 18
- Forks
- 33
- Merge moyen
- 2 j 15 h
- PR mergées (30 j)
- 35
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de PolicyEngine/policyengine-api
-
Difficulté 1/5 1-3 heures Accessibilité débutants 88/100
PolicyEngine/policyengine-api#3911 ·
Les mainteneurs répondent en général sous 3 jours
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
PolicyEngine/policyengine-api#3910 ·
Les mainteneurs répondent en général sous 3 jours
-
Let a saved reform or household point at a new v2 policy or household without changing its IDPeut-être pris @anth-volk l’a pris aujourd’hui. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
PolicyEngine/policyengine-api#4004 ·
Les mainteneurs répondent en général sous 3 jours
-
Releases do not check that each environment calls its own simulation servicePeut-être pris @anth-volk l’a pris aujourd’hui. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 46/100
PolicyEngine/policyengine-api#4002 ·
Les mainteneurs répondent en général sous 3 jours
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 18/100
PolicyEngine/policyengine-api#4000 ·
Les mainteneurs répondent en général sous 3 jours
Toutes les issues de PolicyEngine/policyengine-api
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
developmentseed/titiler-cmr#250 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
[Bug]: IndexHandler.post crashes with HTTP 500 on malformed payloads, missing credentials, or when login is disabledPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 6 jours
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
cookiecutter/cookiecutter-django#6999 ·
Les mainteneurs répondent en général sous 1 jour
-
fix: @_helpers.positional raises TypeError when decorating functions without default argumentsPeut-être pris @codeXsidd l’a pris aujourd’hui. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
googleapis/google-api-python-client#2845 ·
Les mainteneurs répondent en général sous 1 jour
-
Bump zizmor to 1.31.1+Ouvertegithub_actions
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
collective/icalendar#2003 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour