Skip to content

Commit e6370fa

Browse files
committed
[change] Use SeparateDatabaseAndState for JSONField migration #673
Optimized PostgreSQL with jsonb and GIN indexes. Synchronized migration state for both main and sample apps with models. Verified locally in Docker environment. Closes #673
1 parent 5bfcaa9 commit e6370fa

File tree

5 files changed

+119
-25
lines changed

5 files changed

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

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

tests/openwisp2/sample_monitoring/migrations/0004_alter_metric_field_name.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by Django 3.2.15 on 2022-09-08 11:11
22

3+
import django.contrib.postgres.indexes
34
from django.db import migrations, models
45

56

@@ -19,4 +20,23 @@ class Migration(migrations.Migration):
1920
max_length=16,
2021
),
2122
),
23+
migrations.SeparateDatabaseAndState(
24+
state_operations=[
25+
migrations.AddIndex(
26+
model_name="metric",
27+
index=django.contrib.postgres.indexes.GinIndex(
28+
fields=["main_tags"],
29+
name="main_tags_gin_idx",
30+
),
31+
),
32+
migrations.AddIndex(
33+
model_name="metric",
34+
index=django.contrib.postgres.indexes.GinIndex(
35+
fields=["extra_tags"],
36+
name="extra_tags_gin_idx",
37+
),
38+
),
39+
],
40+
database_operations=[],
41+
),
2242
]

0 commit comments

Comments
 (0)