-
-
Notifications
You must be signed in to change notification settings - Fork 223
Fix migrations so that migrate knox zero is possible #379
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,22 @@ | |
| from django.db import migrations, models | ||
|
|
||
|
|
||
| def populate_salt_field_with_dummy_data(apps, schema_editor): | ||
| """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): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [ | ||
|
|
@@ -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), | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ), | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,18 @@ | |
| from django.db import migrations, models | ||
|
|
||
|
|
||
| def clear_token_keys(apps, schema_editor): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [ | ||
|
|
@@ -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), | ||
| ] | ||
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.
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
Uh oh!
There was an error while loading. Please reload this page.
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 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.
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.
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