-
Notifications
You must be signed in to change notification settings - Fork 217
Replace texfields with jsonfield #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mumarkhan999
wants to merge
2
commits into
celery:main
Choose a base branch
from
mumarkhan999:umar/replace-textfiled-with-jsonfield
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...celery_results/migrations/0015_chordcounter_new_sub_tasks_taskresult_new_meta_and_more.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Generated by Django 5.2.4 on 2025-07-09 09:20 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_celery_results', '0014_alter_taskresult_status'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='chordcounter', | ||
name='new_sub_tasks', | ||
field=models.JSONField(default=None, help_text='JSON serialized list of task result tuples. use .group_result() to decode'), | ||
), | ||
migrations.AddField( | ||
model_name='taskresult', | ||
name='new_meta', | ||
field=models.JSONField(default=None, editable=False, help_text='JSON meta information about the task, such as information on child tasks', null=True, verbose_name='Task Meta Information'), | ||
), | ||
migrations.AddField( | ||
model_name='taskresult', | ||
name='new_task_args', | ||
field=models.JSONField(help_text='JSON representation of the positional arguments used with the task', null=True, verbose_name='Task Positional Arguments'), | ||
), | ||
migrations.AddField( | ||
model_name='taskresult', | ||
name='new_task_kwargs', | ||
field=models.JSONField(help_text='JSON representation of the named arguments used with the task', null=True, verbose_name='Task Named Arguments'), | ||
), | ||
] |
81 changes: 81 additions & 0 deletions
81
django_celery_results/migrations/0016_make_copy_of_taskresult_textfields.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import json | ||
import logging | ||
|
||
from django.db import migrations, transaction | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def safe_json_loads(value, default=None): | ||
"""Safely parse JSON string with fallback.""" | ||
if not value: # Handles None, empty string, etc. | ||
return default | ||
return json.loads(value) | ||
|
||
|
||
def make_copy_of_taskresult_textfields(apps, schema_editor): | ||
TaskResult = apps.get_model('django_celery_results', 'TaskResult') | ||
|
||
total_count = TaskResult.objects.count() | ||
logger.info(f"Starting migration for {total_count} TaskResult records") | ||
|
||
batch_size = 500 | ||
processed_count = 0 | ||
error_count = 0 | ||
last_id = 0 | ||
|
||
while True: | ||
with transaction.atomic(): | ||
# Get next batch using cursor pagination | ||
batch = list( | ||
TaskResult.objects.filter(id__gt=last_id) | ||
.order_by('id')[:batch_size] | ||
) | ||
|
||
if not batch: | ||
break | ||
|
||
updates = [] | ||
|
||
for obj in batch: | ||
try: | ||
# Parse JSON fields with appropriate defaults | ||
obj.new_task_args = safe_json_loads(obj.task_args) | ||
obj.new_task_kwargs = safe_json_loads(obj.task_kwargs) | ||
obj.new_meta = safe_json_loads(obj.meta) | ||
|
||
updates.append(obj) | ||
|
||
except Exception as e: | ||
error_count += 1 | ||
logger.error(f"Error processing TaskResult ID {obj.id}: {e}") | ||
continue | ||
|
||
if updates: | ||
TaskResult.objects.bulk_update( | ||
updates, | ||
['new_task_args', 'new_task_kwargs', 'new_meta'] | ||
) | ||
processed_count += len(updates) | ||
|
||
last_id = batch[-1].id | ||
|
||
# Progress logging | ||
progress = (processed_count / total_count) * 100 if total_count > 0 else 0 | ||
logger.info(f"Processed {processed_count}/{total_count} records ({progress:.1f}%)") | ||
|
||
logger.info(f"Migration completed. Successfully processed {processed_count} records, " | ||
f"{error_count} errors encountered") | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_celery_results', '0015_chordcounter_new_sub_tasks_taskresult_new_meta_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
make_copy_of_taskresult_textfields, | ||
migrations.RunPython.noop | ||
) | ||
] |
79 changes: 79 additions & 0 deletions
79
django_celery_results/migrations/0017_make_copy_of_chordcounter_textfields.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import json | ||
import logging | ||
|
||
from django.db import migrations, transaction | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def safe_json_loads(value, default=None): | ||
"""Safely parse JSON string with fallback.""" | ||
if not value: # Handles None, empty string, etc. | ||
return default | ||
return json.loads(value) | ||
|
||
|
||
def make_copy_of_chordcounter_textfields(apps, schema_editor): | ||
chord_counter = apps.get_model('django_celery_results', 'ChordCounter') | ||
|
||
total_count = chord_counter.objects.count() | ||
logger.info(f"Starting migration for {total_count} ChordCounter records") | ||
|
||
batch_size = 500 | ||
processed_count = 0 | ||
error_count = 0 | ||
last_id = 0 | ||
|
||
while True: | ||
with transaction.atomic(): | ||
# Get next batch using cursor pagination | ||
batch = list( | ||
chord_counter.objects.filter(id__gt=last_id) | ||
.order_by('id')[:batch_size] | ||
) | ||
|
||
if not batch: | ||
break | ||
|
||
updates = [] | ||
|
||
for obj in batch: | ||
try: | ||
# Parse JSON fields with appropriate defaults | ||
obj.new_sub_tasks = safe_json_loads(obj.sub_tasks) | ||
updates.append(obj) | ||
|
||
except Exception as e: | ||
error_count += 1 | ||
logger.error(f"Error processing ChordCounter ID {obj.id}: {e}") | ||
continue | ||
|
||
if updates: | ||
chord_counter.objects.bulk_update( | ||
updates, | ||
['new_sub_tasks'] | ||
) | ||
processed_count += len(updates) | ||
|
||
last_id = batch[-1].id | ||
|
||
# Progress logging | ||
progress = (processed_count / total_count) * 100 if total_count > 0 else 0 | ||
logger.info(f"Processed {processed_count}/{total_count} records ({progress:.1f}%)") | ||
|
||
logger.info(f"Migration completed. Successfully processed {processed_count} records, " | ||
f"{error_count} errors encountered") | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_celery_results', '0016_make_copy_of_taskresult_textfields'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
make_copy_of_chordcounter_textfields, | ||
migrations.RunPython.noop | ||
) | ||
] |
50 changes: 50 additions & 0 deletions
50
django_celery_results/migrations/0018_remove_chordcounter_new_sub_tasks_and_more.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_celery_results', '0017_make_copy_of_chordcounter_textfields'), | ||
] | ||
|
||
operations = [ | ||
# Remove the old fields | ||
migrations.RemoveField( | ||
model_name='chordcounter', | ||
name='sub_tasks', | ||
), | ||
migrations.RemoveField( | ||
model_name='taskresult', | ||
name='meta', | ||
), | ||
migrations.RemoveField( | ||
model_name='taskresult', | ||
name='task_args', | ||
), | ||
migrations.RemoveField( | ||
model_name='taskresult', | ||
name='task_kwargs', | ||
), | ||
|
||
# Rename the new_ fields to their non-prefixed versions | ||
migrations.RenameField( | ||
model_name='chordcounter', | ||
old_name='new_sub_tasks', | ||
new_name='sub_tasks', | ||
), | ||
migrations.RenameField( | ||
model_name='taskresult', | ||
old_name='new_meta', | ||
new_name='meta', | ||
), | ||
migrations.RenameField( | ||
model_name='taskresult', | ||
old_name='new_task_args', | ||
new_name='task_args', | ||
), | ||
migrations.RenameField( | ||
model_name='taskresult', | ||
old_name='new_task_kwargs', | ||
new_name='task_kwargs', | ||
), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new JSONField for
sub_tasks
sets a default ofNone
withoutnull=True
, which may cause database constraint errors when existing records are null. Consider addingnull=True
or providing a valid default (e.g., an empty list) to maintain compatibility.Copilot uses AI. Check for mistakes.