rewrite migration to stay compatible with sqlite

This commit is contained in:
Laura Klünder 2024-02-09 21:35:50 +01:00
parent a8261d0b3d
commit 24e14c32ae
2 changed files with 51 additions and 37 deletions

View file

@ -0,0 +1,29 @@
import numpy as np
from django.core.management.base import BaseCommand
from c3nav.mapdata.models.geometry.space import WifiMeasurement, RangingBeacon
class Command(BaseCommand):
help = 'list FTM-capable APs that have been spotted'
def handle(self, *args, **options):
found_beacons = {}
# lets collect them
for wifi_measurement in WifiMeasurement.objects.select_related("space"):
for measurement_list in wifi_measurement.data:
for measurement in measurement_list:
if measurement.get("supports80211mc", False) or measurement.get("distance", None):
found_beacons.setdefault(measurement["bssid"], []).append((wifi_measurement, measurement))
# put in the ones we know
known = {r.bssid: r for r in RangingBeacon.objects.all()}
# lets go through them
for bssid, measurements in found_beacons.items():
print(bssid, measurements[0][1]["ssid"], known.get(bssid))
points = {wifi_measurement.geometry for wifi_measurement, measurement in measurements}
for wifi_measurement, measurement in measurements:
break
#print(f" Space={wifi_measurement.space.title!r} RSSI={measurement['rssi']} "
# f"distance={measurement.get('distance')} distance_sd={measurement.get('distance_sd')}")

View file

@ -2,47 +2,32 @@
from django.db import migrations
sql_up = '''
-- create an obstacle group for every distinct obstacle color
insert into mapdata_obstaclegroup (color, titles)
select color as color,
json_build_object('en', concat('Color ', color)) as titles
from
(select distinct color
from mapdata_obstacle
where color is not null
union
select distinct color
from mapdata_lineobstacle
where color is not null) as obstacle_colors;
def forwards_func(apps, schema_editor):
ObstacleGroup = apps.get_model('mapdata', 'ObstacleGroup')
Obstacle = apps.get_model('mapdata', 'Obstacle')
LineObstacle = apps.get_model('mapdata', 'LineObstacle')
-- set the groups for colored obstacles to the previously created group with that color
update mapdata_obstacle as o
set group_id = g.id
from mapdata_obstaclegroup g
where g.color = o.color;
grouped = {}
update mapdata_lineobstacle as o
set group_id = g.id
from mapdata_obstaclegroup g
where g.color = o.color;
'''
for obstacle in Obstacle.objects.filter(color__isnull=False):
grouped.setdefault(obstacle.color, []).append(obstacle)
sql_down = '''
-- set obstacle color from associated group color and remove group
update mapdata_obstacle as o
set color = g.color, group_id = null
from mapdata_obstaclegroup g
where g.id = o.group_id;
for lineobstacle in LineObstacle.objects.filter(color__isnull=False):
grouped.setdefault(lineobstacle.color, []).append(lineobstacle)
update mapdata_lineobstacle as o
set color = g.color, group_id = null
from mapdata_obstaclegroup g
where g.id = o.group_id;
for color, obstacles in grouped.items():
group = ObstacleGroup.objects.create(
color=color,
titles={"en": f"Color {color}"}
)
for obstacle in obstacles:
obstacle.group = group
obstacle.save()
-- delete groups
delete from mapdata_obstaclegroup where true;
'''
def backwards_func(apps, schema_editor):
ObstacleGroup = apps.get_model('mapdata', 'ObstacleGroup')
ObstacleGroup.objects.all().delete()
class Migration(migrations.Migration):
@ -52,5 +37,5 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunSQL(sql_up, sql_down)
migrations.RunPython(forwards_func, backwards_func)
]