From 2f737f644f86b6420612a545c1e1d6eede570bdd Mon Sep 17 00:00:00 2001 From: Jim Ladd Date: Wed, 10 Feb 2021 13:03:19 -0800 Subject: [PATCH] Drop primary key index before creating partition table * Partition tables require unique contstraints to include the partition key (uniqueness can only be enforced _inside_ of a given partition table) --- awx/main/migrations/0124_event_partitions.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/awx/main/migrations/0124_event_partitions.py b/awx/main/migrations/0124_event_partitions.py index 3ca20730fb..dfcef73143 100644 --- a/awx/main/migrations/0124_event_partitions.py +++ b/awx/main/migrations/0124_event_partitions.py @@ -37,13 +37,27 @@ def migrate_event_data(apps, schema_editor): f'ALTER TABLE {tblname} RENAME TO {tblname}_old' ) - # hacky creation of parent table for partition + # drop primary key constraint; in a partioned table + # constraints must include the partition key itself + # TODO: do more generic search for pkey constraints + # instead of hardcoding this one that applies to main_jobevent + cursor.execute( + f'ALTER TABLE {tblname}_old DROP CONSTRAINT {tblname}_pkey1' + ) + + # create parent table cursor.execute( f'CREATE TABLE {tblname} ' f'(LIKE {tblname}_old INCLUDING ALL, job_created TIMESTAMP WITH TIME ZONE NOT NULL) ' f'PARTITION BY RANGE(job_created);' ) + # recreate primary key constraint + cursor.execute( + f'ALTER TABLE ONLY {tblname} ' + f'ADD CONSTRAINT {tblname}_pkey PRIMARY KEY (id, job_created);' + ) + # .. as well as initial partition containing all existing events cursor.execute( f'CREATE TABLE {tblname}_part0 '