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)
This commit is contained in:
Jim Ladd 2021-02-10 13:03:19 -08:00
parent 0574baf7f7
commit 2f737f644f

View File

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