Skip to content
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
30 changes: 25 additions & 5 deletions knox/migrations/0003_auto_20150916_1526.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
from django.db import migrations, models


def populate_salt_field_with_dummy_data(apps, schema_editor):
Copy link
Author

Choose a reason for hiding this comment

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

This here is a function that when reverting the salt field migration and it gets created, this function populates it with just the index of the row so that it is not null

Copy link
Collaborator

@johnraz johnraz Feb 22, 2025

Choose a reason for hiding this comment

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

The function is never used in the file and and you are modifying a migration that most users of the lib will have run already, meaning they will never benefit from this code.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, yes forgot to use it, I just updated the code. this code is meant to run when they try to undo the migrations not when running from the beginning, they will benifit from this code when they try to run "manage.py migrate knox zero" as shown in issue #368

"""we just populate salt field with index to make it unique"""

AuthToken = apps.get_model("knox", "AuthToken")
for index, token in enumerate(AuthToken.objects.all()):
token.salt = index
token.save(update_fields=["salt"])


def delete_all_rows(apps, schema_editor):
Copy link
Author

Choose a reason for hiding this comment

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

Since the digest field is the primary key of the tokens it is read only and I cannot override it, so when the reverse migrations happen here it transform the max_length property on the digest field from 128 to 64, which will trigger a integrity error when it finds inside the cell a values that exceeds 64 in length. so the only way it runs smoothly is to delete all rows

Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment as above

"""Since digest field is the pk best way to revert migrations is just to delete all Auth token rows"""

AuthToken = apps.get_model("knox", "AuthToken")
AuthToken.objects.using(schema_editor.connection.alias).all().delete()


class Migration(migrations.Migration):

dependencies = [
Expand All @@ -12,13 +28,17 @@ class Migration(migrations.Migration):

operations = [
migrations.AlterField(
model_name='authtoken',
name='digest',
model_name="authtoken",
name="digest",
field=models.CharField(primary_key=True, serialize=False, max_length=128),
),
migrations.RunPython(migrations.RunPython.noop, delete_all_rows),
migrations.AlterField(
model_name='authtoken',
name='salt',
field=models.CharField(unique=True, max_length=16),
model_name="authtoken",
name="salt",
field=models.CharField(max_length=16, null=True),
Copy link
Author

Choose a reason for hiding this comment

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

The salt field is deleted, when the migration is reverted it looks for the field in migration files and recreates it, I don't want the field to be recreated with unique=True because when it gets all populated with null it will also trigger an Integrity error, so I removed unique=True and added null=True so it defaults to null without a problem

),
migrations.RunPython(
migrations.RunPython.noop, populate_salt_field_with_dummy_data
),
]
13 changes: 13 additions & 0 deletions knox/migrations/0009_extend_authtoken_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
from django.db import migrations, models


def clear_token_keys(apps, schema_editor):
Copy link
Author

Choose a reason for hiding this comment

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

Same thing here the token_key field max_length property gets shrinked in reversing the migrations from 25 to 8 which trigger Integrity Error, so i clear it in all rows.

"""
Clears all token values so that when reverting migration
and the field token_key max_length gets changed from 25 to 8 it doesn't trigger DataError
"""

AuthToken = apps.get_model("knox", "AuthToken")
AuthToken.objects.using(schema_editor.connection.alias).update(
token_key=""
) # Clears all token values


class Migration(migrations.Migration):

dependencies = [
Expand All @@ -15,4 +27,5 @@ class Migration(migrations.Migration):
name="token_key",
field=models.CharField(db_index=True, max_length=25),
),
migrations.RunPython(migrations.RunPython.noop, clear_token_keys),
]