diff --git a/snpdb/migrations/0145_one_off_handle_renamed_table_inherited_index.py b/snpdb/migrations/0145_one_off_handle_renamed_table_inherited_index.py new file mode 100644 index 000000000..d9f03de33 --- /dev/null +++ b/snpdb/migrations/0145_one_off_handle_renamed_table_inherited_index.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.15 on 2024-09-09 07:09 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('snpdb', '0144_add_vgc_descriptions'), + ] + + operations = [ + # We need to explicitly inherit sequences now (see RelatedModelsPartitionModel) + # and when Django renames tables it doesn't rename indexes, so we need to rename it in old deployments + migrations.RunSQL(sql=""" + DO $$ + BEGIN + -- Check if the sequence exists + IF EXISTS (SELECT 1 FROM pg_class WHERE relname = 'snpdb_variantcount_id_seq' AND relkind = 'S') THEN + -- Rename the sequence + ALTER SEQUENCE snpdb_variantcount_id_seq RENAME TO snpdb_cohortgenotype_id_seq; + + -- Update the table's column default to use the new sequence name + ALTER TABLE snpdb_cohortgenotype + ALTER COLUMN id + SET DEFAULT nextval('snpdb_cohortgenotype_id_seq'::regclass); + END IF; + END $$; + """, reverse_sql=migrations.RunSQL.noop) + ]