Skip to content

Commit 2012a75

Browse files
committed
[refactor] 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 2012a75

4 files changed

Lines changed: 77 additions & 25 deletions

File tree

.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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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("DROP INDEX IF EXISTS main_tags_gin_idx; DROP INDEX IF EXISTS extra_tags_gin_idx;")
27+
cursor.execute(
28+
"ALTER TABLE monitoring_metric ALTER COLUMN main_tags TYPE text; "
29+
"ALTER TABLE monitoring_metric ALTER COLUMN extra_tags TYPE text;"
30+
)
31+
32+
33+
class Migration(migrations.Migration):
34+
dependencies = [
35+
("monitoring", "0012_migrate_signal_metrics"),
36+
]
37+
38+
operations = [
39+
migrations.SeparateDatabaseAndState(
40+
state_operations=[
41+
migrations.AlterField(
42+
model_name="metric",
43+
name="main_tags",
44+
field=models.JSONField(
45+
blank=True, default=dict, help_text="main tags", verbose_name="main tags"
46+
),
47+
),
48+
migrations.AlterField(
49+
model_name="metric",
50+
name="extra_tags",
51+
field=models.JSONField(
52+
blank=True, default=dict, help_text="extra tags", verbose_name="extra tags"
53+
),
54+
),
55+
],
56+
database_operations=[
57+
migrations.RunPython(migrate_to_jsonb, reverse_code=rollback_jsonb),
58+
],
59+
),
60+
]

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)