|
| 1 | +# Custom migration: switch primary key from segment_id to auto-generated id |
| 2 | +# for DarkFiberSegmentData, EthernetServiceSegmentData, OpticalSpectrumSegmentData. |
| 3 | +# |
| 4 | +# Django's auto-generated migration fails because it tries to add a new PK |
| 5 | +# column while the old PK constraint still exists. This migration uses raw SQL |
| 6 | +# to do the steps in the correct order: |
| 7 | +# 1. Drop the existing PK constraint on segment_id |
| 8 | +# 2. Add a new "id" identity column as the primary key |
| 9 | +# 3. Add a UNIQUE constraint on segment_id (required by OneToOneField) |
| 10 | +# 4. Register the state changes so Django's ORM stays in sync |
| 11 | + |
| 12 | +import django.db.models.deletion |
| 13 | +from django.db import migrations, models |
| 14 | + |
| 15 | + |
| 16 | +# (table_name, old_pkey_constraint_name) |
| 17 | +# Constraint names come from the \d output in Postgres. |
| 18 | +TABLES = [ |
| 19 | + ( |
| 20 | + "cesnet_service_path_plugin_darkfibersegmentdata", |
| 21 | + "cesnet_service_path_plugin_darkfibersegmentdata_pkey", |
| 22 | + ), |
| 23 | + ( |
| 24 | + "cesnet_service_path_plugin_ethernetservicesegmentdata", |
| 25 | + "cesnet_service_path_plugin_ethernetservicesegmentdata_pkey", |
| 26 | + ), |
| 27 | + ( |
| 28 | + "cesnet_service_path_plugin_opticalspectrumsegmentdata", |
| 29 | + "cesnet_service_path_plugin_opticalspectrumsegmentdata_pkey", |
| 30 | + ), |
| 31 | +] |
| 32 | + |
| 33 | + |
| 34 | +def forwards(apps, schema_editor): |
| 35 | + for table, pkey_name in TABLES: |
| 36 | + # 1. Drop the existing primary key (segment_id) |
| 37 | + schema_editor.execute( |
| 38 | + f'ALTER TABLE "{table}" DROP CONSTRAINT "{pkey_name}";' |
| 39 | + ) |
| 40 | + # 2. Add new "id" column as identity primary key |
| 41 | + schema_editor.execute( |
| 42 | + f'ALTER TABLE "{table}" ADD COLUMN "id" bigint NOT NULL ' |
| 43 | + f"GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY;" |
| 44 | + ) |
| 45 | + # 3. Add unique constraint on segment_id (OneToOneField needs it) |
| 46 | + schema_editor.execute( |
| 47 | + f'ALTER TABLE "{table}" ADD CONSTRAINT ' |
| 48 | + f'"{table}_segment_id_key" UNIQUE ("segment_id");' |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def backwards(apps, schema_editor): |
| 53 | + for table, pkey_name in TABLES: |
| 54 | + # Reverse: drop the unique constraint, drop id column, re-add old PK |
| 55 | + schema_editor.execute( |
| 56 | + f'ALTER TABLE "{table}" DROP CONSTRAINT "{table}_segment_id_key";' |
| 57 | + ) |
| 58 | + schema_editor.execute( |
| 59 | + f'ALTER TABLE "{table}" DROP COLUMN "id";' |
| 60 | + ) |
| 61 | + schema_editor.execute( |
| 62 | + f'ALTER TABLE "{table}" ADD CONSTRAINT "{pkey_name}" ' |
| 63 | + f'PRIMARY KEY ("segment_id");' |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +class Migration(migrations.Migration): |
| 68 | + |
| 69 | + dependencies = [ |
| 70 | + ( |
| 71 | + "cesnet_service_path_plugin", |
| 72 | + "0036_alter_opticalspectrumsegmentdata_chromatic_dispersion", |
| 73 | + ), |
| 74 | + ] |
| 75 | + |
| 76 | + operations = [ |
| 77 | + # Raw SQL handles the actual schema change |
| 78 | + migrations.RunPython(forwards, backwards), |
| 79 | + # State-only operations so Django's ORM knows about the new fields |
| 80 | + migrations.SeparateDatabaseAndState( |
| 81 | + state_operations=[ |
| 82 | + migrations.AddField( |
| 83 | + model_name="darkfibersegmentdata", |
| 84 | + name="id", |
| 85 | + field=models.BigAutoField( |
| 86 | + auto_created=True, primary_key=True, serialize=False |
| 87 | + ), |
| 88 | + ), |
| 89 | + migrations.AlterField( |
| 90 | + model_name="darkfibersegmentdata", |
| 91 | + name="segment", |
| 92 | + field=models.OneToOneField( |
| 93 | + on_delete=django.db.models.deletion.CASCADE, |
| 94 | + related_name="dark_fiber_data", |
| 95 | + to="cesnet_service_path_plugin.segment", |
| 96 | + ), |
| 97 | + ), |
| 98 | + migrations.AddField( |
| 99 | + model_name="ethernetservicesegmentdata", |
| 100 | + name="id", |
| 101 | + field=models.BigAutoField( |
| 102 | + auto_created=True, primary_key=True, serialize=False |
| 103 | + ), |
| 104 | + ), |
| 105 | + migrations.AlterField( |
| 106 | + model_name="ethernetservicesegmentdata", |
| 107 | + name="segment", |
| 108 | + field=models.OneToOneField( |
| 109 | + on_delete=django.db.models.deletion.CASCADE, |
| 110 | + related_name="ethernet_service_data", |
| 111 | + to="cesnet_service_path_plugin.segment", |
| 112 | + ), |
| 113 | + ), |
| 114 | + migrations.AddField( |
| 115 | + model_name="opticalspectrumsegmentdata", |
| 116 | + name="id", |
| 117 | + field=models.BigAutoField( |
| 118 | + auto_created=True, primary_key=True, serialize=False |
| 119 | + ), |
| 120 | + ), |
| 121 | + migrations.AlterField( |
| 122 | + model_name="opticalspectrumsegmentdata", |
| 123 | + name="segment", |
| 124 | + field=models.OneToOneField( |
| 125 | + on_delete=django.db.models.deletion.CASCADE, |
| 126 | + related_name="optical_spectrum_data", |
| 127 | + to="cesnet_service_path_plugin.segment", |
| 128 | + ), |
| 129 | + ), |
| 130 | + ], |
| 131 | + database_operations=[], # Already handled by RunPython above |
| 132 | + ), |
| 133 | + ] |
0 commit comments