Skip to content

Db json result - #9907

Closed
vladborovtsov wants to merge 4 commits into
celery:mainfrom
vladborovtsov:db-json-result
Closed

vladborovtsov wants to merge 4 commits into
celery:mainfrom
vladborovtsov:db-json-result

Conversation

@vladborovtsov

@vladborovtsov vladborovtsov commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

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:

backend_url = (
    f'db-json+mssql+pyodbc://sa:{mssql_password}@localhost:1433/celery'
    '?driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes'
)

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:

@codecov

codecov Bot commented Sep 23, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.03922% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.69%. Comparing base (70087e4) to head (31a908b).
⚠️ Report is 69 commits behind head on main.

Files with missing lines Patch % Lines
celery/backends/database/models.py 97.67% 1 Missing ⚠️
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              
Flag Coverage Δ
unittests 78.67% <98.03%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@auvipy
auvipy requested review from auvipy and Copilot September 24, 2025 04:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +98 to +100
id = sa.Column(DialectSpecificInteger,
sa.Sequence('task_id_sequence_json'),
primary_key=True, autoincrement=True)

Copilot AI Sep 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
id = sa.Column(DialectSpecificInteger,
sa.Sequence('task_id_sequence_json'),
primary_key=True, autoincrement=True)

Copilot uses AI. Check for mistakes.
Comment on lines +132 to +143
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)


Copilot AI Sep 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
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.

Copilot uses AI. Check for mistakes.
Comment on lines +158 to +160
id = sa.Column(DialectSpecificInteger,
sa.Sequence('taskset_id_sequence_json'),
autoincrement=True, primary_key=True)

Copilot AI Sep 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copilot uses AI. Check for mistakes.
dxdc added a commit to dxdc/celery that referenced this pull request Sep 24, 2025
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.
@vladborovtsov

Copy link
Copy Markdown
Contributor Author

Closing this in favor of #9908. Extensions approach looks definitely better!

@vladborovtsov
vladborovtsov deleted the db-json-result branch September 24, 2025 19:45
Nusnus pushed a commit to dxdc/celery that referenced this pull request Oct 4, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants