From e31fc372153f75abcd1110517c8670f3e4cba16b Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Thu, 11 Mar 2021 11:25:59 -0500 Subject: [PATCH] Make sure that EE foreign keys are polymorphic.SET_NULL Deleting EEs that had been attached to something was failing. --- .../0130_ee_polymorphic_set_null.py | 34 +++++++++++++++++++ awx/main/models/mixins.py | 4 +-- awx/main/models/organization.py | 3 +- awx/main/models/projects.py | 4 +-- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 awx/main/migrations/0130_ee_polymorphic_set_null.py diff --git a/awx/main/migrations/0130_ee_polymorphic_set_null.py b/awx/main/migrations/0130_ee_polymorphic_set_null.py new file mode 100644 index 0000000000..a9a0b63715 --- /dev/null +++ b/awx/main/migrations/0130_ee_polymorphic_set_null.py @@ -0,0 +1,34 @@ +# Generated by Django 2.2.16 on 2021-03-11 16:25 + +import awx.main.utils.polymorphic +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0129_unifiedjob_installed_collections'), + ] + + operations = [ + migrations.AlterField( + model_name='organization', + name='default_environment', + field=models.ForeignKey(blank=True, default=None, help_text='The default execution environment for jobs run by this organization.', null=True, on_delete=awx.main.utils.polymorphic.SET_NULL, related_name='+', to='main.ExecutionEnvironment'), + ), + migrations.AlterField( + model_name='project', + name='default_environment', + field=models.ForeignKey(blank=True, default=None, help_text='The default execution environment for jobs run using this project.', null=True, on_delete=awx.main.utils.polymorphic.SET_NULL, related_name='+', to='main.ExecutionEnvironment'), + ), + migrations.AlterField( + model_name='unifiedjob', + name='execution_environment', + field=models.ForeignKey(blank=True, default=None, help_text='The container image to be used for execution.', null=True, on_delete=awx.main.utils.polymorphic.SET_NULL, related_name='unifiedjobs', to='main.ExecutionEnvironment'), + ), + migrations.AlterField( + model_name='unifiedjobtemplate', + name='execution_environment', + field=models.ForeignKey(blank=True, default=None, help_text='The container image to be used for execution.', null=True, on_delete=awx.main.utils.polymorphic.SET_NULL, related_name='unifiedjobtemplates', to='main.ExecutionEnvironment'), + ), + ] diff --git a/awx/main/models/mixins.py b/awx/main/models/mixins.py index 549c93607d..9df38b3fa4 100644 --- a/awx/main/models/mixins.py +++ b/awx/main/models/mixins.py @@ -22,7 +22,7 @@ from awx.main.models.base import prevent_search from awx.main.models.rbac import ( Role, RoleAncestorEntry, get_roles_on_resource ) -from awx.main.utils import parse_yaml_or_json, get_custom_venv_choices, get_licenser +from awx.main.utils import parse_yaml_or_json, get_custom_venv_choices, get_licenser, polymorphic from awx.main.utils.encryption import decrypt_value, get_encryption_key, is_encrypted from awx.main.utils.polymorphic import build_polymorphic_ctypes_map from awx.main.fields import JSONField, AskForField @@ -450,7 +450,7 @@ class ExecutionEnvironmentMixin(models.Model): null=True, blank=True, default=None, - on_delete=models.SET_NULL, + on_delete=polymorphic.SET_NULL, related_name='%(class)ss', help_text=_('The container image to be used for execution.'), ) diff --git a/awx/main/models/organization.py b/awx/main/models/organization.py index f0ecfea5c7..f087b8eb6b 100644 --- a/awx/main/models/organization.py +++ b/awx/main/models/organization.py @@ -27,6 +27,7 @@ from awx.main.models.rbac import ( ) from awx.main.models.unified_jobs import UnifiedJob from awx.main.models.mixins import ResourceMixin, CustomVirtualEnvMixin, RelatedJobsMixin +from awx.main.utils import polymorphic __all__ = ['Organization', 'Team', 'Profile', 'UserSessionMembership'] @@ -66,7 +67,7 @@ class Organization(CommonModel, NotificationFieldsModel, ResourceMixin, CustomVi null=True, blank=True, default=None, - on_delete=models.SET_NULL, + on_delete=polymorphic.SET_NULL, related_name='+', help_text=_('The default execution environment for jobs run by this organization.'), ) diff --git a/awx/main/models/projects.py b/awx/main/models/projects.py index c9bf87f408..5a613b6c21 100644 --- a/awx/main/models/projects.py +++ b/awx/main/models/projects.py @@ -35,7 +35,7 @@ from awx.main.models.mixins import ( CustomVirtualEnvMixin, RelatedJobsMixin ) -from awx.main.utils import update_scm_url +from awx.main.utils import update_scm_url, polymorphic from awx.main.utils.ansible import skip_directory, could_be_inventory, could_be_playbook from awx.main.fields import ImplicitRoleField from awx.main.models.rbac import ( @@ -272,7 +272,7 @@ class Project(UnifiedJobTemplate, ProjectOptions, ResourceMixin, CustomVirtualEn null=True, blank=True, default=None, - on_delete=models.SET_NULL, + on_delete=polymorphic.SET_NULL, related_name='+', help_text=_('The default execution environment for jobs run using this project.'), )