|
| 1 | +from django.db import migrations |
| 2 | +from django.db.models import Count |
| 3 | + |
| 4 | +import utilities.fields |
| 5 | + |
| 6 | + |
| 7 | +def recalculate_device_counts(apps, schema_editor): |
| 8 | + Device = apps.get_model("dcim", "Device") |
| 9 | + devices = list(Device.objects.all().annotate( |
| 10 | + _console_port_count=Count('consoleports', distinct=True), |
| 11 | + _console_server_port_count=Count('consoleserverports', distinct=True), |
| 12 | + _power_port_count=Count('powerports', distinct=True), |
| 13 | + _power_outlet_count=Count('poweroutlets', distinct=True), |
| 14 | + _interface_count=Count('interfaces', distinct=True), |
| 15 | + _front_port_count=Count('frontports', distinct=True), |
| 16 | + _rear_port_count=Count('rearports', distinct=True), |
| 17 | + _device_bay_count=Count('devicebays', distinct=True), |
| 18 | + _module_bay_count=Count('modulebays', distinct=True), |
| 19 | + _inventory_item_count=Count('inventoryitems', distinct=True), |
| 20 | + )) |
| 21 | + |
| 22 | + for device in devices: |
| 23 | + device.console_port_count = device._console_port_count |
| 24 | + device.console_server_port_count = device._console_server_port_count |
| 25 | + device.power_port_count = device._power_port_count |
| 26 | + device.power_outlet_count = device._power_outlet_count |
| 27 | + device.interface_count = device._interface_count |
| 28 | + device.front_port_count = device._front_port_count |
| 29 | + device.rear_port_count = device._rear_port_count |
| 30 | + device.device_bay_count = device._device_bay_count |
| 31 | + device.module_bay_count = device._module_bay_count |
| 32 | + device.inventory_item_count = device._inventory_item_count |
| 33 | + |
| 34 | + Device.objects.bulk_update(devices, [ |
| 35 | + 'console_port_count', 'console_server_port_count', 'power_port_count', 'power_outlet_count', 'interface_count', |
| 36 | + 'front_port_count', 'rear_port_count', 'device_bay_count', 'module_bay_count', 'inventory_item_count', |
| 37 | + ]) |
| 38 | + |
| 39 | + |
| 40 | +class Migration(migrations.Migration): |
| 41 | + dependencies = [ |
| 42 | + ('dcim', '0174_rack_starting_unit'), |
| 43 | + ] |
| 44 | + |
| 45 | + operations = [ |
| 46 | + migrations.AddField( |
| 47 | + model_name='device', |
| 48 | + name='console_port_count', |
| 49 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.ConsolePort'), |
| 50 | + ), |
| 51 | + migrations.AddField( |
| 52 | + model_name='device', |
| 53 | + name='console_server_port_count', |
| 54 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.ConsoleServerPort'), |
| 55 | + ), |
| 56 | + migrations.AddField( |
| 57 | + model_name='device', |
| 58 | + name='power_port_count', |
| 59 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.PowerPort'), |
| 60 | + ), |
| 61 | + migrations.AddField( |
| 62 | + model_name='device', |
| 63 | + name='power_outlet_count', |
| 64 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.PowerOutlet'), |
| 65 | + ), |
| 66 | + migrations.AddField( |
| 67 | + model_name='device', |
| 68 | + name='interface_count', |
| 69 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.Interface'), |
| 70 | + ), |
| 71 | + migrations.AddField( |
| 72 | + model_name='device', |
| 73 | + name='front_port_count', |
| 74 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.FrontPort'), |
| 75 | + ), |
| 76 | + migrations.AddField( |
| 77 | + model_name='device', |
| 78 | + name='rear_port_count', |
| 79 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.RearPort'), |
| 80 | + ), |
| 81 | + migrations.AddField( |
| 82 | + model_name='device', |
| 83 | + name='device_bay_count', |
| 84 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.DeviceBay'), |
| 85 | + ), |
| 86 | + migrations.AddField( |
| 87 | + model_name='device', |
| 88 | + name='module_bay_count', |
| 89 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.ModuleBay'), |
| 90 | + ), |
| 91 | + migrations.AddField( |
| 92 | + model_name='device', |
| 93 | + name='inventory_item_count', |
| 94 | + field=utilities.fields.CounterCacheField(default=0, to_field='device', to_model='dcim.InventoryItem'), |
| 95 | + ), |
| 96 | + migrations.RunPython( |
| 97 | + recalculate_device_counts, |
| 98 | + reverse_code=migrations.RunPython.noop |
| 99 | + ), |
| 100 | + ] |
0 commit comments