-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Closes #19977: Denormalize device relationships on component models #19984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8643cbc
Closes #19977: Denormalize site, location, and rack for device compon…
jeremystretch 4038b13
Set blank=True on denormalized ForeignKeys
jeremystretch 945b8b6
Populate denormalized field in test data
jeremystretch de72ac9
Ignore private fields when constructing test GraphQL requests
jeremystretch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
287 changes: 287 additions & 0 deletions
287
netbox/dcim/migrations/0209_device_component_denorm_site_location.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
| from django.db.models import OuterRef, Subquery | ||
|
|
||
|
|
||
| def populate_denormalized_data(apps, schema_editor): | ||
| Device = apps.get_model('dcim', 'Device') | ||
| component_models = ( | ||
| apps.get_model('dcim', 'ConsolePort'), | ||
| apps.get_model('dcim', 'ConsoleServerPort'), | ||
| apps.get_model('dcim', 'PowerPort'), | ||
| apps.get_model('dcim', 'PowerOutlet'), | ||
| apps.get_model('dcim', 'Interface'), | ||
| apps.get_model('dcim', 'FrontPort'), | ||
| apps.get_model('dcim', 'RearPort'), | ||
| apps.get_model('dcim', 'DeviceBay'), | ||
| apps.get_model('dcim', 'ModuleBay'), | ||
| apps.get_model('dcim', 'InventoryItem'), | ||
| ) | ||
|
|
||
| for model in component_models: | ||
| subquery = Device.objects.filter(pk=OuterRef('device_id')) | ||
| model.objects.update( | ||
| _site=Subquery(subquery.values('site_id')[:1]), | ||
| _location=Subquery(subquery.values('location_id')[:1]), | ||
| _rack=Subquery(subquery.values('rack_id')[:1]), | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ('dcim', '0208_devicerole_uniqueness'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='consoleport', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='consoleport', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='consoleport', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='consoleserverport', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='consoleserverport', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='consoleserverport', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='devicebay', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='devicebay', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='devicebay', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='frontport', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='frontport', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='frontport', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='interface', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='interface', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='interface', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='inventoryitem', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='inventoryitem', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='inventoryitem', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='modulebay', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='modulebay', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='modulebay', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='poweroutlet', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='poweroutlet', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='poweroutlet', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='powerport', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='powerport', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='powerport', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='rearport', | ||
| name='_location', | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name='+', | ||
| to='dcim.location', | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='rearport', | ||
| name='_rack', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.rack' | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='rearport', | ||
| name='_site', | ||
| field=models.ForeignKey( | ||
| blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='dcim.site' | ||
| ), | ||
| ), | ||
| migrations.RunPython(populate_denormalized_data), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,29 @@ class ComponentModel(NetBoxModel): | |
| blank=True | ||
| ) | ||
|
|
||
| # Denormalized references replicated from the parent Device | ||
| _site = models.ForeignKey( | ||
| to='dcim.Site', | ||
| on_delete=models.SET_NULL, | ||
| related_name='+', | ||
| blank=True, | ||
| null=True, | ||
| ) | ||
| _location = models.ForeignKey( | ||
| to='dcim.Location', | ||
| on_delete=models.SET_NULL, | ||
| related_name='+', | ||
| blank=True, | ||
| null=True, | ||
| ) | ||
| _rack = models.ForeignKey( | ||
| to='dcim.Rack', | ||
| on_delete=models.SET_NULL, | ||
| related_name='+', | ||
| blank=True, | ||
| null=True, | ||
| ) | ||
|
|
||
| class Meta: | ||
| abstract = True | ||
| ordering = ('device', 'name') | ||
|
|
@@ -100,6 +123,14 @@ def clean(self): | |
| "device": _("Components cannot be moved to a different device.") | ||
| }) | ||
|
|
||
| def save(self, *args, **kwargs): | ||
| # Save denormalized references | ||
| self._site = self.device.site | ||
| self._location = self.device.location | ||
| self._rack = self.device.rack | ||
|
|
||
| super().save(*args, **kwargs) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, any reason to not use the signal handler approach for this as well?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. Maybe slightly cleaner, more direct? |
||
| @property | ||
| def parent_object(self): | ||
| return self.device | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any concern here about how long these updates might lock up a whole table, particularly
dcim_interface? I would imagine a series of batched updates would results more, but shorter locks, meaning that other operations on the tables can happen, if needed.I'd be curious if you had a chance to run this against a DB with 1M+ interfaces.
Having said all that, I'm far from certain this is really an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During testing with ~500K interfaces, the migration took only a few seconds to run, so I'm not too concerned about it. It's a valid point, but I'm not sure how we'd meaningfully split up the query to batch it. I wanted to use subqueries to avoid handling any of the bulk data in Python.