Skip to content

Commit

Permalink
InstanceLink unique constraint source and target
Browse files Browse the repository at this point in the history
Prevent creating InstanceLinks with duplicate
source and target pairings.

Signed-off-by: Seth Foster <[email protected]>
  • Loading branch information
fosterseth committed Jan 31, 2024
1 parent 745157e commit 19e73da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions awx/main/migrations/0189_inbound_hop_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,10 @@ class Migration(migrations.Migration):
help_text='The target receptor address of this peer link.', on_delete=django.db.models.deletion.CASCADE, to='main.receptoraddress'
),
),
migrations.AddConstraint(
model_name='instancelink',
constraint=models.UniqueConstraint(
fields=('source', 'target'), name='unique_source_target', violation_error_message='Field source and target must be unique together.'
),
),
]
8 changes: 8 additions & 0 deletions awx/main/models/ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def has_policy_changes(self):
class InstanceLink(BaseModel):
class Meta:
ordering = ("id",)
# add constraint for source and target to be unique together
constraints = [
models.UniqueConstraint(
fields=["source", "target"],
name="unique_source_target",
violation_error_message=_("Field source and target must be 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."))
Expand Down

0 comments on commit 19e73da

Please sign in to comment.