mirror of
https://github.com/ansible/awx.git
synced 2026-03-20 10:27:34 -02:30
Merge pull request #3003 from AlanCoding/2988_team_cascade
Cascade delete teams from organization
This commit is contained in:
20
awx/main/migrations/0027_v300_team_migrations.py
Normal file
20
awx/main/migrations/0027_v300_team_migrations.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from awx.main.migrations import _rbac as rbac
|
||||||
|
from awx.main.migrations import _team_cleanup as team_cleanup
|
||||||
|
from awx.main.migrations import _migration_utils as migration_utils
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('main', '0026_v300_credential_unique'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(migration_utils.set_current_apps_for_migrations),
|
||||||
|
migrations.RunPython(team_cleanup.migrate_team),
|
||||||
|
migrations.RunPython(rbac.rebuild_role_hierarchy),
|
||||||
|
]
|
||||||
21
awx/main/migrations/0028_v300_org_team_cascade.py
Normal file
21
awx/main/migrations/0028_v300_org_team_cascade.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import awx.main.fields
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('main', '0027_v300_team_migrations'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='team',
|
||||||
|
name='organization',
|
||||||
|
field=models.ForeignKey(related_name='teams', to='main.Organization'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
30
awx/main/migrations/_team_cleanup.py
Normal file
30
awx/main/migrations/_team_cleanup.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# Python
|
||||||
|
import logging
|
||||||
|
from django.utils.encoding import smart_text
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def log_migration(wrapped):
|
||||||
|
'''setup the logging mechanism for each migration method
|
||||||
|
as it runs, Django resets this, so we use a decorator
|
||||||
|
to re-add the handler for each method.
|
||||||
|
'''
|
||||||
|
handler = logging.FileHandler("/tmp/tower_rbac_migrations.log", mode="a", encoding="UTF-8")
|
||||||
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
|
handler.setLevel(logging.DEBUG)
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
logger.handlers = []
|
||||||
|
logger.addHandler(handler)
|
||||||
|
return wrapped(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
@log_migration
|
||||||
|
def migrate_team(apps, schema_editor):
|
||||||
|
'''If an orphan team exists that is still active, delete it.'''
|
||||||
|
Team = apps.get_model('main', 'Team')
|
||||||
|
for team in Team.objects.iterator():
|
||||||
|
if team.organization is None:
|
||||||
|
logger.info(smart_text(u"Deleting orphaned team: {}".format(team.name)))
|
||||||
|
team.delete()
|
||||||
@@ -92,8 +92,8 @@ class Team(CommonModelNameNotUnique, ResourceMixin):
|
|||||||
organization = models.ForeignKey(
|
organization = models.ForeignKey(
|
||||||
'Organization',
|
'Organization',
|
||||||
blank=False,
|
blank=False,
|
||||||
null=True,
|
null=False,
|
||||||
on_delete=models.SET_NULL,
|
on_delete=models.CASCADE,
|
||||||
related_name='teams',
|
related_name='teams',
|
||||||
)
|
)
|
||||||
deprecated_projects = models.ManyToManyField(
|
deprecated_projects = models.ManyToManyField(
|
||||||
|
|||||||
Reference in New Issue
Block a user