Skip to content

Commit 88ebc7f

Browse files
committed
fix: Fixed running DGN on non-PostgeSQL databases. An old JSONField index was blocking this, and even though the field got removed later on, the migration files were the problem.
Closes #23
1 parent bde773f commit 88ebc7f

File tree

4 files changed

+105
-25
lines changed

4 files changed

+105
-25
lines changed

example/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generic_notifications/migrations/0001_initial.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,50 @@
66
from django.db import migrations, models
77

88

9+
def create_postgresql_indexes(apps, schema_editor):
10+
"""Create indexes on PostgreSQL only. Other databases don't support JSONField indexes."""
11+
if schema_editor.connection.vendor != "postgresql":
12+
return
13+
14+
Notification = apps.get_model("generic_notifications", "Notification")
15+
16+
schema_editor.add_index(
17+
Notification, django.contrib.postgres.indexes.GinIndex(fields=["channels"], name="notification_channels_gin")
18+
)
19+
schema_editor.add_index(
20+
Notification, models.Index(fields=["recipient", "read", "channels"], name="notification_unread_channel")
21+
)
22+
schema_editor.add_index(
23+
Notification, models.Index(fields=["recipient", "channels"], name="notification_recipient_channel")
24+
)
25+
schema_editor.add_index(
26+
Notification,
27+
models.Index(fields=["recipient", "email_sent_at", "read", "channels"], name="notification_user_email_digest"),
28+
)
29+
30+
31+
def drop_postgresql_indexes(apps, schema_editor):
32+
"""Reverse operation: drop the PostgreSQL indexes."""
33+
if schema_editor.connection.vendor != "postgresql":
34+
return
35+
36+
Notification = apps.get_model("generic_notifications", "Notification")
37+
38+
schema_editor.remove_index(
39+
Notification, django.contrib.postgres.indexes.GinIndex(fields=["channels"], name="notification_channels_gin")
40+
)
41+
schema_editor.remove_index(
42+
Notification, models.Index(fields=["recipient", "read", "channels"], name="notification_unread_channel")
43+
)
44+
schema_editor.remove_index(
45+
Notification, models.Index(fields=["recipient", "channels"], name="notification_recipient_channel")
46+
)
47+
schema_editor.remove_index(
48+
Notification,
49+
models.Index(fields=["recipient", "email_sent_at", "read", "channels"], name="notification_user_email_digest"),
50+
)
51+
52+
953
class Migration(migrations.Migration):
1054
initial = True
1155

@@ -97,14 +141,11 @@ class Migration(migrations.Migration):
97141
],
98142
options={
99143
"ordering": ["-added"],
100-
"indexes": [
101-
django.contrib.postgres.indexes.GinIndex(fields=["channels"], name="notification_channels_gin"),
102-
models.Index(fields=["recipient", "read", "channels"], name="notification_unread_channel"),
103-
models.Index(fields=["recipient", "channels"], name="notification_recipient_channel"),
104-
models.Index(
105-
fields=["recipient", "email_sent_at", "read", "channels"], name="notification_user_email_digest"
106-
),
107-
],
108144
},
109145
),
146+
# Create PostgreSQL-specific indexes (skipped on SQL Server and other databases)
147+
migrations.RunPython(
148+
code=create_postgresql_indexes,
149+
reverse_code=drop_postgresql_indexes,
150+
),
110151
]

generic_notifications/migrations/0003_remove_notification_notification_channels_gin_and_more.py

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,56 @@
44
from django.db import migrations, models
55

66

7+
def remove_postgresql_indexes(apps, schema_editor):
8+
"""Remove PostgreSQL-specific indexes. Skip on other databases where they were never created."""
9+
if schema_editor.connection.vendor != "postgresql":
10+
return
11+
12+
Notification = apps.get_model("generic_notifications", "Notification")
13+
14+
# Import here to avoid issues with apps registry
15+
import django.contrib.postgres.indexes
16+
17+
# Remove the indexes that were created in migration 0001
18+
schema_editor.remove_index(
19+
Notification, django.contrib.postgres.indexes.GinIndex(fields=["channels"], name="notification_channels_gin")
20+
)
21+
schema_editor.remove_index(
22+
Notification, models.Index(fields=["recipient", "read", "channels"], name="notification_unread_channel")
23+
)
24+
schema_editor.remove_index(
25+
Notification, models.Index(fields=["recipient", "channels"], name="notification_recipient_channel")
26+
)
27+
schema_editor.remove_index(
28+
Notification,
29+
models.Index(fields=["recipient", "email_sent_at", "read", "channels"], name="notification_user_email_digest"),
30+
)
31+
32+
33+
def recreate_postgresql_indexes(apps, schema_editor):
34+
"""Reverse operation: recreate the PostgreSQL indexes."""
35+
if schema_editor.connection.vendor != "postgresql":
36+
return
37+
38+
Notification = apps.get_model("generic_notifications", "Notification")
39+
40+
import django.contrib.postgres.indexes
41+
42+
schema_editor.add_index(
43+
Notification, django.contrib.postgres.indexes.GinIndex(fields=["channels"], name="notification_channels_gin")
44+
)
45+
schema_editor.add_index(
46+
Notification, models.Index(fields=["recipient", "read", "channels"], name="notification_unread_channel")
47+
)
48+
schema_editor.add_index(
49+
Notification, models.Index(fields=["recipient", "channels"], name="notification_recipient_channel")
50+
)
51+
schema_editor.add_index(
52+
Notification,
53+
models.Index(fields=["recipient", "email_sent_at", "read", "channels"], name="notification_user_email_digest"),
54+
)
55+
56+
757
class Migration(migrations.Migration):
858
dependencies = [
959
("contenttypes", "0002_remove_content_type_name"),
@@ -12,21 +62,10 @@ class Migration(migrations.Migration):
1262
]
1363

1464
operations = [
15-
migrations.RemoveIndex(
16-
model_name="notification",
17-
name="notification_channels_gin",
18-
),
19-
migrations.RemoveIndex(
20-
model_name="notification",
21-
name="notification_unread_channel",
22-
),
23-
migrations.RemoveIndex(
24-
model_name="notification",
25-
name="notification_recipient_channel",
26-
),
27-
migrations.RemoveIndex(
28-
model_name="notification",
29-
name="notification_user_email_digest",
65+
# Remove PostgreSQL-specific indexes (skipped on SQL Server and other databases)
66+
migrations.RunPython(
67+
code=remove_postgresql_indexes,
68+
reverse_code=recreate_postgresql_indexes,
3069
),
3170
migrations.RemoveField(
3271
model_name="notification",

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "django-generic-notifications"
3-
version = "2.3.2"
3+
version = "2.3.3"
44
description = "A flexible, multi-channel notification system for Django applications with built-in support for email digests, user preferences, and extensible delivery channels."
55
authors = [
66
{name = "Kevin Renskers", email = "kevin@loopwerk.io"},

0 commit comments

Comments
 (0)