Add stdout progress logging to cable migration

This commit is contained in:
Jeremy Stretch 2019-10-03 18:49:56 -04:00
parent 8a5ee6f8ea
commit 26071903c1

View File

@ -5,21 +5,32 @@ import django.db.models.deletion
def cache_cable_devices(apps, schema_editor):
Cable = apps.get_model('dcim', 'Cable')
print("\nUpdatng cable device terminations...")
cable_count = Cable.objects.count()
# Cache A/B termination devices on all existing Cables. Note that the custom save() method on Cable is not
# available during a migration, so we replicate its logic here.
for cable in Cable.objects.all():
for i, cable in enumerate(Cable.objects.all(), start=1):
if not i % 1000:
print("[{}/{}]".format(i, cable_count))
termination_a_model = apps.get_model(cable.termination_a_type.app_label, cable.termination_a_type.model)
termination_a_device = None
if hasattr(termination_a_model, 'device'):
termination_a = termination_a_model.objects.get(pk=cable.termination_a_id)
cable._termination_a_device = termination_a.device
termination_a_device = termination_a.device
termination_b_model = apps.get_model(cable.termination_b_type.app_label, cable.termination_b_type.model)
termination_b_device = None
if hasattr(termination_b_model, 'device'):
termination_b = termination_b_model.objects.get(pk=cable.termination_b_id)
cable._termination_b_device = termination_b.device
termination_b_device = termination_b.device
cable.save()
Cable.objects.filter(pk=cable.pk).update(
_termination_a_device=termination_a_device,
_termination_b_device=termination_b_device
)
class Migration(migrations.Migration):