From cedc08bc08fdc0f7d80ac1a08a6a26f3c2058568 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Mon, 5 Feb 2024 13:10:24 +0530 Subject: [PATCH] dev: sync data for issue attachments --- ...tachment_asset_key_issueattachment_type.py | 32 ++++++++ .../db/migrations/0062_auto_20240205_0703.py | 74 +++++++++++++++++++ apiserver/plane/db/models/issue.py | 13 ++++ 3 files changed, 119 insertions(+) create mode 100644 apiserver/plane/db/migrations/0061_issueattachment_asset_key_issueattachment_type.py create mode 100644 apiserver/plane/db/migrations/0062_auto_20240205_0703.py diff --git a/apiserver/plane/db/migrations/0061_issueattachment_asset_key_issueattachment_type.py b/apiserver/plane/db/migrations/0061_issueattachment_asset_key_issueattachment_type.py new file mode 100644 index 000000000..d975edfff --- /dev/null +++ b/apiserver/plane/db/migrations/0061_issueattachment_asset_key_issueattachment_type.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.7 on 2024-02-05 06:42 + +from django.db import migrations, models +import django.db.models.deletion + + + +class Migration(migrations.Migration): + + dependencies = [ + ("db", "0060_fileasset_size"), + ] + + operations = [ + migrations.AddField( + model_name="issueattachment", + name="asset_key", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="issue_assets", + to="db.fileasset", + ), + ), + migrations.AddField( + model_name="issueattachment", + name="type", + field=models.PositiveSmallIntegerField( + choices=[(0, "Attachment"), (1, "Description")], default=0 + ), + ), + ] diff --git a/apiserver/plane/db/migrations/0062_auto_20240205_0703.py b/apiserver/plane/db/migrations/0062_auto_20240205_0703.py new file mode 100644 index 000000000..8311503dd --- /dev/null +++ b/apiserver/plane/db/migrations/0062_auto_20240205_0703.py @@ -0,0 +1,74 @@ +# Generated by Django 4.2.7 on 2024-02-05 07:03 + +from django.db import migrations + + +def update_attachment_assets(apps, schema_editor): + pass + + +def create_description_assets(apps, schema_editor): + FileAsset = apps.get_model("db", "FileAsset") + IssueAttachment = apps.get_model("db", "IssueAttachment") + + bulk_issue_attachments = [] + for asset in FileAsset.objects.filter(entity_type="issue").values(): + bulk_issue_attachments.append( + IssueAttachment( + workspace_id=asset["workspace_id"], + project_id=asset["project_id"], + issue_id=asset["entity_identifier"], + asset_key_id=asset["id"], + type=1, + ) + ) + + IssueAttachment.objects.bulk_create(bulk_issue_attachments, batch_size=100) + + +def create_attachment_assets(apps, schema_editor): + bulk_assets = [] + + issue_attachments = {} + + FileAsset = apps.get_model("db", "FileAsset") + IssueAttachment = apps.get_model("db", "IssueAttachment") + + for issue_attachment in IssueAttachment.objects.filter(type=0).values(): + bulk_assets.append( + FileAsset( + workspace_id=issue_attachment["workspace_id"], + project_id=issue_attachment["project_id"], + entity_identifier=issue_attachment["issue_id"], + entity_type="issue", + asset=issue_attachment["asset"], + attributes=issue_attachment["attributes"], + ) + ) + issue_attachments[str(issue_attachment["asset"])] = str(issue_attachment["id"]) + + FileAsset.objects.bulk_create(bulk_assets, batch_size=100) + + + assets = FileAsset.objects.filter(asset__in=issue_attachments.keys()).values("id", "asset") + bulk_issue_attachments = [] + for issue_attachment in IssueAttachment.objects.filter(type=0): + asset_key_id = [asset.get("id") for asset in assets if str(asset.get("asset")) == str(issue_attachment.asset)] + if asset_key_id: + issue_attachment.asset_key_id = str(asset_key_id[0]) + bulk_issue_attachments.append(issue_attachment) + + IssueAttachment.objects.bulk_update(bulk_issue_attachments, ["asset_key"], batch_size=100) + + + +class Migration(migrations.Migration): + + dependencies = [ + ("db", "0061_issueattachment_asset_key_issueattachment_type"), + ] + + operations = [ + migrations.RunPython(create_description_assets), + migrations.RunPython(create_attachment_assets), + ] diff --git a/apiserver/plane/db/models/issue.py b/apiserver/plane/db/models/issue.py index 24bce3816..8a63da9a3 100644 --- a/apiserver/plane/db/models/issue.py +++ b/apiserver/plane/db/models/issue.py @@ -354,9 +354,22 @@ class IssueAttachment(ProjectBaseModel): file_size, ], ) + asset_key = models.ForeignKey( + "db.FileAsset", + on_delete=models.CASCADE, + related_name="issue_assets", + null=True, + ) issue = models.ForeignKey( "db.Issue", on_delete=models.CASCADE, related_name="issue_attachment" ) + type = models.PositiveSmallIntegerField( + choices=( + (0, "Attachment"), + (1, "Description"), + ), + default=0, + ) class Meta: verbose_name = "Issue Attachment"