|
| 1 | +from django.db import migrations, models |
| 2 | + |
| 3 | +""" |
| 4 | +Hi! I made this because I can't stop making the same mistake: Never use a third-party ID as foreign |
| 5 | +key, because every change will be painful. I am fixing this mistake here: We're going to walk over |
| 6 | +all tables that reference osmcal_user.osm_id and replace this with a serial id. |
| 7 | +This involves a little bit of fiddling, as you might see. |
| 8 | +""" |
| 9 | + |
| 10 | +user_id_dependent_tables = [ |
| 11 | + ("osmcal_eventlog", "created_by_id", "osmcal_eventlog_created_by_id_89c62fed_fk_osmcal_user_osm_id"), |
| 12 | + ("osmcal_eventparticipation", "user_id", "osmcal_eventparticip_user_id_8a2dfe0f_fk_osmcal_us"), |
| 13 | + ("osmcal_participationanswer", "user_id", "osmcal_participation_user_id_93228060_fk_osmcal_us"), |
| 14 | + ("osmcal_user_groups", "user_id", "osmcal_user_groups_user_id_c9d0a3d1_fk_osmcal_user_osm_id"), |
| 15 | + ("osmcal_user_user_permissions", "user_id", "osmcal_user_user_per_user_id_1ecd1641_fk_osmcal_us"), |
| 16 | + ("django_admin_log", "user_id", "django_admin_log_user_id_c564eba6_fk_osmcal_user_osm_id"), |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +def conversion_sql(): |
| 21 | + sql = "" |
| 22 | + for t in user_id_dependent_tables: |
| 23 | + sql += """ |
| 24 | + ALTER TABLE {0} DROP CONSTRAINT {2}; |
| 25 | + UPDATE {0} SET {1} = (SELECT id FROM osmcal_user WHERE osm_id = {0}.{1}); |
| 26 | + ALTER TABLE {0} ADD CONSTRAINT {2} FOREIGN KEY ({1}) REFERENCES osmcal_user (id); |
| 27 | + """.format(*t) |
| 28 | + return sql |
| 29 | + |
| 30 | + |
| 31 | +class Migration(migrations.Migration): |
| 32 | + |
| 33 | + dependencies = [ |
| 34 | + ('osmcal', '0022_event_cancelled'), |
| 35 | + ] |
| 36 | + |
| 37 | + operations = [ |
| 38 | + migrations.SeparateDatabaseAndState( |
| 39 | + database_operations=[ |
| 40 | + migrations.RunSQL([ |
| 41 | + 'ALTER TABLE osmcal_user ADD column id serial UNIQUE;', |
| 42 | + conversion_sql(), |
| 43 | + 'ALTER TABLE osmcal_user DROP CONSTRAINT osmcal_user_pkey;', |
| 44 | + 'ALTER TABLE osmcal_user ADD PRIMARY KEY (id);', |
| 45 | + ]) |
| 46 | + ], |
| 47 | + state_operations=[ |
| 48 | + migrations.AddField( |
| 49 | + model_name='user', |
| 50 | + name='id', |
| 51 | + field=models.AutoField(primary_key=True, serialize=False), |
| 52 | + preserve_default=False, |
| 53 | + ), |
| 54 | + migrations.AlterField( |
| 55 | + model_name='user', |
| 56 | + name='osm_id', |
| 57 | + field=models.IntegerField(), |
| 58 | + ), |
| 59 | + ] |
| 60 | + ) |
| 61 | + ] |
0 commit comments