|
| 1 | +# Generated by Django 6.0.5 on 2026-07-02 13:07 |
| 2 | + |
| 3 | +from django.db import migrations, models |
| 4 | + |
| 5 | + |
| 6 | +def populate_labels_json(apps, schema_editor): |
| 7 | + """ |
| 8 | + Copy the existing django-taggit labels into the new ``labels_json`` field. |
| 9 | + Raw SQL is used against the legacy ``scanpipe_uuidtaggeditem`` and |
| 10 | + ``taggit_tag`` tables since the "taggit" app is no longer installed and has no |
| 11 | + ORM model available in this migration state. |
| 12 | + """ |
| 13 | + Project = apps.get_model("scanpipe", "Project") |
| 14 | + |
| 15 | + with schema_editor.connection.cursor() as cursor: |
| 16 | + existing_tables = schema_editor.connection.introspection.table_names(cursor) |
| 17 | + if "scanpipe_uuidtaggeditem" not in existing_tables: |
| 18 | + return |
| 19 | + |
| 20 | + cursor.execute( |
| 21 | + "SELECT tagged_item.object_id, tag.name " |
| 22 | + "FROM scanpipe_uuidtaggeditem AS tagged_item " |
| 23 | + "JOIN taggit_tag AS tag ON tag.id = tagged_item.tag_id " |
| 24 | + "ORDER BY tag.name" |
| 25 | + ) |
| 26 | + rows = cursor.fetchall() |
| 27 | + |
| 28 | + if not rows: |
| 29 | + return |
| 30 | + |
| 31 | + labels_by_project_id = {} |
| 32 | + for project_id, label in rows: |
| 33 | + labels_by_project_id.setdefault(project_id, []).append(label) |
| 34 | + |
| 35 | + projects = [ |
| 36 | + Project(pk=project_id, labels_json=labels) |
| 37 | + for project_id, labels in labels_by_project_id.items() |
| 38 | + ] |
| 39 | + Project.objects.bulk_update(projects, ["labels_json"], batch_size=1000) |
| 40 | + |
| 41 | + |
| 42 | +def drop_legacy_taggit_tables(apps, schema_editor): |
| 43 | + """Drop the legacy django-taggit tables now that their data has been migrated.""" |
| 44 | + with schema_editor.connection.cursor() as cursor: |
| 45 | + cursor.execute("DROP TABLE IF EXISTS scanpipe_uuidtaggeditem") |
| 46 | + # The default django-taggit through table, unused since Project.labels used |
| 47 | + # the custom UUIDTaggedItem through table, but still created by taggit's own |
| 48 | + # migrations. |
| 49 | + cursor.execute("DROP TABLE IF EXISTS taggit_taggeditem") |
| 50 | + cursor.execute("DROP TABLE IF EXISTS taggit_tag") |
| 51 | + |
| 52 | + |
| 53 | +class Migration(migrations.Migration): |
| 54 | + |
| 55 | + dependencies = [ |
| 56 | + ('scanpipe', '0081_project_count_fields'), |
| 57 | + ] |
| 58 | + |
| 59 | + operations = [ |
| 60 | + migrations.AddField( |
| 61 | + model_name='project', |
| 62 | + name='labels_json', |
| 63 | + field=models.JSONField( |
| 64 | + blank=True, |
| 65 | + default=list, |
| 66 | + help_text='A list of labels for this project.', |
| 67 | + ), |
| 68 | + ), |
| 69 | + migrations.RunPython(populate_labels_json, migrations.RunPython.noop), |
| 70 | + migrations.RunPython(drop_legacy_taggit_tables, migrations.RunPython.noop), |
| 71 | + migrations.RenameField( |
| 72 | + model_name='project', |
| 73 | + old_name='labels_json', |
| 74 | + new_name='labels', |
| 75 | + ), |
| 76 | + ] |
0 commit comments