diff --git a/awx/main/migrations/0124_event_partitions.py b/awx/main/migrations/0124_event_partitions.py index ffff8f5397..a8a7c9b50f 100644 --- a/awx/main/migrations/0124_event_partitions.py +++ b/awx/main/migrations/0124_event_partitions.py @@ -60,12 +60,18 @@ def migrate_event_data(apps, schema_editor): # recreate primary key constraint cursor.execute( f'ALTER TABLE ONLY {tblname} ' - f'ADD CONSTRAINT {tblname}_pkey PRIMARY KEY (id, job_created);' + f'ADD CONSTRAINT {tblname}_pkey_new PRIMARY KEY (id, job_created);' ) + current_time = now() + # .. as well as initial partition containing all existing events epoch = datetime(1980, 1, 1, 0, 0) - create_partition('old_events', epoch, now()) + create_partition(epoch, current_time, 'old_events') + + # .. and first partition + # .. which is a special case, as it only covers remainder of current hour + create_partition(current_time) # copy over all job events into partitioned table # TODO: https://github.com/ansible/awx/issues/9257 diff --git a/awx/main/utils/common.py b/awx/main/utils/common.py index be3732eb93..6715b5268a 100644 --- a/awx/main/utils/common.py +++ b/awx/main/utils/common.py @@ -1009,7 +1009,6 @@ def truncate_stdout(stdout, size): return stdout + u'\u001b[0m' * (set_count - reset_count) - def deepmerge(a, b): """ Merge dict structures and return the result. @@ -1027,12 +1026,16 @@ def deepmerge(a, b): return b -def create_partition(partition_label, start, end=None): - """Creates new partition tables for events.""" +def create_partition(start, end=None, partition_label=None): + """Creates new partition tables for events. + If not specified, end is set to the end of the current hour.""" if not end: - end = (now() + timedelta(hours=1)) - start_timestamp = start.strftime('%Y-%m-%d %H:00:00.000000%z') - end_timestamp = end.strftime('%Y-%m-%d %H:00:00.000000%z') + end = start.replace(microsecond=0, second=0, minute=0) + timedelta(hours=1) + start_timestamp = str(start) + end_timestamp = str(end) + + if not partition_label: + partition_label = start.strftime('%Y_%m_%d_%H') with connection.cursor() as cursor: # Only partitioning main_jobevent on first pass