Skip to content

Commit

Permalink
Reconstitute migration file
Browse files Browse the repository at this point in the history
Do the things as a proper 3-step process -- add new stuff, copy the data, drop the old.
  • Loading branch information
jbradberry authored and fosterseth committed Feb 2, 2024
1 parent 4c5ac1d commit 8232339
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 49 deletions.
85 changes: 38 additions & 47 deletions awx/main/migrations/0189_inbound_hop_nodes.py
Original file line number Diff line number Diff line change
@@ -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()


Expand Down Expand Up @@ -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(),
Expand All @@ -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',
Expand All @@ -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'),
),
]
3 changes: 1 addition & 2 deletions awx/main/models/ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 8232339

Please sign in to comment.