Migrate legacy `jobs` table datetime columns to timestamptz on PostgreSQL
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
Overview
On PostgreSQL instances upgraded from the old SQLAlchemy job store, the jobs table's datetime columns are still timestamp without time zone, so enqueue_lifo() reads a naive scheduled_time and raises ValueError: Must use a timezone aware datetime object. Add a tasks migration converting those columns to timestamptz.
Complexity: Low Target branch: develop
Context
kolibri/core/tasks/migrations/0001_initial.py uses CreateModelIfNotExists, so where the jobs table predates the Django tasks app (SQLAlchemy era), its legacy naive columns are never converted.
enqueue_lifo() is the only enqueue path that derives its schedule time from a stored value (dt = soonest_job.scheduled_time - timedelta(microseconds=1)); the others use self._now(), always aware. So a naive column only bites here, and only once a job is already QUEUED. SQLite is unaffected (its converter forces UTC awareness from USE_TZ); fresh Postgres installs get a timestamptz column.
File "kolibri/core/tasks/storage.py", line 737, in schedule
raise ValueError("Must use a timezone aware datetime object for scheduling tasks")
The Change
Add a tasks migration that converts the jobs table's datetime columns (scheduled_time, time_created, time_updated) to timestamp with time zone on PostgreSQL, interpreting existing values as UTC (how they were written).
Out of Scope
- Changing
enqueue_lifo()/schedule()logic — this is purely a schema fix. - The
CreateModelIfNotExistsbehaviour in0001_initial.
Acceptance Criteria
General
- [ ] After migration on PostgreSQL,
jobs.scheduled_time,jobs.time_created, andjobs.time_updatedaretimestamp with time zone. - [ ] Existing column values are preserved, interpreted as UTC.
- [ ] The migration is a no-op when the columns are already tz-aware and on non-PostgreSQL backends.
- [ ]
enqueue_lifo()does not raise on an upgraded PostgreSQL deployment that already has aQUEUEDjob.
Testing
- [ ] A regression test (PostgreSQL only) covers the legacy naive-column condition.
Testing
Use the existing migration-test harness — TestMigrations in kolibri/core/auth/test/migrationtestcase.py (set migrate_from/migrate_to and use setUpBeforeMigration); kolibri/core/content/test/test_data_migrations.py shows usage. Recreate the legacy schema in setUpBeforeMigration by ALTER-ing the jobs datetime columns to timestamp without time zone. The test applies to PostgreSQL only — the naive-column condition cannot occur on SQLite — so gate it on connection.vendor == "postgresql".
References
kolibri/core/tasks/migrations/0001_initial.py— theCreateModelIfNotExistsoperation that skips the legacy table.kolibri/core/tasks/storage.py—enqueue_lifo()/schedule().- Surfaced downstream in Kolibri Data Portal:
learningequality/kolibri-data-portal#1100(a local stopgap migration was applied there).
AI usage
I used Claude Code to investigate the Sentry error, reproduce it against PostgreSQL, and draft this issue. The root cause was confirmed by reproducing the exact ValueError with a simulated legacy naive column; I reviewed and edited the writeup and verified the claims against the code.
The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.
This is not closed by the KDP PR!
The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.
The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.