Dynamically create initial partitions

* First partition holds all events up to this very moment
* And second partition starts where first left off and runs
  .. through rest of current hour
* From there, dynamically generated partitions will cover
  one hour at a time
This commit is contained in:
Jim Ladd 2021-02-16 19:41:48 -08:00
parent 48f1910075
commit fb30528197

View File

@ -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 '