Skip to content

Commit 08c56f5

Browse files
committed
add a forward migration function that computes checksum for existing tokens
1 parent f220235 commit 08c56f5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

oauth2_provider/migrations/0012_add_token_checksum.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
from django.db import migrations, models
55
from oauth2_provider.settings import oauth2_settings
66

7+
def forwards_func(apps, schema_editor):
8+
"""
9+
Forward migration touches every "old" accesstoken.token which will cause the checksum to be computed.
10+
"""
11+
AccessToken = apps.get_model(oauth2_settings.ACCESS_TOKEN_MODEL)
12+
accesstokens = AccessToken._default_manager.all()
13+
for accesstoken in accesstokens:
14+
accesstoken.save(update_fields=['token_checksum'])
15+
16+
717
class Migration(migrations.Migration):
818
dependencies = [
919
("oauth2_provider", "0011_refreshtoken_token_family"),
@@ -15,12 +25,13 @@ class Migration(migrations.Migration):
1525
model_name="accesstoken",
1626
name="token_checksum",
1727
field=oauth2_provider.models.TokenChecksumField(
18-
blank=True, db_index=True, max_length=64, unique=True
28+
blank=True, null=True, db_index=True, max_length=64, unique=True
1929
),
2030
),
2131
migrations.AlterField(
2232
model_name="accesstoken",
2333
name="token",
2434
field=models.TextField(),
2535
),
36+
migrations.RunPython(forwards_func),
2637
]

0 commit comments

Comments
 (0)