From 4940cda1806aa13591485b996264ddc887202d35 Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Thu, 27 Feb 2020 16:26:47 -0500 Subject: Improve Data Migration Improved migration files so that they preserve data Change-Id: I53283fd3fd207ed3f9773beea7b1ce2062b5bd9f Signed-off-by: Parker Berberian --- src/api/migrations/0011_auto_20200218_1536.py | 25 ---- src/api/migrations/0012_manual_20200218_1536.py | 22 +++ src/api/migrations/0012_manual_20200220.py | 18 --- src/api/migrations/0013_manual_20200218_1536.py | 29 ++++ src/api/migrations/0014_manual_20200220.py | 18 +++ .../migrations/0012_auto_20200103_1850.py | 37 ++--- .../migrations/0012_manual_20200218_1536.py | 25 ++++ .../migrations/0013_auto_20200218_1536.py | 161 ++++++++------------- src/resource_inventory/models.py | 2 +- 9 files changed, 163 insertions(+), 174 deletions(-) create mode 100644 src/api/migrations/0012_manual_20200218_1536.py delete mode 100644 src/api/migrations/0012_manual_20200220.py create mode 100644 src/api/migrations/0013_manual_20200218_1536.py create mode 100644 src/api/migrations/0014_manual_20200220.py create mode 100644 src/resource_inventory/migrations/0012_manual_20200218_1536.py diff --git a/src/api/migrations/0011_auto_20200218_1536.py b/src/api/migrations/0011_auto_20200218_1536.py index a399495..0fd7029 100644 --- a/src/api/migrations/0011_auto_20200218_1536.py +++ b/src/api/migrations/0011_auto_20200218_1536.py @@ -3,14 +3,6 @@ from django.db import migrations, models -def set_resource_id(apps, schema_editor): - for cls in ["HardwareConfig", "NetworkConfig", "SnapshotConfig"]: - model = apps.get_model('api', cls) - for m in model.objects.all(): - m.resource_id = m.host.labid - m.save() - - class Migration(migrations.Migration): dependencies = [ @@ -34,21 +26,4 @@ class Migration(migrations.Migration): name='resource_id', field=models.CharField(default='default_id', max_length=200), ), - migrations.RunPython(set_resource_id), - migrations.RemoveField( - model_name='hosthardwarerelation', - name='host', - ), - migrations.RemoveField( - model_name='hostnetworkrelation', - name='host', - ), - migrations.RemoveField( - model_name='snapshotconfig', - name='host', - ), - migrations.RemoveField( - model_name='opnfvapiconfig', - name='roles', - ), ] diff --git a/src/api/migrations/0012_manual_20200218_1536.py b/src/api/migrations/0012_manual_20200218_1536.py new file mode 100644 index 0000000..55befbd --- /dev/null +++ b/src/api/migrations/0012_manual_20200218_1536.py @@ -0,0 +1,22 @@ +# Generated by Django 2.2 on 2020-02-18 15:36 + +from django.db import migrations + + +def set_resource_id(apps, schema_editor): + for cls in ["HostHardwareRelation", "HostNetworkRelation", "SnapshotConfig"]: + model = apps.get_model('api', cls) + for m in model.objects.all(): + m.resource_id = m.host.labid + m.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0011_auto_20200218_1536'), + ] + + operations = [ + migrations.RunPython(set_resource_id), + ] diff --git a/src/api/migrations/0012_manual_20200220.py b/src/api/migrations/0012_manual_20200220.py deleted file mode 100644 index 40f7e79..0000000 --- a/src/api/migrations/0012_manual_20200220.py +++ /dev/null @@ -1,18 +0,0 @@ - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('api', '0011_auto_20200218_1536'), - ('resource_inventory', '0013_auto_20200218_1536') - ] - - operations = [ - migrations.AddField( - model_name='opnfvapiconfig', - name='roles', - field=models.ManyToManyField(to='resource_inventory.ResourceOPNFVConfig'), - ), - ] diff --git a/src/api/migrations/0013_manual_20200218_1536.py b/src/api/migrations/0013_manual_20200218_1536.py new file mode 100644 index 0000000..0b76e84 --- /dev/null +++ b/src/api/migrations/0013_manual_20200218_1536.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2 on 2020-02-18 15:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0012_manual_20200218_1536'), + ] + + operations = [ + migrations.RemoveField( + model_name='hosthardwarerelation', + name='host', + ), + migrations.RemoveField( + model_name='hostnetworkrelation', + name='host', + ), + migrations.RemoveField( + model_name='snapshotconfig', + name='host', + ), + migrations.RemoveField( + model_name='opnfvapiconfig', + name='roles', + ), + ] diff --git a/src/api/migrations/0014_manual_20200220.py b/src/api/migrations/0014_manual_20200220.py new file mode 100644 index 0000000..2e2cd58 --- /dev/null +++ b/src/api/migrations/0014_manual_20200220.py @@ -0,0 +1,18 @@ + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0013_manual_20200218_1536'), + ('resource_inventory', '0013_auto_20200218_1536') + ] + + operations = [ + migrations.AddField( + model_name='opnfvapiconfig', + name='roles', + field=models.ManyToManyField(to='resource_inventory.ResourceOPNFVConfig'), + ), + ] diff --git a/src/resource_inventory/migrations/0012_auto_20200103_1850.py b/src/resource_inventory/migrations/0012_auto_20200103_1850.py index 2bb203e..569e433 100644 --- a/src/resource_inventory/migrations/0012_auto_20200103_1850.py +++ b/src/resource_inventory/migrations/0012_auto_20200103_1850.py @@ -4,30 +4,23 @@ from django.db import migrations, models import django.db.models.deletion -def genTempVlanNetwork(apps, editor): +def pairVlanPhysicalNetworks(apps, editor): + PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork") Vlan = apps.get_model("resource_inventory", "Vlan") - Network = apps.get_model("resource_inventory", "Network") - tempVlanNetwork = apps.get_model("resource_inventory", "tempVlanNetwork") for vlan in Vlan.objects.filter(network__isnull=False): - tempVlanNetwork.objects.create(network=vlan.network, vlan=vlan) + 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 deleteTempVlanNetworks(apps, editor): - tempVlanNetwork = apps.get_model("resource_inventory", "tempVlanNetwork") - tempVlanNetwork.objects.all().delete() - - -def pairVlanPhysicalNetworks(apps, editor): - PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork") - tempVlanPair = apps.get_model("resource_inventory", "tempVlanNetwork") - for pair in tempVlanPair.objects.all(): - physicalNetwork = PhysicalNetwork.objects.create(vlan_id=vlan.vlan_id, - generic_network=pair.network) - pair.vlan.network = physicalNetwork 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 = [ @@ -56,21 +49,11 @@ class Migration(migrations.Migration): name='id', field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), ), - migrations.CreateModel( - name='tempVlanNetwork', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('vlan', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.vlan')), - ('network', models.ForeignKey(null=True, to='resource_inventory.network', on_delete=django.db.models.deletion.CASCADE)), - ] - ), - migrations.RunPython(genTempVlanNetwork, deleteTempVlanNetworks), + 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), ), - migrations.RunPython(pairVlanPhysicalNetworks, deletePhysicalNetworks), - migrations.DeleteModel("tempVlanNetwork") ] diff --git a/src/resource_inventory/migrations/0012_manual_20200218_1536.py b/src/resource_inventory/migrations/0012_manual_20200218_1536.py new file mode 100644 index 0000000..378bdc3 --- /dev/null +++ b/src/resource_inventory/migrations/0012_manual_20200218_1536.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2 on 2020-02-18 15:36 + +from django.conf import settings +from django.db import migrations + + +def clear_networks(apps, schema_editor): + Network = apps.get_model('resource_inventory', 'Network') + Vlan = apps.get_model('resource_inventory', 'Vlan') + for vlan in Vlan.objects.all(): + vlan.delete() + for net in Network.objects.all(): + net.delete() + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('resource_inventory', '0012_auto_20200103_1850'), + ] + + operations = [ + migrations.RunPython(clear_networks) + ] diff --git a/src/resource_inventory/migrations/0013_auto_20200218_1536.py b/src/resource_inventory/migrations/0013_auto_20200218_1536.py index 3049dc7..014cb2f 100644 --- a/src/resource_inventory/migrations/0013_auto_20200218_1536.py +++ b/src/resource_inventory/migrations/0013_auto_20200218_1536.py @@ -6,17 +6,33 @@ import django.db.models.deletion import resource_inventory.models +def clear_resource_bundles(apps, schema_editor): + ResourceBundle = apps.get_model('resource_inventory', 'ResourceBundle') + for rb in ResourceBundle.objects.all(): + rb.template = None + rb.save() + + +def create_default_template(apps, schema_editor): + ResourceTemplate = apps.get_model('resource_inventory', 'ResourceTemplate') + ResourceTemplate.objects.create(id=1, name="Default Template") + + def populate_servers(apps, schema_editor): """Convert old Host models to Server Resources.""" Host = apps.get_model('resource_inventory', 'Host') Server = apps.get_model('resource_inventory', 'Server') + ResourceProfile = apps.get_model('resource_inventory', 'ResourceProfile') for h in Host.objects.all(): + rp = ResourceProfile.objects.get(id=h.profile.id) Server.objects.create( working=h.working, vendor=h.vendor, labid=h.labid, booked=h.booked, - name=h.labid + name=h.labid, + lab=h.lab, + profile=rp ) @@ -42,14 +58,16 @@ def populate_resource_profiles(apps, schema_editor): HostProfile = apps.get_model('resource_inventory', 'HostProfile') ResourceProfile = apps.get_model('resource_inventory', 'ResourceProfile') for hp in HostProfile.objects.all(): - rp = ResourceProfile.objects.create(name=hp.name, description=hp.description) + rp = ResourceProfile.objects.create(id=hp.id, name=hp.name, description=hp.description) rp.labs.add(*list(hp.labs.all())) + """ + TODO: link these models together rp.interfaceprofile = hp.interfaceprofile rp.storageprofile = hp.storageprofile rp.cpuprofile = hp.cpuprofile rp.ramprofile = hp.ramprofile rp.save() - rp.interfaceprofile.host = rp + hp.interfaceprofile.host = rp rp.storageprofile.host = rp rp.cpuprofile.host = rp rp.ramprofile.host = rp @@ -57,30 +75,17 @@ def populate_resource_profiles(apps, schema_editor): rp.storageprofile.save() rp.cpuprofile.save() rp.ramprofile.save() + """ class Migration(migrations.Migration): - """TODO: Creation of the following models. - - - Server - - ResourceTemplate - - ResourceProfile - - ResourceConfiguration (?) - - InterfaceConfiguration (?) - - And set the correct defaults for the following fields: - - Interface.profile - - OpnfvConfig.template (?) - - physicalnetwork.lab - - physicalnetwork.profile - """ dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('booking', '0007_remove_booking_config_bundle'), ('account', '0004_downtime'), - ('api', '0011_auto_20200218_1536'), - ('resource_inventory', '0012_auto_20200103_1850'), + ('api', '0013_manual_20200218_1536'), + ('resource_inventory', '0012_manual_20200218_1536'), ] operations = [ @@ -143,6 +148,36 @@ class Migration(migrations.Migration): 'abstract': False, }, ), + migrations.AddField( + model_name='server', + name='bundle', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceBundle'), + ), + migrations.AddField( + model_name='server', + name='config', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceConfiguration'), + ), + migrations.AddField( + model_name='server', + name='interfaces', + field=models.ManyToManyField(to='resource_inventory.Interface'), + ), + migrations.AddField( + model_name='server', + name='lab', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Lab'), + ), + migrations.AddField( + model_name='server', + name='profile', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceProfile'), + ), + migrations.AddField( + model_name='server', + name='remote_management', + field=models.ForeignKey(default=resource_inventory.models.get_default_remote_info, on_delete=models.SET(resource_inventory.models.get_default_remote_info), to='resource_inventory.RemoteInfo'), + ), migrations.RunPython(populate_servers), migrations.RemoveField( model_name='generichost', @@ -251,47 +286,6 @@ class Migration(migrations.Migration): name='order', field=models.IntegerField(default=-1), ), - migrations.AddField( - model_name='physicalnetwork', - name='bundle', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceBundle'), - ), - migrations.AddField( - model_name='physicalnetwork', - name='interfaces', - field=models.ManyToManyField(to='resource_inventory.Interface'), - ), - migrations.AddField( - model_name='physicalnetwork', - name='lab', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='account.Lab'), - preserve_default=False, - ), - migrations.AddField( - model_name='physicalnetwork', - name='labid', - field=models.CharField(default='default_id', max_length=200, unique=True), - ), - migrations.AddField( - model_name='physicalnetwork', - name='model', - field=models.CharField(default='unknown', max_length=150), - ), - migrations.AddField( - model_name='physicalnetwork', - name='remote_management', - field=models.ForeignKey(default=resource_inventory.models.get_default_remote_info, on_delete=models.SET(resource_inventory.models.get_default_remote_info), to='resource_inventory.RemoteInfo'), - ), - migrations.AddField( - model_name='physicalnetwork', - name='vendor', - field=models.CharField(default='unknown', max_length=100), - ), - migrations.AddField( - model_name='physicalnetwork', - name='working', - field=models.BooleanField(default=True), - ), migrations.AlterField( model_name='cpuprofile', name='host', @@ -322,6 +316,7 @@ class Migration(migrations.Migration): name='host', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ramprofile', to='resource_inventory.ResourceProfile'), ), + migrations.RunPython(clear_resource_bundles), migrations.AlterField( model_name='resourcebundle', name='template', @@ -342,9 +337,6 @@ class Migration(migrations.Migration): migrations.DeleteModel( name='GenericResourceBundle', ), - migrations.DeleteModel( - name='Host', - ), migrations.DeleteModel( name='HostConfiguration', ), @@ -354,35 +346,8 @@ class Migration(migrations.Migration): migrations.DeleteModel( name='HostProfile', ), - migrations.AddField( - model_name='server', - name='bundle', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceBundle'), - ), - migrations.AddField( - model_name='server', - name='config', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceConfiguration'), - ), - migrations.AddField( - model_name='server', - name='interfaces', - field=models.ManyToManyField(to='resource_inventory.Interface'), - ), - migrations.AddField( - model_name='server', - name='lab', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Lab'), - ), - migrations.AddField( - model_name='server', - name='profile', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceProfile'), - ), - migrations.AddField( - model_name='server', - name='remote_management', - field=models.ForeignKey(default=resource_inventory.models.get_default_remote_info, on_delete=models.SET(resource_inventory.models.get_default_remote_info), to='resource_inventory.RemoteInfo'), + migrations.DeleteModel( + name='Host', ), migrations.AddField( model_name='resourceopnfvconfig', @@ -429,21 +394,11 @@ class Migration(migrations.Migration): name='acts_as', field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.InterfaceConfiguration'), ), + migrations.RunPython(create_default_template), migrations.AddField( model_name='opnfvconfig', name='template', field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='opnfv_config', to='resource_inventory.ResourceTemplate'), preserve_default=False, ), - migrations.AddField( - model_name='physicalnetwork', - name='config', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceConfiguration'), - ), - migrations.AddField( - model_name='physicalnetwork', - name='profile', - field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceProfile'), - preserve_default=False, - ), ] diff --git a/src/resource_inventory/models.py b/src/resource_inventory/models.py index eec009a..d11f71b 100644 --- a/src/resource_inventory/models.py +++ b/src/resource_inventory/models.py @@ -382,7 +382,7 @@ class Network(models.Model): return self.name -class PhysicalNetwork(Resource): +class PhysicalNetwork(models.Model): vlan_id = models.IntegerField() generic_network = models.ForeignKey(Network, on_delete=models.CASCADE) -- cgit 1.2.3-korg