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

Fix: migrations being generated when settings are altered #37

Merged
merged 2 commits into from
May 3, 2023

Conversation

Caiofcas
Copy link
Contributor

Versions:

$ pip freeze
...
Django==3.2.11
djangorestframework==3.13.1
django-rest-durin==1.1.0
...

There are two settings changes which can generate new migrations inside the installed durin files currently.

The first one is when you change the DEFAULT_AUTO_FIELD setting in django to something other than models.BigAutoField, it can be detected by django as a change in the durin provided models, which can lead to migrations being generated for durin models.

This happens since durin does not explicitly provide an AppConfig class with the default_auto_field property set. See this SO question for another explanation of the problem.

The second is when you change one of the values which is directly referenced in one of the fields default argument (Client.token_ttl field with DEFAULT_TOKEN_TTL). If you change the value of these settings, it's interpreted as a change in the value of the argument, and it generates a migration.

This one can be fixed by changing the arg to reference a function which reads the setting at run time.

Example migration generated inside the durin migrations directory when you change some of this settings:

settings.py:

from datetime import timedelta

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

REST_DURIN = {
    "DEFAULT_TOKEN_TTL": timedelta(hours=6),
}
# Generated by Django 3.2.11 on 2022-11-25 18:02

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('durin', '0002_client_throttlerate'),
    ]

    operations = [
        migrations.AlterField(
            model_name='authtoken',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='client',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='client',
            name='token_ttl',
            field=models.DurationField(default=datetime.timedelta(seconds=21600), help_text='\n            Token Time To Live (TTL) in timedelta. Format: <code>DAYS HH:MM:SS</code>.\n            ', verbose_name='Token Time To Live (TTL)'),
        ),
    ]

@eshaan7
Copy link
Owner

eshaan7 commented May 3, 2023

Thanks for this! The tests will pass in main as the CI file has been fixed.

@eshaan7 eshaan7 merged commit 9560017 into eshaan7:main May 3, 2023
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