Repository navigation
Db json result - #9907
Db json result#9907vladborovtsov wants to merge 4 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #9907 +/- ##
==========================================
+ Coverage 78.66% 78.69% +0.02%
==========================================
Files 153 153
Lines 19291 19317 +26
Branches 2568 2568
==========================================
+ Hits 15176 15202 +26
Misses 3816 3816
Partials 299 299
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new JSON-based database backend for storing Celery task results, designed to work with databases supporting JSON fields (MSSQL, MySQL, Postgres). The implementation provides an alternative to the pickle-based storage approach.
Key changes:
- Creates JSON variants of existing database models with JSON field types instead of PickleType
- Adds a new JSONDatabaseBackend class that uses the JSON models
- Registers the new backend with the "db-json" URL scheme
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| celery/backends/database/models.py | Refactors existing models into base classes and creates JSON variants (TaskJSON, TaskExtendedJSON, TaskSetJSON) |
| celery/backends/database/init.py | Adds JSONDatabaseBackend class and imports for JSON model classes |
| celery/app/backends.py | Registers the new "db-json" backend scheme |
| id = sa.Column(DialectSpecificInteger, | ||
| sa.Sequence('task_id_sequence_json'), | ||
| primary_key=True, autoincrement=True) |
There was a problem hiding this comment.
The TaskJSON class redefines the id column that's already defined in TaskBase. This creates potential confusion and maintenance issues. Should TaskJSON inherit the id column from TaskBase instead of redefining it?
| id = sa.Column(DialectSpecificInteger, | |
| sa.Sequence('task_id_sequence_json'), | |
| primary_key=True, autoincrement=True) |
| class TaskExtendedJSON(TaskBase): | ||
| """Extended task with JSON result.""" | ||
|
|
||
| __tablename__ = 'celery_taskmeta_json' | ||
| __table_args__ = {'sqlite_autoincrement': True, 'extend_existing': True} | ||
|
|
||
| id = sa.Column(DialectSpecificInteger, | ||
| sa.Sequence('task_id_sequence_json'), | ||
| primary_key=True, autoincrement=True) | ||
| result = sa.Column(sa_JSON, nullable=True) | ||
|
|
||
|
|
There was a problem hiding this comment.
TaskExtendedJSON shares the same table name 'celery_taskmeta_json' as TaskJSON but uses 'extend_existing': True. This could lead to schema conflicts or unexpected behavior. Should these classes use different table names or share a common inheritance structure?
| class TaskExtendedJSON(TaskBase): | |
| """Extended task with JSON result.""" | |
| __tablename__ = 'celery_taskmeta_json' | |
| __table_args__ = {'sqlite_autoincrement': True, 'extend_existing': True} | |
| id = sa.Column(DialectSpecificInteger, | |
| sa.Sequence('task_id_sequence_json'), | |
| primary_key=True, autoincrement=True) | |
| result = sa.Column(sa_JSON, nullable=True) | |
| class TaskExtendedJSON(TaskJSON): | |
| """Extended task with JSON result.""" | |
| # Add any additional methods or properties here if needed. |
| id = sa.Column(DialectSpecificInteger, | ||
| sa.Sequence('taskset_id_sequence_json'), | ||
| autoincrement=True, primary_key=True) |
There was a problem hiding this comment.
Similar to the Task classes, TaskSetJSON redefines the id column that's already in TaskSetBase. This pattern creates duplicated column definitions across the codebase. Should the JSON classes inherit the id column from their base classes?
Introduce schema_extensions argument to DatabaseBackend, allowing users to modify Task/TaskSet table definitions in a clean, composable way. Extensions implement SchemaExtension and can adjust column types, add indexes, or other schema-level changes. This is a backwards-compatible alternative to monkey-patching, and a more flexible approach than PR celery#9907. Related to issue celery#9501.
|
Closing this in favor of #9908. Extensions approach looks definitely better! |
Introduce schema_extensions argument to DatabaseBackend, allowing users to modify Task/TaskSet table definitions in a clean, composable way. Extensions implement SchemaExtension and can adjust column types, add indexes, or other schema-level changes. This is a backwards-compatible alternative to monkey-patching, and a more flexible approach than PR celery#9907. Related to issue celery#9501.
Description
Not sure if it has to be considered as draft.
This change introduces a new database backend that's supposed to store results in JSON field. The main idea behind this is to have it in the codebase, avoid fragile monkey patching.
Usage is simple:
Use "db-json" prefix instead of just "db".
The change is mainly for MSSQL, though it must work with modern MySQL as well as with Postgres, wherever SQLAlchemy supports the JSON field.
Perhaps, some tests still need to be written to cover the new backend. Though, it seems to be functional now. Will be happy to commit more time into this, but happy to see some feedback.
Related issues: