Repository navigation
feat(task-queue): PLTF-3689 add Procrastinate and its database schema - #587
Conversation
|
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
bf86e6e to
dfa97bc
Compare
c591712 to
c27fe11
Compare
…evision 174) Adds psycopg[binary,pool] and procrastinate==3.10.0. Revision 174 runs the literal output of 'procrastinate schema --read' at 3.10.0, vendored under migrations/procrastinate/, on the raw driver cursor. It never asks the installed library for a schema, so the revision stays stable across future Procrastinate upgrades, which vendor pre/post files as new revisions. TASK_QUEUE_DB_ROLE, when set, is granted DML on the queue tables only, so workers cannot change the schema. procrastinate 3.10.0 is younger than the repo's 7-day exclude-newer cooldown; a dated exclude-newer-package entry admits exactly that release and can be dropped once it ages past the window. OHE-3417 (Track 1, T1.3) Co-authored-by: openhands <openhands@all-hands.dev>
c27fe11 to
f8db211
Compare
Mutation review of the migration 174 testsI broke Caught: all four controls (reverting what the PR claims) and M4
The schema side is solid. Survivors
M1: the injection test passes even without the guard
(I tried M2 / M3 / M5 / M6: nothing pins what the worker role is actually grantedThe PR says the role gets "read and write access to the queue tables but no permission to change the schema". The tests check the lower bound (a normal job runs) and a DDL denylist. Nothing checks the upper bound. Granting the worker DML on every table in A snapshot of the role's grants closes all four: @@ class TestWorkerRole:
+ def test_worker_role_privileges_are_exactly_queue_dml(self, worker_conninfo: str):
+ with psycopg.connect(worker_conninfo) as conn:
+ table_grants = set(
+ conn.execute(
+ 'SELECT table_name, privilege_type'
+ ' FROM information_schema.role_table_grants'
+ ' WHERE grantee = current_user'
+ ).fetchall()
+ )
+ sequences = {
+ name
+ for (name,) in conn.execute(
+ 'SELECT object_name FROM information_schema.usage_privileges'
+ " WHERE grantee = current_user AND object_type = 'SEQUENCE'"
+ )
+ }
+ assert table_grants == {
+ (table, privilege)
+ for table in (
+ 'procrastinate_workers',
+ 'procrastinate_jobs',
+ 'procrastinate_periodic_defers',
+ 'procrastinate_events',
+ )
+ for privilege in ('SELECT', 'INSERT', 'UPDATE', 'DELETE')
+ }
+ assert sequences == {
+ 'procrastinate_jobs_id_seq',
+ 'procrastinate_periodic_defers_id_seq',
+ 'procrastinate_events_id_seq',
+ 'procrastinate_workers_id_seq',
+ }
+
def test_rejects_role_name_that_is_not_an_identifier(
- self, test_database: postgres_testdb.TestDatabase
+ self, test_database: postgres_testdb.TestDatabase, worker_role
):
+ role, _ = worker_role
postgres_testdb.run_alembic(
test_database.server, test_database.name, 'downgrade', '173'
)
@@
- extra_env={'TASK_QUEUE_DB_ROLE': 'x; DROP TABLE org'},
+ extra_env={'TASK_QUEUE_DB_ROLE': f'{role}, public'},
)Verified: with this change the file passes on the unmodified branch (7 passed), and M1, M2, M3, M5 and M6 are each killed. M4 is still killed too. M7: low priority, no fix proposedWithout Not a test gap
This comment was generated by an AI assistant on behalf of the user. |
Move the worker role's DML grants out of revision 174 into migrations/env.py, where they run after every migration run. A role created after 174 was applied, and tables or sequences added by later Procrastinate revisions, now get their grants on the next run. Rename TASK_QUEUE_DB_ROLE to TASK_QUEUE_DB_USER, the name the worker connects as, and quote the role instead of restricting it to plain identifiers. Co-authored-by: openhands <openhands@all-hands.dev>
|
Follow-up on the mutation review. That review ran against
I'm deferring every open item on purpose. All of them are in the
This comment was generated by an AI agent (OpenHands) on behalf of the user. |
The worker process runs the job bodies in-process, and those use the app engine and DB_USER, so the process holds full privileges regardless. A DML-only role on the queue connection alongside it is not a privilege boundary, and nothing in the chart, KOTS or saas-deploy ever set TASK_QUEUE_DB_USER, so the grant never ran. Remove the grant from migrations/env.py, which reverts that file to main, along with the role's tests and the test-only extra_env hook. Co-authored-by: openhands <openhands@all-hands.dev>
Adds the imports the test already relies on (procrastinate, psycopg, psycopg.conninfo, uuid, collections.abc.Iterator) to fix ruff F821 undefined-name failures in the Lint python check. Co-authored-by: openhands <openhands@all-hands.dev>
|
🚀 Released in 1.68.0. |
HUMAN:
AGENT:
Why
The task queue needs its Postgres schema before any worker can run. This adds Procrastinate and migration 174, which creates its four tables, functions, triggers and types on every install. Nothing enqueues or runs jobs yet, so the new tables sit empty until a worker is added.
Migration 174 runs a copy of the SQL that Procrastinate 3.10.0 ships, checked into the repo, instead of asking the installed library for its schema. That way the migration builds the same schema on every future install, whatever Procrastinate version is installed by then, and a later upgrade adds its changes as new migrations.
Summary
procrastinate==3.10.0andpsycopg[binary,pool](psycopg 3, which Procrastinate's async connector needs) to the project dependencies.migrations/procrastinate/03.10.00_schema.sql.TASK_QUEUE_DB_USER(the role the worker connects as) is set,migrations/env.pygrants it read and write access to everyprocrastinate_*table and sequence, but no permission to change the schema. It does this after every migration run, not inside revision 174, so a role created after 174 was applied, or a table added by a later Procrastinate upgrade, is granted on the next run.exclude-newer-packageentry so uv accepts Procrastinate 3.10.0, which is newer than the repo's 7-day cooldown on new releases. The entry can be removed once the release is older than the cooldown.Issue Number
OHE-3417
How to Test
uv run pytest tests/unit/test_migration_174_procrastinate_schema.py(thePython Tests on LinuxCI job also runs it). The tests check that:TASK_QUEUE_DB_USERon a database already at head (so no revision runs) can run a job end to end but gets a permission error on DDL; aprocrastinate_*table added later is granted on the next run; the role name is quoted (the test role's name contains a space and a hyphen); and a role that does not exist fails the migration.Video/Screenshots
N/A. No UI change.
Type
Notes
If
TASK_QUEUE_DB_USERis set, the role must already exist when migrations run, or theGRANTfails and the whole migration run rolls back. When it is unset, nothing is granted.This PR was drafted by an AI agent on behalf of the user.
@aivong-openhands can click here to continue refining the PR
Enterprise server image for this PR: