Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions src/website/shared/migrations/0048_remove_attribute_suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,49 @@

def remove_suffix(apps, schema_editor):
NixDerivation = apps.get_model('shared', 'NixDerivation')
for instance in NixDerivation.objects.all():
if instance.attribute and instance.system:
# Remove the suffix if it matches the system field
suffix = f".{instance.system}"
if instance.attribute.endswith(suffix):
instance.attribute = instance.attribute.removesuffix(suffix)
instance.save()
batch_size = 30000
instances = NixDerivation.objects.all()
count = instances.count()

for offset in range(0, count, batch_size):
instances_to_update = []
batch = instances[offset : offset + batch_size]
for instance in batch:
if instance.attribute and instance.system:
# Remove the suffix if it matches the system field
suffix = f".{instance.system}"
if instance.attribute.endswith(suffix):
instance.attribute = instance.attribute.removesuffix(suffix)
instances_to_update.append(instance)

if instances_to_update:
NixDerivation.objects.bulk_update(
instances_to_update, ["attribute"], batch_size=1000
)


def reapply_suffix(apps, schema_editor):
NixDerivation = apps.get_model('shared', 'NixDerivation')
for instance in NixDerivation.objects.all():
if instance.attribute and instance.system:
# Reapply the suffix if it doesn't already exist
suffix = f".{instance.system}"
if not instance.attribute.endswith(suffix):
instance.attribute = f"{instance.attribute}{suffix}"
instance.save()
batch_size = 30000
instances = NixDerivation.objects.all()
count = instances.count()

for offset in range(0, offset, batch_size):
instances_to_update = []
batch = instances[offset : offset + batch_size]
for instance in batch:
if instance.attribute and instance.system:
# Reapply the suffix if it doesn't already exist
suffix = f".{instance.system}"
if not instance.attribute.endswith(suffix):
instance.attribute = f"{instance.attribute}{suffix}"
instances_to_update.append(instance)

if instances_to_update:
NixDerivation.objects.bulk_update(
instances_to_update, ["attribute"], batch_size=1000
)


class Migration(migrations.Migration):

Expand Down