migrate event table primary keys from integer to bigint

see: https://github.com/ansible/awx/issues/6010
This commit is contained in:
Ryan Petrello
2020-02-21 13:03:24 -05:00
parent 3045511401
commit c8044b4755
5 changed files with 200 additions and 3 deletions

View File

@@ -3,6 +3,7 @@
# Django
from django.conf import settings # noqa
from django.db import connection, ProgrammingError
from django.db.models.signals import pre_delete # noqa
# AWX
@@ -79,6 +80,27 @@ User.add_to_class('can_access_with_errors', check_user_access_with_errors)
User.add_to_class('accessible_objects', user_accessible_objects)
def enforce_bigint_pk_migration():
# see: https://github.com/ansible/awx/issues/6010
# look at all the event tables and verify that they have been fully migrated
# from the *old* int primary key table to the replacement bigint table
# if not, attempt to migrate them in the background
for tblname in (
'main_jobevent', 'main_inventoryupdateevent',
'main_projectupdateevent', 'main_adhoccommandevent',
'main_systemjobevent'
):
with connection.cursor() as cursor:
try:
cursor.execute(f'SELECT MAX(id) FROM _old_{tblname}')
if cursor.fetchone():
from awx.main.tasks import migrate_legacy_event_data
migrate_legacy_event_data.apply_async([tblname])
except ProgrammingError:
# the table is gone (migration is unnecessary)
pass
def cleanup_created_modified_by(sender, **kwargs):
# work around a bug in django-polymorphic that doesn't properly
# handle cascades for reverse foreign keys on the polymorphic base model