dev: sync data for issue attachments

This commit is contained in:
pablohashescobar 2024-02-05 13:10:24 +05:30
parent 31cca8d07e
commit cedc08bc08
3 changed files with 119 additions and 0 deletions

View File

@ -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
),
),
]

View File

@ -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),
]

View File

@ -354,9 +354,22 @@ class IssueAttachment(ProjectBaseModel):
file_size, file_size,
], ],
) )
asset_key = models.ForeignKey(
"db.FileAsset",
on_delete=models.CASCADE,
related_name="issue_assets",
null=True,
)
issue = models.ForeignKey( issue = models.ForeignKey(
"db.Issue", on_delete=models.CASCADE, related_name="issue_attachment" "db.Issue", on_delete=models.CASCADE, related_name="issue_attachment"
) )
type = models.PositiveSmallIntegerField(
choices=(
(0, "Attachment"),
(1, "Description"),
),
default=0,
)
class Meta: class Meta:
verbose_name = "Issue Attachment" verbose_name = "Issue Attachment"