first draft of db partitioning

This commit is contained in:
Jim Ladd
2021-01-27 14:02:02 -08:00
parent 12f2975809
commit c6acca08d5

View File

@@ -0,0 +1,63 @@
# Generated by Django 2.2.8 on 2020-02-21 16:31
from django.db import migrations, models, connection
def migrate_event_data(apps, schema_editor):
# see: https://github.com/ansible/awx/issues/9039
#
# the goal of this function is to:
# - [ ] create a parent partition table, main_jobevent_parent
# - [ ] .. with a single partition
# - [ ] .. that includes all existing job events
#
# the new main_jobevent_parent table should have a new
# denormalized column, job_created, this is used as a
# basis for partitioning job event rows
#
# The initial partion will be a unique case. After
# the migration is completed, awx should create
# new partitions on an hourly basis, as needed.
# All events for a given job should be placed in
# a partition based on the job's _created time_.
# Only partitioning main_jobevent on first pass
#
#for tblname in (
# 'main_jobevent', 'main_inventoryupdateevent',
# 'main_projectupdateevent', 'main_adhoccommandevent',
# 'main_systemjobevent'
#):
for tblname in ('main_jobevent',):
with connection.cursor() as cursor:
# hacky creation of parent table for partition
cursor.execute(
f'CREATE TABLE {tblname}_parent '
f'(LIKE {tblname}, job_created TIMESTAMP WITH TIME ZONE NOT NULL) '
f'PARTITION BY RANGE(job_created);'
)
# .. as well as initial partition containing all existing events
cursor.execute(
f'CREATE TABLE {tblname}_part0 '
f'PARTITION OF {tblname}_parent '
f'FOR VALUES FROM (\'2000-01-01 00:00:00.000000+00\') to (\'2021-02-01 00:00:00.000000+00\');'
)
# copy over all job events into partitioned table
cursor.execute(
f'INSERT INTO {tblname}_parent '
f'SELECT {tblname}.*, main_unifiedjob.created '
f'FROM {tblname} '
f'INNER JOIN main_unifiedjob ON {tblname}.job_id = main_unifiedjob.id;'
)
class Migration(migrations.Migration):
dependencies = [
('main', '0123_drop_hg_support'),
]
operations = [
migrations.RunPython(migrate_event_data),
]