Skip to content

Commit 7890094

Browse files
committed
user: migrating to serial user ID #19
1 parent 0526c3c commit 7890094

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
]

osmcal/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ class EventLog(models.Model):
114114

115115

116116
class User(AbstractUser):
117-
osm_id = models.IntegerField(primary_key=True)
117+
id = models.AutoField(primary_key=True)
118+
osm_id = models.IntegerField()
118119
name = models.CharField(max_length=255)
119120

120121
def save(self, *args, **kwargs):

0 commit comments

Comments
 (0)