Skip to content

Commit 1fa05e4

Browse files
committed
[change] Use SeparateDatabaseAndState for JSONField migration #673
Optimized PostgreSQL schema with jsonb and GIN indexes while ensuring SpatiaLite compatibility via vendor checks. Verified in local Docker environment. Closes #673
1 parent 399f06c commit 1fa05e4

File tree

4 files changed

+85
-25
lines changed

4 files changed

+85
-25
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ jobs:
9292
if: ${{ !cancelled() && steps.deps.conclusion == 'success' }}
9393
run: docker compose up -d influxdb
9494

95+
- name: Wait for InfluxDB
96+
if: ${{ !cancelled() && steps.deps.conclusion == 'success' }}
97+
run: |
98+
for i in {1..20}; do
99+
if curl -sI http://localhost:8086/ping | grep "204" > /dev/null; then
100+
echo "InfluxDB is ready!"
101+
exit 0
102+
fi
103+
echo "Waiting for InfluxDB to initialize..."
104+
sleep 3
105+
done
106+
echo "Error: InfluxDB timed out"
107+
exit 1
108+
95109
- name: QA checks
96110
run: |
97111
./run-qa-checks

openwisp_monitoring/monitoring/migrations/0013_auto_20260224_0730.py

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from django.db import migrations, models
2+
3+
4+
def migrate_to_jsonb(apps, schema_editor):
5+
if schema_editor.connection.vendor != "postgresql":
6+
return
7+
with schema_editor.connection.cursor() as cursor:
8+
cursor.execute(
9+
"DROP INDEX IF EXISTS monitoring_metric_main_tags_2d8550ae_like; "
10+
"DROP INDEX IF EXISTS monitoring_metric_main_tags_2d8550ae;"
11+
)
12+
cursor.execute(
13+
"ALTER TABLE monitoring_metric ALTER COLUMN main_tags TYPE jsonb USING main_tags::jsonb; "
14+
"ALTER TABLE monitoring_metric ALTER COLUMN extra_tags TYPE jsonb USING extra_tags::jsonb;"
15+
)
16+
cursor.execute(
17+
"CREATE INDEX IF NOT EXISTS main_tags_gin_idx ON monitoring_metric USING gin (main_tags); "
18+
"CREATE INDEX IF NOT EXISTS extra_tags_gin_idx ON monitoring_metric USING gin (extra_tags);"
19+
)
20+
21+
22+
def rollback_jsonb(apps, schema_editor):
23+
if schema_editor.connection.vendor != "postgresql":
24+
return
25+
with schema_editor.connection.cursor() as cursor:
26+
cursor.execute(
27+
"DROP INDEX IF EXISTS main_tags_gin_idx; DROP INDEX IF EXISTS extra_tags_gin_idx;"
28+
)
29+
cursor.execute(
30+
"ALTER TABLE monitoring_metric ALTER COLUMN main_tags TYPE text; "
31+
"ALTER TABLE monitoring_metric ALTER COLUMN extra_tags TYPE text;"
32+
)
33+
34+
35+
class Migration(migrations.Migration):
36+
dependencies = [
37+
("monitoring", "0012_migrate_signal_metrics"),
38+
]
39+
40+
operations = [
41+
migrations.SeparateDatabaseAndState(
42+
state_operations=[
43+
migrations.AlterField(
44+
model_name="metric",
45+
name="main_tags",
46+
field=models.JSONField(
47+
blank=True,
48+
default=dict,
49+
help_text="main tags",
50+
verbose_name="main tags",
51+
),
52+
),
53+
migrations.AlterField(
54+
model_name="metric",
55+
name="extra_tags",
56+
field=models.JSONField(
57+
blank=True,
58+
default=dict,
59+
help_text="extra tags",
60+
verbose_name="extra tags",
61+
),
62+
),
63+
],
64+
database_operations=[
65+
migrations.RunPython(migrate_to_jsonb, reverse_code=rollback_jsonb),
66+
],
67+
),
68+
]

requirements-test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ django-redis~=6.0.0
33
mock-ssh-server~=0.9.1
44
channels_redis~=4.3.0
55
freezegun~=1.5.5
6+
commitizen~=4.13.0
7+
prompt-toolkit!=3.0.52
8+
django-filter<25.0

0 commit comments

Comments
 (0)