From 82323390a79259d4ad2c74c80739f111d061edb0 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Fri, 19 Jan 2024 14:37:37 -0500 Subject: [PATCH] Reconstitute migration file Do the things as a proper 3-step process -- add new stuff, copy the data, drop the old. --- awx/main/migrations/0189_inbound_hop_nodes.py | 85 +++++++++---------- awx/main/models/ha.py | 3 +- 2 files changed, 39 insertions(+), 49 deletions(-) diff --git a/awx/main/migrations/0189_inbound_hop_nodes.py b/awx/main/migrations/0189_inbound_hop_nodes.py index e16b3429052f..a01ed3152725 100644 --- a/awx/main/migrations/0189_inbound_hop_nodes.py +++ b/awx/main/migrations/0189_inbound_hop_nodes.py @@ -1,39 +1,36 @@ -# Generated by Django 4.2.6 on 2024-01-19 03:13 +# Generated by Django 4.2.6 on 2024-01-19 19:24 import django.core.validators from django.db import migrations, models import django.db.models.deletion -def create_receptor_address(apps, schema_editor): +def create_receptor_addresses(apps, schema_editor): """ - if listener_port was defined on an instance, create a receptor address for it + If listener_port was defined on an instance, create a receptor address for it """ Instance = apps.get_model('main', 'Instance') ReceptorAddress = apps.get_model('main', 'ReceptorAddress') - for instance in Instance.objects.all(): - if instance.listener_port: - instance.peers_from.all() - ReceptorAddress.objects.create( - instance=instance, - address=instance.hostname, - port=instance.listener_port, - peers_from_control_nodes=instance.peers_from_control_nodes, - protocol='tcp', - is_internal=False, - canonical=True, - ) + for instance in Instance.objects.exclude(listener_port=None): + ReceptorAddress.objects.create( + instance=instance, + address=instance.hostname, + port=instance.listener_port, + peers_from_control_nodes=instance.peers_from_control_nodes, + protocol='tcp', + is_internal=False, + canonical=True, + ) -def link_to_receptor_address(apps, schema_editor): +def link_to_receptor_addresses(apps, schema_editor): """ - modify each InstanceLink to point to the newly created - ReceptorAddresses, through the target_new field - target_new will be renamed to target in an operation below + Modify each InstanceLink to point to the newly created + ReceptorAddresses, using the new target field """ InstanceLink = apps.get_model('main', 'InstanceLink') for link in InstanceLink.objects.all(): - link.target_new = link.target.receptor_addresses.get() + link.target = link.target_old.receptor_addresses.get() link.save() @@ -78,6 +75,11 @@ class Migration(migrations.Migration): model_name='instancelink', name='source_and_target_can_not_be_equal', ), + migrations.RenameField( + model_name='instancelink', + old_name='target', + new_name='target_old', + ), migrations.AlterUniqueTogether( name='instancelink', unique_together=set(), @@ -87,9 +89,10 @@ class Migration(migrations.Migration): name='managed', field=models.BooleanField(default=False, editable=False, help_text='If True, this instance is managed by the control plane.'), ), - migrations.AddConstraint( + migrations.AlterField( model_name='instancelink', - constraint=models.UniqueConstraint(fields=('source', 'target'), name='source_target_unique_together'), + name='source', + field=models.ForeignKey(help_text='The source instance of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.instance'), ), migrations.AddField( model_name='receptoraddress', @@ -101,46 +104,34 @@ class Migration(migrations.Migration): name='receptor_address', field=models.ManyToManyField(blank=True, to='main.receptoraddress'), ), - migrations.AddField( - model_name='instancelink', - name='target_new', - field=models.ForeignKey( - help_text='The target receptor address of this peer link.', null=True, on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress' - ), - ), migrations.AddConstraint( model_name='receptoraddress', constraint=models.UniqueConstraint(fields=('address',), name='unique_receptor_address', violation_error_message='Receptor address must be unique.'), ), - migrations.RunPython(create_receptor_address), - migrations.RunPython(link_to_receptor_address), - migrations.RemoveField( - model_name='instancelink', - name='target_new', - ), - migrations.AlterField( - model_name='instance', - name='peers', - field=models.ManyToManyField(related_name='peers_from', through='main.InstanceLink', to='main.receptoraddress'), - ), - migrations.AlterField( + migrations.AddField( model_name='instancelink', name='target', field=models.ForeignKey( - help_text='The target receptor address of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress' + help_text='The target receptor address of this peer link.', null=True, on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress' ), ), - migrations.AlterField( - model_name='instancelink', - name='source', - field=models.ForeignKey(help_text='The source instance of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.instance'), + migrations.RunPython(create_receptor_addresses), + migrations.RunPython(link_to_receptor_addresses), + migrations.RemoveField( + model_name='instance', + name='peers_from_control_nodes', ), migrations.RemoveField( model_name='instance', name='listener_port', ), migrations.RemoveField( + model_name='instancelink', + name='target_old', + ), + migrations.AlterField( model_name='instance', - name='peers_from_control_nodes', + name='peers', + field=models.ManyToManyField(related_name='peers_from', through='main.InstanceLink', to='main.receptoraddress'), ), ] diff --git a/awx/main/models/ha.py b/awx/main/models/ha.py index fdf28e231656..e6e7b0861a8b 100644 --- a/awx/main/models/ha.py +++ b/awx/main/models/ha.py @@ -67,10 +67,9 @@ def has_policy_changes(self): class InstanceLink(BaseModel): class Meta: ordering = ("id",) - constraints = [models.UniqueConstraint(fields=['source', 'target'], name='source_target_unique_together')] source = models.ForeignKey('Instance', on_delete=models.CASCADE, help_text=_("The source instance of this peer link.")) - target = models.ForeignKey('ReceptorAddress', on_delete=models.CASCADE, help_text=_("The target receptor address of this peer link.")) + target = models.ForeignKey('ReceptorAddress', null=True, on_delete=models.CASCADE, help_text=_("The target receptor address of this peer link.")) class States(models.TextChoices): ADDING = 'adding', _('Adding')