|
| 1 | +"""Convert Attribute.target_servertype from ForeignKey to ManyToManyField. |
| 2 | +
|
| 3 | +Uses SeparateDatabaseAndState so that: |
| 4 | +- Django's state tracker sees standard RemoveField + AddField operations |
| 5 | +- The actual DB operations are controlled manually to allow a data |
| 6 | + migration between creating the M2M table and dropping the FK column |
| 7 | +
|
| 8 | +Steps: |
| 9 | +1. Create the M2M join table |
| 10 | +2. Copy existing FK data into the join table |
| 11 | +3. Drop the old FK column |
| 12 | +""" |
| 13 | + |
| 14 | +import django.db.models |
| 15 | +from django.db import migrations, models |
| 16 | + |
| 17 | + |
| 18 | +def copy_fk_to_m2m(apps, schema_editor): |
| 19 | + """Copy existing target_servertype_id FK values to the new M2M table.""" |
| 20 | + with schema_editor.connection.cursor() as cursor: |
| 21 | + cursor.execute( |
| 22 | + "INSERT INTO attribute_target_servertype (attribute_id, servertype_id) " |
| 23 | + "SELECT attribute_id, target_servertype_id FROM attribute " |
| 24 | + "WHERE target_servertype_id IS NOT NULL" |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def copy_m2m_to_fk(apps, schema_editor): |
| 29 | + """Reverse: copy M2M values back into the FK column (takes first value).""" |
| 30 | + with schema_editor.connection.cursor() as cursor: |
| 31 | + cursor.execute( |
| 32 | + "UPDATE attribute SET target_servertype_id = m2m.servertype_id " |
| 33 | + "FROM attribute_target_servertype AS m2m " |
| 34 | + "WHERE attribute.attribute_id = m2m.attribute_id" |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +class Migration(migrations.Migration): |
| 39 | + |
| 40 | + dependencies = [ |
| 41 | + ('serverdb', '0022_attribute_relax_target_servertype_constraints') |
| 42 | + ] |
| 43 | + |
| 44 | + operations = [ |
| 45 | + # Phase 1: Create the M2M join table (DB only, state updated later). |
| 46 | + migrations.SeparateDatabaseAndState( |
| 47 | + database_operations=[ |
| 48 | + migrations.RunSQL( |
| 49 | + sql=( |
| 50 | + "CREATE TABLE attribute_target_servertype (" |
| 51 | + " id BIGSERIAL PRIMARY KEY," |
| 52 | + " attribute_id VARCHAR(32) NOT NULL" |
| 53 | + " REFERENCES attribute(attribute_id) ON DELETE CASCADE," |
| 54 | + " servertype_id VARCHAR(32) NOT NULL" |
| 55 | + " REFERENCES servertype(servertype_id) ON DELETE CASCADE," |
| 56 | + " UNIQUE (attribute_id, servertype_id)" |
| 57 | + ")" |
| 58 | + ), |
| 59 | + reverse_sql="DROP TABLE IF EXISTS attribute_target_servertype", |
| 60 | + ), |
| 61 | + ], |
| 62 | + state_operations=[], |
| 63 | + ), |
| 64 | + |
| 65 | + # Phase 2: Copy existing FK data into the M2M table. |
| 66 | + migrations.RunPython(copy_fk_to_m2m, copy_m2m_to_fk), |
| 67 | + |
| 68 | + # Phase 3: Drop old FK column + constraints; update Django state. |
| 69 | + migrations.SeparateDatabaseAndState( |
| 70 | + database_operations=[ |
| 71 | + migrations.RunSQL( |
| 72 | + sql=( |
| 73 | + "ALTER TABLE attribute " |
| 74 | + "DROP CONSTRAINT IF EXISTS" |
| 75 | + " attribute_target_servertype_id_0eab2dcc_fk_servertyp" |
| 76 | + ), |
| 77 | + reverse_sql=( |
| 78 | + "ALTER TABLE attribute ADD CONSTRAINT" |
| 79 | + " attribute_target_servertype_id_0eab2dcc_fk_servertyp" |
| 80 | + " FOREIGN KEY (target_servertype_id)" |
| 81 | + " REFERENCES servertype(servertype_id)" |
| 82 | + " DEFERRABLE INITIALLY DEFERRED" |
| 83 | + ), |
| 84 | + ), |
| 85 | + migrations.RunSQL( |
| 86 | + sql=( |
| 87 | + "ALTER TABLE attribute " |
| 88 | + "DROP COLUMN IF EXISTS target_servertype_id" |
| 89 | + ), |
| 90 | + reverse_sql=( |
| 91 | + "ALTER TABLE attribute ADD COLUMN target_servertype_id " |
| 92 | + "VARCHAR(32)" |
| 93 | + ), |
| 94 | + ), |
| 95 | + ], |
| 96 | + state_operations=[ |
| 97 | + migrations.RemoveField( |
| 98 | + model_name='attribute', |
| 99 | + name='target_servertype', |
| 100 | + ), |
| 101 | + migrations.AddField( |
| 102 | + model_name='attribute', |
| 103 | + name='target_servertype', |
| 104 | + field=models.ManyToManyField( |
| 105 | + blank=True, |
| 106 | + help_text='Selecting no servertype allows all servertypes.', |
| 107 | + to='serverdb.servertype', |
| 108 | + ), |
| 109 | + ), |
| 110 | + ], |
| 111 | + ), |
| 112 | + ] |
0 commit comments