Coverage for documents/management/commands/convert_mariadb_uuid.py: 0%

20 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 09:07 +0000

1from django.core.management.base import BaseCommand 

2from django.db import connection 

3from django.db import models 

4 

5from documents.models import Document 

6 

7 

8class Command(BaseCommand): 

9 # This code is taken almost entirely from https://github.com/wagtail/wagtail/pull/11912 with all credit to the original author. 

10 help = "Converts UUID columns from char type to the native UUID type used in MariaDB 10.7+ and Django 5.0+." 

11 

12 def convert_field(self, model, field_name, *, null=False) -> None: 

13 if model._meta.get_field(field_name).model != model: # pragma: no cover 

14 # Field is inherited from a parent model 

15 return 

16 

17 if not model._meta.managed: # pragma: no cover 

18 # The migration framework skips unmanaged models, so we should too 

19 return 

20 

21 old_field = models.CharField(null=null, max_length=36) 

22 old_field.set_attributes_from_name(field_name) 

23 

24 new_field = models.UUIDField(null=null) 

25 new_field.set_attributes_from_name(field_name) 

26 

27 with connection.schema_editor() as schema_editor: 

28 schema_editor.alter_field(model, old_field, new_field) 

29 self.stdout.write( 

30 self.style.SUCCESS( 

31 f"Successfully converted {model._meta.label} {field_name} field to UUID type.", 

32 ), 

33 ) 

34 

35 def handle(self, **options): 

36 self.convert_field(Document, "transaction_id", null=True)