aboutsummaryrefslogtreecommitdiffstats
path: root/src/resource_inventory/migrations/0012_auto_20200103_1850.py
blob: 569e433f52091900f056b1168e8b2a36a16edcbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Generated by Django 2.2 on 2020-01-03 18:50

from django.db import migrations, models
import django.db.models.deletion


def pairVlanPhysicalNetworks(apps, editor):
    PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork")
    Vlan = apps.get_model("resource_inventory", "Vlan")
    for vlan in Vlan.objects.filter(network__isnull=False):
        if PhysicalNetwork.objects.filter(id=vlan.network.id).exists():
            continue
        PhysicalNetwork.objects.create(id=vlan.network.id, vlan_id=vlan.vlan_id, generic_network=vlan.network)


def deletePhysicalNetworks(apps, editor):
    Vlan = apps.get_model("resource_inventory", "Vlan")
    for vlan in Vlan.objects.all():
        vlan.network = None
    PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork")
    PhysicalNetwork.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('resource_inventory', '0011_auto_20191106_2024'),
    ]

    operations = [
        migrations.CreateModel(
            name='PhysicalNetwork',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('vlan_id', models.IntegerField()),
                ('generic_network', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.Network')),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AlterField(
            model_name='host',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='resourcebundle',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.RunPython(pairVlanPhysicalNetworks, deletePhysicalNetworks),
        migrations.AlterField(
            model_name='vlan',
            name='network',
            field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING,
                to='resource_inventory.PhysicalNetwork', null=True),
        ),
    ]