Skip to content
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

Make app AutoField configurable on first deployment #465

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
TOXENV: py-django${{ matrix.django }}

rest:
name: Integration/Coverage/Docs/Codestyle
name: ${{ matrix.toxenv }}
runs-on: blacksmith-4vcpu-ubuntu-2204
strategy:
matrix:
Expand Down
4 changes: 3 additions & 1 deletion django_celery_results/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

__all__ = ['CeleryResultConfig']

from django_celery_results.conf import app_settings


class CeleryResultConfig(AppConfig):
"""Default configuration for the django_celery_results app."""

name = 'django_celery_results'
label = 'django_celery_results'
verbose_name = _('Celery Results')
default_auto_field = 'django.db.models.AutoField'
default_auto_field = app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD
43 changes: 43 additions & 0 deletions django_celery_results/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Application settings."""
from __future__ import annotations

from dataclasses import dataclass
from typing import Any

from django.conf import settings as django_settings

# All attributes accessed with this prefix are possible
# to overwrite through django.conf.settings.
SETTINGS_PREFIX = "DJANGO_CELERY_RESULTS_"


@dataclass(frozen=True)
class AppSettings:
"""Proxy class to encapsulate all the app settings.

This instance should be accessed via the singleton
``django_celery_results.conf.app_settings``.

You shouldn't have to set any of these yourself, the class checks a Django
settings with the same name and use these if defined, defaulting to the
values documented here.
"""

DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

def __getattribute__(self, __name: str) -> Any:
"""Check if a Django project settings should override the app default.

In order to avoid returning any random properties of the Django
settings, we first inspect the prefix.
"""
if (
__name.startswith(SETTINGS_PREFIX)
and hasattr(django_settings, __name)
):
return getattr(django_settings, __name)

return super().__getattribute__(__name)


app_settings = AppSettings()
8 changes: 7 additions & 1 deletion django_celery_results/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from django.conf import settings
from django.db import migrations, models
from django.utils.module_loading import import_string

from django_celery_results.conf import app_settings

auto_field_class = import_string(
app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD)


class Migration(migrations.Migration):
Expand All @@ -13,7 +19,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='TaskResult',
fields=[
('id', models.AutoField(auto_created=True,
('id', auto_field_class(auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID')),
Expand Down
8 changes: 7 additions & 1 deletion django_celery_results/migrations/0008_chordcounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from django.conf import settings
from django.db import migrations, models
from django.utils.module_loading import import_string

from django_celery_results.conf import app_settings

auto_field_class = import_string(
app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD)


class Migration(migrations.Migration):
Expand All @@ -14,7 +20,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='ChordCounter',
fields=[
('id', models.AutoField(
('id', auto_field_class(
auto_created=True,
primary_key=True,
serialize=False,
Expand Down
8 changes: 7 additions & 1 deletion django_celery_results/migrations/0009_groupresult.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Generated by Django 3.2 on 2021-04-19 14:55
from django.conf import settings
from django.db import migrations, models
from django.utils.module_loading import import_string

from django_celery_results.conf import app_settings

auto_field_class = import_string(
app_settings.DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD)


class FakeAddIndex(migrations.AddIndex):
Expand Down Expand Up @@ -28,7 +34,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='GroupResult',
fields=[
('id', models.AutoField(
('id', auto_field_class(
auto_created=True,
primary_key=True,
serialize=False,
Expand Down
19 changes: 19 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Configuration
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is another setting DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH which should probably be added here but I'd rather do this separately if you don't mind.

=============

These are the available settings that can be configured in your Django
project's settings module by defining a setting with the same name.

* ``DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD``: The ``default_auto_field`` used
when first deploying the app, defaults to ``django.db.models.BigAutoField``
if unspecified.

This is only used for the first deployment and
and has no effect if changed after this point. If you want to change its
value after the initial deployment, you should migrate back to zero and
re-apply migrations again::

$ python manage.py migrate django_celery_results zero
$ python manage.py migrate django_celery_results

This will drop and recreate all tables used by django-celery-results.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Contents

getting_started
injecting_metadata
configuration
copyright

.. toctree::
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ markers =
[flake8]
# classes can be lowercase, arguments and variables can be uppercase
# whenever it makes the code more readable.
ignore = N806, N802, N801, N803
ignore = N806, N802, N801, N803, W503

[pep257]
convention=google
Expand Down
1 change: 1 addition & 0 deletions t/proj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@

USE_TZ = True
DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH = 191
DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Static files (CSS, JavaScript, Images)
Expand Down
5 changes: 5 additions & 0 deletions t/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def test_taskmeta(self, ctype='application/json', cenc='utf-8'):
)
assert m1 not in TaskResult.objects.all()

def test_task_result_pk_use_big_auto_field(self):
task_result = TaskResult.objects.create(
pk=5_000_000_000, task_id=uuid())
assert task_result.pk is not None

def test_store_result(self, ctype='application/json', cenc='utf-8'):
"""
Test the `using` argument.
Expand Down