diff --git a/awx/main/migrations/0124_event_partitions.py b/awx/main/migrations/0124_event_partitions.py index 26ea187144..e9a8bb8c85 100644 --- a/awx/main/migrations/0124_event_partitions.py +++ b/awx/main/migrations/0124_event_partitions.py @@ -1,6 +1,9 @@ # Generated by Django 2.2.8 on 2020-02-21 16:31 +from datetime import timedelta + from django.db import migrations, models, connection +from django.utils.timezone import now def migrate_event_data(apps, schema_editor): @@ -59,13 +62,28 @@ def migrate_event_data(apps, schema_editor): ) # .. as well as initial partition containing all existing events + current_time = now() # only retrieve current time once to avoid a boundary error + end_timestamp = current_time.strftime('%Y-%m-%d %H:%M:%S.000000%z') cursor.execute( - f'CREATE TABLE {tblname}_part0 ' + f'CREATE TABLE {tblname}_old_events ' f'PARTITION OF {tblname} ' - f'FOR VALUES FROM (\'2000-01-01 00:00:00.000000+00\') to (\'2021-02-01 00:00:00.000000+00\');' + f'FOR VALUES FROM (\'1980-01-01 00:00:00.000000+00\') to (\'{end_timestamp}\');' + ) + + # First partition is a special case since it runs up through this moment + # Go ahead and create next partition, since it will also need to be a + # custom partition (that accounts for the remainder of the current hour) + partition_label = current_time.strftime('%Y%m%d_%H') + start_timestamp = end_timestamp + end_timestamp = (current_time + timedelta(hours=1)).strftime('%Y-%m-%d %H:00:00.000000%z') + cursor.execute( + f'CREATE TABLE {tblname}_{partition_label} ' + f'PARTITION OF {tblname} ' + f'FOR VALUES FROM (\'{start_timestamp}\') to (\'{end_timestamp}\');' ) # copy over all job events into partitioned table + # TODO: https://github.com/ansible/awx/issues/9257 cursor.execute( f'INSERT INTO {tblname} ' f'SELECT {tblname}_old.*, main_unifiedjob.created '