Skip to content

Commit 7e209fd

Browse files
committed
fix: fix migration problem
1 parent 4d9786f commit 7e209fd

1 file changed

Lines changed: 59 additions & 26 deletions

File tree

users/migrations/0022_user_country_user_hide_badges_and_more.py

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
import django_countries.fields
44
from django.db import migrations, models
55

6+
# This migration shipped to long-lived environments under several different
7+
# names, so some databases have its columns already present but recorded under
8+
# a name that no longer exists on disk. Emitting the DDL with IF NOT EXISTS
9+
# makes it a no-op wherever the columns are already there, which lets the file
10+
# keep a stable name regardless of what a given database recorded.
11+
ADD_COLUMNS = """
12+
ALTER TABLE "users_user" ADD COLUMN IF NOT EXISTS "country" varchar(2) DEFAULT '' NOT NULL;
13+
ALTER TABLE "users_user" ALTER COLUMN "country" DROP DEFAULT;
14+
ALTER TABLE "users_user" ADD COLUMN IF NOT EXISTS "hide_badges" boolean DEFAULT false NOT NULL;
15+
ALTER TABLE "users_user" ALTER COLUMN "hide_badges" DROP DEFAULT;
16+
ALTER TABLE "users_user" ADD COLUMN IF NOT EXISTS "hide_github_activity" boolean DEFAULT false NOT NULL;
17+
ALTER TABLE "users_user" ALTER COLUMN "hide_github_activity" DROP DEFAULT;
18+
ALTER TABLE "users_user" ADD COLUMN IF NOT EXISTS "hide_mailing_list_activity" boolean DEFAULT false NOT NULL;
19+
ALTER TABLE "users_user" ALTER COLUMN "hide_mailing_list_activity" DROP DEFAULT;
20+
"""
21+
22+
DROP_COLUMNS = """
23+
ALTER TABLE "users_user" DROP COLUMN IF EXISTS "hide_mailing_list_activity";
24+
ALTER TABLE "users_user" DROP COLUMN IF EXISTS "hide_github_activity";
25+
ALTER TABLE "users_user" DROP COLUMN IF EXISTS "hide_badges";
26+
ALTER TABLE "users_user" DROP COLUMN IF EXISTS "country";
27+
"""
28+
629

730
class Migration(migrations.Migration):
831

@@ -11,31 +34,41 @@ class Migration(migrations.Migration):
1134
]
1235

1336
operations = [
14-
migrations.AddField(
15-
model_name="user",
16-
name="country",
17-
field=django_countries.fields.CountryField(blank=True, max_length=2),
18-
),
19-
migrations.AddField(
20-
model_name="user",
21-
name="hide_badges",
22-
field=models.BooleanField(
23-
default=False, help_text="Hide badges from the public profile."
24-
),
25-
),
26-
migrations.AddField(
27-
model_name="user",
28-
name="hide_github_activity",
29-
field=models.BooleanField(
30-
default=False, help_text="Hide GitHub activity from the public profile."
31-
),
32-
),
33-
migrations.AddField(
34-
model_name="user",
35-
name="hide_mailing_list_activity",
36-
field=models.BooleanField(
37-
default=False,
38-
help_text="Hide mailing list activity from the public profile.",
39-
),
37+
migrations.SeparateDatabaseAndState(
38+
database_operations=[
39+
migrations.RunSQL(ADD_COLUMNS, DROP_COLUMNS),
40+
],
41+
state_operations=[
42+
migrations.AddField(
43+
model_name="user",
44+
name="country",
45+
field=django_countries.fields.CountryField(
46+
blank=True, max_length=2
47+
),
48+
),
49+
migrations.AddField(
50+
model_name="user",
51+
name="hide_badges",
52+
field=models.BooleanField(
53+
default=False, help_text="Hide badges from the public profile."
54+
),
55+
),
56+
migrations.AddField(
57+
model_name="user",
58+
name="hide_github_activity",
59+
field=models.BooleanField(
60+
default=False,
61+
help_text="Hide GitHub activity from the public profile.",
62+
),
63+
),
64+
migrations.AddField(
65+
model_name="user",
66+
name="hide_mailing_list_activity",
67+
field=models.BooleanField(
68+
default=False,
69+
help_text="Hide mailing list activity from the public profile.",
70+
),
71+
),
72+
],
4073
),
4174
]

0 commit comments

Comments
 (0)